Storing energy data


im having the problem right now, that i need to store data in some kind.

Im using shelly devices with MQTT, so its reporting its total energy consumption via a /energy topic.

But this amount is only from "powering" the device. If the device lost his power, this total energy is set to zero.

What i need now is:

If i store it in a DB or varbiable or whatever and the value sums up till (for example) 10kWh, then looses his power and get it back.

The amount from NOW has to be sumed up with the already existing 10kWh. This amount should stay also after my Raspberry looses his power.

I hope you understand my problem.

Thank you

Have a look here:

But how?
I know about global Variables. But i dont think thats the thing i need.

well you could store it in a database.

Global variables can also be arrays!

Thats not helping me.. do you have some examples?

I have no clue how to do it.

You need to define what you are trying to do in more detail. Have you tried anything, or are you just starting on your method?

It might help you (and us) if ou were to write out the steps you need to do to reach your goal. Once you do that you might see how to impliment it.

if you don’t want to program it all yourself take a look at open energy monitor which integrates well with Node-red and was designed for analysis of energy usage

Did you see that you can make them persistent? That means they are saved in a file and will be available following a restart. So you could save your running total in a persistent context variable (I suggest a flow rather than global variable) and then you can use that as the base value to add to the values from the device.

Well, im not the best programmer at all. Thats why im asking here for a solution.

Node-Red gives alot of solutions i may not know now.

I dont know how to explain well. I hope you understood my problem.

How should i use the context variables to add the power to a persistent variable?

If my Total consumption is around lets say 100Wh. Now the Shelly device is set to zero from its power. Then it starts counting again. Lets say it shows 10Wh at its energy topic. Then this 10Wh should be add to the existing 100Wh. If it now counts up to 15Wh, it have to add only the 5Wh diffrence from the last message to the total consumption, otherwise i would get bad results.

Hopefully my explaination is understandable

On startup pick up the persistent context value (100) from the context and save it in a different variable in context, something like baseUsage. Each time a new value comes from the device (say 10, then 15) then add baseUsage to it (so 100+10=110, then 100+15=115) and use that value for display, and save it back to the persistent context value. So the persistent one becomes 110 then 115, but baseUsage stays at 100. If you restart again at that point then it will pick up the 115 and put that in baseUsage ready for the next reading from the device.
Actually that will only work if you restart both node-red and the Shelly at the same time, so you will have to work at it a bit more. Hopefully that is enough to get you started at least.

Are you using a Shelly EM?

The product page says:
365 days built-in energy history (Wh)

Sounds nice. But restarting the device and node-red at the same time is no solution i think. What could be a workaround?

@ghayne: Im using more Shelly devices like Shelly 1PM, Shelly Plug and Shelly Plug S. Everything has a powermeter inside, but the "built-in energy history" is only included if you are using the cloud.

If you are using MQTT-Only then you have no history. Only the consumption from powering the device up

OK, got it.

First you have to forget about the fact that it is software and work out logically how you might do it. Once you have worked out some logic then we can work out how to implement it. So the question is, I suppose, how can you tell that the shelly has reset? Is there some way of determining that from the data you can get from it?
If there is then you can extend my suggested algorithm to take the appropriate action when it sees the shelly has reset.

Well, the shelly got a will-topic. Maybe this topic could show that the shelly is alive or dead?

That will only tell you if it stays offline for more than about 30 secs I think (though not certain about that). Consider what will happen to the energy used value that you receive over a reset.

I cant believe its so complicated.

I feel like iam the only one on the world using shelly devices local with mqtt and node-red.

ive now done something to test like

var globaleVariable = global.get("variable"||2);
var globaleVariable = msg.payload;

return {payload: globaleVariable};


var shellyEnergyTest = global.get("shellyEnergyTest");
var shellyEnergyTestOnline = global.get("shellyEnergyTestOnline");

msg.payload = shellyEnergyTest.toString();

if (shellyEnergyTestOnline === "true")
return msg;

That just gives me the current energy value out if will-topic says true. if false its not give me out anything.

Shouldnt the "false" give a command to save up the current consumption to another variable?

Better test this out... i think its a "start"