Scheduling MQTT

I am sure this is a really quick question, I have just confused myself :slight_smile:

I have a value from MQTT that I want to store at 23:55pm every night. It is the total kWh that IoT device has logged for the day (so the interval is daily).

I do loads of MQTT >Some transformation> Influx2.0DB logging on these devices. Where I have confused myself is I just need to store this value once a day at 23:55pm.

Typically all I would do is setup an inject node with the correct schedule but you cant connect the inject node to an MQTT output node to trigger the read at that time and then to other flow to store.

So a little embarrassed to ask if someone could just clarify how I would set this up on a flow. The end result would be:

<a_Timer:23:55pm> --> <Read_MQTT_Topic> --> <Output_to_InfluxDb>

You will need to store the MQTT value continuously and then using a scheduler, grab the stored value and save that to a database.

You can do this with flow or global context

<Read_MQTT_Topic> --> <store_value_in_context>

<a_Timer:23:55pm> --> <Read_stored_context_value> --> <Output_to_InfluxDb>

Got you! Thank you.

Everytime you update the context does it replace the value or append the value?

-> Value: 1 -> --- Stored Value: 1
-> Value: 2 -> ---- Stored value: Will it be 2 or will it be 2+1 =3

Depends on how you write your code and what you want to achieve.

// Example of writing a new value to a global variable (overwites old value)
// This is the most common way it is used
let new_value_from_mqtt = msg.payload; //This would come from your MQTT-In node

global.set("name", new_value_from_mqtt);

// Example of adding a new value to a global variable (adds to old value)
let new_value_from_mqtt = msg.payload; //This would come from your MQTT-In node

let current_value = global.get("name");

let updated_value = new_value_from_mqtt + current_value;

global.set("name", updated_value);

two more questions for you

  1. How often does a MQTT msg come thru with a value?
  2. If adding the value, should it be reset at 23:55 when you send it to InfluxDb?

If the answer to #2 is yes then you need to add a step in @Steve-Mcl suggestion

<a_Timer:23:55pm> --> <Read_stored_context_value> --> <Output_to_InfluxDb> --> <Reset_stored_context_value>

which could be done easily usig a change node.

Thank you @zenofmud -
So I need to figure out what the device is actually doing right now in terms of update. Let me explain.
The device is a Sonoff Switch, it show on the its own dashboard the real time values and its connected to a MQTT broker via its settings.

On the device I can see the right values updated in real time. In MQTT I can see value but dont see the right values. What does this mean:

For example the Total Energy Today gets incremented every time the boiler heats. In MQTT currently it is showing a value of 2.35 kWh but on the device its showing 4.23 kWh. Network is up, MQTT is up so I need to figure out how the Sonoff is updating to MQTT - I use many Sonoff devices and they update MQTT when there is a change so this does seem weird.

Back to your question:
Basically what I am trying to do is store the value from the device > MQTT > Influx DB 2.x so that i can then use a front end like Grafana to plot the electricity usage. On the query side I have the query working out the cost per kWH etc. I was wanting to use the "TodayEnergyUsage" value to provide me with a view of cost per day and then extrapulate over a week, month.

The "TodayEnergyUsage" value is updated constantly, so when the boiler comes on and uses energy it increments the "TodayEnergyUsage". The reason I was thinking of storing it once only at 23:55pm was to get the total usage once for the day at that time and then use the "real time / last reading" for current value.

Does that make sense?

  1. what Sonoff device is it?
  2. what software do you have installed on it?
  3. have you tried using something like MQTT Explorer to watch the broker to see when the value is updated?

So it looks like you first need to figure out why the device is not sending the value to MQTT when it is updated on the Sonoff.

  1. What device: Sonoff PowR3
  2. Running Tasmota (12.0.2 latest)
  3. Yip thats the MQTT tool I use to check the values.

Figured out what was happening and have fixed the issue, its now storing the right values :slight_smile:

Can you let us know what the solution was. That could potentially help others in the future.

Also, are you dealing with resetting the node-red global at the end of the day?

1 Like

And just to add to this - Tasmota records the total energy use for the previous day - you can get this by parsing the Tasmota/Tele/Sensor output

And you might want to make sure that your timezone etc is set correctly


You can poll the tasmota device every 24hr to retrieve the power usage.

  • Sonoff Pow (and any device with sensors) status can be requested manually with cmnd/tasmota/status 8. Additionally, Tasmota periodically sends telemetry every TelePeriod, which defaults to 300 seconds (5 minutes).

I am not sure its a solution but rather a way to to get the values to appear so needs more investigation.
Basically if I reboot the Tasmota device it updates the values in MQTT. If the device just runs its not updating the values as they are updated on the device dashboard.

Need to figure out why its not doing it dynamically i.e. as value is updated on Tasmota that is pushed into MQTT

Great tip! I have about 23 different devices between PowR2 and Pow3 devices, never checked the time zone3. Of course the time zone is wrong as its set by default to UTC, updated it now to the right time zone - thank you!

Think I found the issue and thank you to @E1cid and @craigcurtin

I was subscribing to the wrong topic. This could be a gotcha for others, I was subscribing to:


Instead of


Seems like tasmota/discovery/<DEVICE_ID>/sensors only updates on reboots where as tele/<DEVICE_ID>/SENSOR updates every 5 minutes.

Thank you both!

You can also change the update interval using the teleperiod command at the console - by default it is 300 seconds - hence 5 minutes - you can change it on the tasmota console (permanently) using

Teleperiod 10 (for 10 seconds) for instance



This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.