Why does my daily median calculation and write to channel fail unpredictably?

5 vues (au cours des 30 derniers jours)
Ray Berkelmans
Ray Berkelmans le 8 Mai 2025
Réponse apportée : Heidi le 12 Mai 2025
I am trying to calculate the daily median across a range of channels and write the result to a new field for each channel. The code seems to run sometimes, but runs into rate limitation other times. I'm not sure why since I am only writing once and to different channels?
Error using Median calcs (line 39)
Requests are too frequent. For further information, see Limitations in the documentation.
My code is:
% Sensor/channel configuration
sensors = [
struct('name', 'McKinley','channelID', 28xxxx, 'writeKey', 'G3WPJ586M55Gxxxx')
struct('name', 'DPI', 'channelID', 80xxxx, 'writeKey', 'S0E0LB45GQLMxxxx')
struct('name', 'Bryony','channelID', 29xxxx, 'writeKey', '2BPCI0IOAINPxxxx')
];
% Define date range (last 24 hours to midnight)
endTime = dateshift(datetime('now'), 'start', 'day');
startTime = endTime - hours(24);
% Preallocate a results array
results = [];
% Step 1: Read and calculate all medians
for i = 1:length(sensors)
s = sensors(i);
% Read Field 1 for the past 24 hours
[weight, ~] = thingSpeakRead(s.channelID, 'DateRange', [startTime, endTime], 'Fields', 1);
% Compute median, ignoring NaNs
medianWeight = round(median(weight, 'omitnan'),2);
% Store in results
results(i).name = s.name;
results(i).channelID = s.channelID;
results(i).writeKey = s.writeKey;
results(i).value = medianWeight;
end
% Step 2: Display results
for i = 1:length(results)
r = results(i);
fprintf('%s (Channel %d) → 24hr Median: %.2f\n', r.name, r.channelID, r.value);
end
% Step 3: Write results
thingSpeakWrite(results(1).channelID, 'Fields', 6, 'Values', {results(1).value}, 'WriteKey', results(1).writeKey);
thingSpeakWrite(results(2).channelID, 'Fields', 6, 'Values', {results(2).value}, 'WriteKey', results(2).writeKey);
thingSpeakWrite(results(3).channelID, 'Fields', 6, 'Values', {results(3).value}, 'WriteKey', results(3).writeKey);
  1 commentaire
Christopher Stapels
Christopher Stapels le 8 Mai 2025
Writing a table of values from MATLAB uses the bulk write interface.
See the limitations area there for additional info. How often are you running this code?
"The number of messages in a single bulk-update is limited to 960 messages for users of free accounts and 14,400 messages for users of paid accounts. The time interval between sequential bulk-update calls should be 15 seconds or more."

Connectez-vous pour commenter.

Réponses (2)

Ray Berkelmans
Ray Berkelmans le 8 Mai 2025
I'm running the code once per day, about a minute after midnight. So far it has run 6 out of 9 nights successfully and failed 3 nights. I'm not sure if there is something in the pattern, but so far it is 2 success, 1 fail, 2 success, 1 fail, 2 success, 1 fail. Is this co-incidental?
I have a paid account, so could easily add a 1 sec delay between writes. I'll try that and see how it goes.
  2 commentaires
Vinod
Vinod le 9 Mai 2025
If you don't need to run it at exactly midnight, I'd also suggest some more random time (like 12:14 AM, for example). This will avoid the issue of potentially running into jitter from other work which is also scheduled exactly at midnight.

Connectez-vous pour commenter.


Heidi
Heidi le 12 Mai 2025
A combination of 1s delay between writes and moving the TimeControl away from midnight seems to be doing the trick. Three nights good so far... :)

Communautés

Plus de réponses dans  ThingSpeak Community

Catégories

En savoir plus sur Configure Accounts and Channels 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