Update/replace channel value?

7 ビュー (過去 30 日間)
Bernd
Bernd 2025 年 4 月 15 日
編集済み: Walter Roberson 2025 年 4 月 22 日
I know how to write data with MATLAB Analysis to channel, in my case I read the absolute value from my electricity counter (which updates every 1min) and calculate the consumption of the last 20min.
datetimeStop = datetime('now');
datetimeStart = datetime('now')- minutes(20);
data = thingSpeakRead(readChannelID, Fields=[1], DateRange=[datetimeStart,datetimeStop], ReadKey=readAPIKey);
analyzedData = data(length(data)) - data(1);
thingSpeakWrite(writeChannelID, analyzedData, WriteKey=writeAPIKey);
I would like to write to a second channel the average consumption for each hour of the day, e.g. 01:00, 02:00, ...
My problem: With thingSpeakWrite I add values to a channel, but if I calculate the running average for each hour at every new day - with (n*avg_old + new_value)/(n+1) - I want to update/replace the average. How is that possible?
BR
Bernd

回答 (1 件)

Vinod
Vinod 2025 年 4 月 15 日
The data in a ThingSpeak channel is a time series. Think of a value in the field as a measurement made at a certain point in time. If I understand what you are trying to do, it is "changing history" in that timeseries by going back and updating/replacing a previous measurement.
I'd recommend a better approach is to only update the value in the channel when you have all the data for the period. So, let's say you're doing a 4 hour running average, then wait until the 4 hour window is complete to calculate and update the value in the field.
  2 件のコメント
Bernd
Bernd 2025 年 4 月 20 日
移動済み: Walter Roberson 2025 年 4 月 20 日
Thanks Vinod for your answer, but I think I do not understand your recommended approach.
So I understand that a thingspeak channel is only ment to be a time series and does not work to hold a single value (which would be the case if I want to store a rolling average), right?
What could be a workaround? For sake of simplicity, let's assume I want to compute each day the rolling average for the daily consumption (I know this use cases does not make sense, but let's keep it simple) like this:
Day 1 consumption: 10 --> Avg = 10
D2 consumption: 20 --> Avg* = 15
D3 consumption: 3 --> Avg* = 11
....
I know how to read at the end of a day the consumption of the last 24h hours, but where could I "store" the newly calculated average, because I am not interested in a series plot but in the single average consumption value?
BR
Bernd
Vinod
Vinod 2025 年 4 月 22 日
編集済み: Walter Roberson 2025 年 4 月 22 日
Thanks for clarifying that Bernd. I think I understand your use case a bit better now.
Every ThingSpeak channel has a metadata field. You can use the REST API to update the metadata of the ThingSpeak channel on a rolling basis. See here for more info:

サインインしてコメントする。

コミュニティ

その他の回答  ThingSpeak コミュニティ

カテゴリ

Help Center および File ExchangeThingSpeak についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by