# Understanding Shelly aenergy vs apower for daily power consumption?

**URL:** https://discourse.nodered.org/t/understanding-shelly-aenergy-vs-apower-for-daily-power-consumption/72331
**Category:** Hardware
**Created:** [16 December 2022 18:00 UTC](https://discourse.nodered.org/t/understanding-shelly-aenergy-vs-apower-for-daily-power-consumption/72331 "2022-12-16T18:00:20Z")
**Posts on this page:** 1
**Showing post:** 19

<div class="post-metadata">

### Author: ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)
#### Post date: [17 December 2022 12:20 UTC](https://discourse.nodered.org/t/understanding-shelly-aenergy-vs-apower-for-daily-power-consumption/72331/19 "2022-12-17T12:20:42Z")

</div>

> [@Colin](#):
>
> Do you mean on the way into the influx database?

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;

```auto
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

```

```auto
[{"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"]]}]

```

---

_[View the full topic](https://discourse.nodered.org/t/understanding-shelly-aenergy-vs-apower-for-daily-power-consumption/72331)._
