Reading from Thingspeak: Daily averages are easy. How do I get a monthly average?

10 vues (au cours des 30 derniers jours)
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 commentaires
Vinod
Vinod le 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.
Håkon Dahle
Håkon Dahle le 15 Nov 2019
Thanks for replying!
The problem: I have a large dataset (10 years of hourly measurements, i.e. about 90.000 measurements stored in a single Thingspeak channel).
In my mobile app I want to create a single chart in order to show a trend over time. Plotting 90.000 datapoints in a single chart makes no sense. Using daily averages helps a lot of course - it results in about 3600 datapoints. But that's still overkill for my charting needs.
I would like to plot montly averages over 10 years, this should make for a nice and compact chart for a mobile app. Not using all 90.000 datapoints also makes the mobile app a lot faster - fetching and plotting 10 x 12 datapoints is preferable.
I could of course do all the averaging in my app, but that still means I have to fetch all the data from Thingspeak before averaging/reducing the data in the app itself. Ideally I would like to request 10 years of monthly averages (i.e. 120 data points).
If there is a way to accomplish this in Matlab it would be great if there is a pointer to some similar example as I am not familiar with Matlab (I do most of my client and server-side stuff in JavaScript/Node.js).

Connectez-vous pour commenter.

Réponse acceptée

Christopher Stapels
Christopher Stapels le 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.
Here is a tutorial for using one of the templates to calculate averages.:
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.
  1 commentaire
Håkon Dahle
Håkon Dahle le 17 Nov 2019
Hi -
thanks! I will go with the core of our suggestion - "preprocess the data and store it in another channel". I looked at using Matlab for this, but I fear the learning curve will be a bit steep. I will simply use javascript/node.js on the server side for this task. Scheduling a node.js run once a month to update the "other" channel should be easy. I played around with using javascript for processing the monthly averages (and creating the chart) and I liked the result: https://jsfiddle.net/hdahle/6wLhbc7p/151/
-h

Connectez-vous pour commenter.

Plus de réponses (1)

Christopher Stapels
Christopher Stapels le 18 Nov 2019
Modifié(e) : Christopher Stapels le 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 commentaires
abba baab
abba baab le 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
Christopher Stapels le 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.

Connectez-vous pour commenter.

Communautés

Plus de réponses dans  ThingSpeak Community

Catégories

En savoir plus sur Read Data from Channel dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by