Daily average of a temperature

Hi all,
I have a ds18b20 and esp8266 with espeasy which posts a temperature reading to thingspeak every 5 minutes, works without any issue.
I want to have multiple fields to be able to show a daily average. I think this is not possible with espeasy directly so i have to get some workaround with the visualizations. I have the code below to show an average, it outputs a number, but nothing is plotted.
% Read temperature over the past hour from a ThingSpeak channel
readChannelID = 1462749;
TemperatureFieldID = 1;
readAPIKey = '__REDACTED__';
% Get temperature data for the last 60 minutes
[data, timeStamps ] = thingSpeakRead(readChannelID,'Fields',[TemperatureFieldID], 'NumPoints',30,'ReadKey',readAPIKey);
% Calculate the average temperature
avgTemperature = mean(data);
display(avgTemperature,'Average Temperature');
% Plot temperature and timestamp
plot(timeStamps,avgTemperature)
ylabel('temperature (°C)');

1 Comment

Did you solve this issue I have the same problem, I can compute the average but not plot it.

Sign in to comment.

Answers (2)

Your avgTemperature is just a scalar, a single point. Maybe you want to plot the data instead?
plot(timeStamps, data)

10 Comments

Yeah sure, that works, but it doesn't give a daily average ;)
What do you mean by plotting average? avgTemperature is just a single point. You want to draw a single dot in the figure?
Your timeStamps is an array of 30, your avgTemperature is a scaler, a single average of all 30 temperatures.
hmm in that case i think my wording is wrong.
So, i know have all my datapoint plotted in field 1, every 5 minutes. I would like to see a chart with the average temperature of every day. so for 1ste august, 2nd of august,...
You need to review the ThingSpeak document.
Right now, your code just read 30 temperatures from ThingSpeak and then computes the average of these 30 temperatures.
You might need to
  1. figure out how to read multiple temperatures for a given day, and then do an average
  2. Use a loop to loop through all the days you want to check.
I already have a field which plots all the data, from a sensor which writes every 5 minutes.
This is only for calculating average and plotting it. Other way would be to get esp_easy to update 2 fields and just do it there.
Still confused about what you want to plot. You need to have an array of averages if you want to ploth averages, but here thter is only one.
Or say when you call plot(a, b), both a and b shall be arrays and they shall have the same size.
So, will elaborate on this.
I have a temp sensor which outputs temperature readings every 5 minutes. This is plotted with temp on y axis and time on x axis.
I want another chart which plots the average temperature reading of each day so i can see the average temperature of some days compared, how it evolves.
I see. So first you need to compute the dailyAverages, not just one single average as shown in your code.
Could you maybe guide me through the process?
How about this:
  1. Use a for loop to loop through all the days you want to plot.
  2. Inside the loop, get the temperatures of that particular day, and do an average
  3. append this average to an array, inside the for loop
  4. once the for loop is done, you have an array of averageTemperatures
  5. Plot that array.

Sign in to comment.

You can use the pencil icon on the top of the ThingSpeak plot to make the timeScale Daily.
Also, you can write the output from the code above to another ThingSpeak channel. We call that a derived channel.
Instead of 'NumPoints',30,
use 'NumDays', 1.
Use thingSpeakWrite() to write the data to you new channel.
And then use a timecontrol to call that code daily. Then you will have a plot of daily average.
See the getting started tutorial for an example of that workflow.
Also you could create a matlab visualisation that reads many days of data and then use retime() function to calculate the daily avarage and plot it.
I reccomend using 'outputformat','timetable' when you read the data becasue retime works nicely on timetables.

Communities

More Answers in the  ThingSpeak Community

Categories

Products

Asked:

on 3 Aug 2021

Commented:

on 9 Feb 2022

Community Treasure Hunt

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

Start Hunting!