Reading from Thingspeak: Daily averages are easy. How do I get a monthly average?
9 views (last 30 days)
Show older comments
Håkon Dahle
on 14 Nov 2019
Commented: Christopher Stapels
on 21 Jan 2023
I use the REST API to read data from Thingspeak and this works well:
This request correctly returns one value per day. However I need a monthly average - a single value per month. Ideally something like:
Is there a way to achieve something like this?
2 Comments
Vinod
on 14 Nov 2019
One way I can think of doing this is to have MATLAB code that is scheduled to run once a month using the TimeControl app. The MATLAB code in the MATLAB analysis app reads the values form the "raw data" channel and computes the monthly average which gets written into a "derived channel". You can then directly query your "derived channel" to get monthly data.
Can you tell us more about what you are doing? That can help guide us to the appropriate solution.
Accepted Answer
Christopher Stapels
on 15 Nov 2019
There are built in template codes to help you develop MATLAB code. If you go to the MATLAB analysis or MATLAB Visualizations apps, you can select from the templates after choosing to create a new instance of the app.
For your example, I think you will proably want to preprocess the data and store it in another channel. Reads are limited to 8000 points, so you want be able to read all the data in a single call.
I would reccomend writing a script that reads the data and writes the daily avaerages to another channel. Then your Mobile app can read the values or call the charts API for the derived channel. Going forward, you can set a timeControl to write that daily average to the derived channel every day (as Vinod suggested). Historicall you may have to process them in a few batches.
I can provide more details if you get stuck with the code.
More Answers (1)
Christopher Stapels
on 18 Nov 2019
Edited: Christopher Stapels
on 21 Jan 2023
I also like the look of your result. But I am afraid that it makes a bunch of API requests to the server in a big burst. Its fine for once in a while, but it you are regularly using this kind of code, you could eventually trigger a rate limiting response from the ThingSpeak server. Your code already generates the monthly data, you can write the historical data to the derived channel, and reduce the future burden.
If you didn't want to use your own server-side resources, you could use TimeControl to schedule the MATLAB code to run at regular intervals. I understand not wanting to use a new language, but MATLAB tries pretty hard to be easy to learn.
Just in case you are considering it, the format would look something like this:
Create a new MATLAB analysis with this pseudocode
edited per comments below
numDays=30; % for monthly
readChannelId =123455;
writeChannelId = 543211;
myReadKey='XXXXXXXXXXXXXXXX';
myWriteKey='yyyyyyyyyyyyyyyy';
myData=thingSpeakRead(readChannelID, 'ReadKey',myReadKey,'OutputFortmat','timetable','numDays',numDays);
aveData=retime(myData,'regular','mean','TimeStep',days(numDays));
thingSpeakWrite(writeChannelId, aveData, 'WriteKey', writeAPIKey);
Then use TimeControl to run the Analysis every numDays.
3 Comments
abba baab
on 21 Jan 2023
Thanks for the code. Error on last line. aveCar should be aveData.
"MATLAB tries pretty hard to be easy to learn." So true. It is easily the hardest language so far (coming from HTML, CSS, PHP, Arduino and Python). No copy paste wtf. Documentation is horrible and lacks real examples.
Christopher Stapels
on 21 Jan 2023
Thanks for the syntax suggestion, I have fixed it above.
I'm sorry to hear you are frustrated with our documentation, its realtively well known as very high quality from most reports. If you provide an example that you didnt like, I can look into it.
Communities
More Answers in the ThingSpeak Community
See Also
Categories
Find more on Read Data from Channel in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!