How to compute and aggregate energy production

Frederic Lhoest on 26 Oct 2022 (Edited on 3 Nov 2022)
Latest activity Reply by Christopher Stapels on 18 Nov 2022

Dear Community,
I'm new on ThingSpeak for nearly 4 weeks and I'm struggling to compute some MATLAB data. Mostly because I don't know the syntax.
I have a hourly graph of solar production in kW, I'm taking a sample every 20 seconds, and I would like to aggregate the data on a daily basis in kWh.
I guess I need to select the last 7 days of data using some thingsread function, add the data into an array and compute the daily data into an average/sum ... As you can see, I mostly got the mechanics but I'm struggling to create the car ;) Is there anyone kind enough to assist me ?
Thansk a lot !
Christopher Stapels
Christopher Stapels on 31 Oct 2022
Here it is in two glorious lines. Ive done it using the traffic monitor channel, since it posts data every 15 seconds, you can't read 7 days in one try.
myData=thingSpeakRead(38629,'numdays',2,'outputformat','timetable');
newData=retime(myData,'hourly','sum');
Frederic Lhoest
Frederic Lhoest on 31 Oct 2022
Ok, so I gave it a try but without luck :
Here is the code section :
%% Read Data %%
[data, time] = thingSpeakRead(readChannelID, 'Field', fieldID1, 'NumDays', 2, 'ReadKey', readAPIKey);
newData=retime(data,'hourly','sum');
%% Visualize Data %%
plot(time, newData);
title("Last 2 days Aggregation");
xlabel("Time");
ylabel("Production (Wh)");
I'm having this error :
Incorrect number or types of inputs or outputs for function 'retime'.
Error in aggregate (line 21)
newData=retime(data,'hourly','sum');
In case of you wonder, Line 21 is where retime is used.
Christopher Stapels
Christopher Stapels on 1 Nov 2022
Look at what else is different about the code I gave you, especially in the first line.
Frederic Lhoest
Frederic Lhoest on 1 Nov 2022
I understood, I'm using an array with 2 dimensions ? right ?
I'm sorry I'm discovering this language...
Christopher Stapels
Christopher Stapels on 1 Nov 2022
Sure, welcome to MATLAB. Also look the the name, value pairs I used at the end to get a timetable output. The retime function needs a timetable to act on. You can see those requirements in the doc. MATLAB has famously good doc.
Frederic Lhoest
Frederic Lhoest on 15 Nov 2022
I'm sorry but I'm still not able to do anything :-/
Christopher Stapels
Christopher Stapels on 15 Nov 2022
Please show me what you tried and what result you are getting. Here is some updated code.
myData=thingSpeakRead(readChannelID,'numdays',2,'outputformat','timetable'...
,'ReadKey', readAPIKey);
newData=retime(myData,'hourly','sum');
%% Visualize Data %%
plot(newData.Timestamps, newData.(1));
title("Hourly Aggregation");
xlabel("Time");
ylabel("Production (Wh)");
Frederic Lhoest
Frederic Lhoest on 17 Nov 2022 (Edited on 17 Nov 2022)
Hi Christopher,
Thanks a lot. I know what's hapening ... Matlab is too powerful ;)
So, I have the data plot now, how can I geet a full week ? I can seemax 3 days.
I know you're stating above I cannot see 7 days in one try, can you elaborate on that ? Since we are aggregating data now, we should have less data in one go ?
Christopher Stapels
Christopher Stapels on 18 Nov 2022
Can you try the MATLAB onramp? Its really helpful to get going. If you still have troubles after that, consider starting a new thread on MATLAB answers.
Frederic Lhoest
Frederic Lhoest on 31 Oct 2022
Wow ....
Ok let me try on my side and I'll let you know what I came up with !