Calculate it before writing to the database.
Save the reading to context, then when another reading arrives compare it with the previous reading to determine how much energy has been used between those times, and write that to the database, and then save the current reading to context, etc, etc.
You can determine and deal with the device being reset because the current data would be less than the previous.
To then read it in influx, just sum the values over whatever time period that you wish.
Something like this would do, but I'm sure @Colin could improve this further;
let lastReading = context.get('energy') || 0
let currentReading = msg.payload
let energy
if (currentReading < lastReading) {
energy = currentReading
} else {
energy = currentReading - lastReading
}
context.set ('energy', currentReading)
msg.payload = energy
return msg
[{"id":"5525c984cfe71ca1","type":"inject","z":"8ec88bd9c36c3f6b","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"50","payloadType":"num","x":195,"y":1035,"wires":[["17c5b3c05d36205b"]]},{"id":"17c5b3c05d36205b","type":"function","z":"8ec88bd9c36c3f6b","name":"Energy calcs","func":"const lastReading = context.get('energy') || 0\nconst currentReading = msg.payload\nlet energy\nif (currentReading < lastReading) {\n energy = currentReading\n} else {\n energy = currentReading - lastReading\n}\ncontext.set ('energy', currentReading)\nmsg.payload = energy\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":375,"y":1035,"wires":[["7b5f5791bb689fba"]]},{"id":"7b5f5791bb689fba","type":"debug","z":"8ec88bd9c36c3f6b","name":"Energy","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":555,"y":1035,"wires":[]},{"id":"a9a85d45b0853b19","type":"inject","z":"8ec88bd9c36c3f6b","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"60","payloadType":"num","x":194.8958282470703,"y":1076.3333740234375,"wires":[["17c5b3c05d36205b"]]},{"id":"5452cde97709f5cd","type":"inject","z":"8ec88bd9c36c3f6b","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"100","payloadType":"num","x":196.8958282470703,"y":1120.3333740234375,"wires":[["17c5b3c05d36205b"]]}]