# Energy Sensor from NodeRed to HomeAssistant

**URL:** <https://discourse.nodered.org/t/energy-sensor-from-nodered-to-homeassistant/69838>\
**Category:** General\
**Tags:** function-node, home-assistant\
**Created:** [31 October 2022 12:35 UTC](https://discourse.nodered.org/t/energy-sensor-from-nodered-to-homeassistant/69838 "2022-10-31T12:35:09Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![MrThiemann](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mrthiemann/32/68063_2.png) [@MrThiemann](https://discourse.nodered.org/u/MrThiemann)\
**Post date:** [31 October 2022 12:35 UTC](https://discourse.nodered.org/t/energy-sensor-from-nodered-to-homeassistant/69838/1 "2022-10-31T12:35:10Z")

</div>

Hey,  
I have a flow here that I'm "trying" to transfer my consumption to HomeAssistant.

I would say that this works well except for one thing.  
what amazes me now is the evaluation in the energy dashboard.  
Here he seems to "add up" all the data.  
So I think maybe there is a bug in the function-node and from here something wrong is being passed?

 ![Bildschirmfoto 2022-10-31 um 12.48.06](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/e/fe05f0d9d285a7e4becd7bee7e2e4749ef8f8c03.png)

* * *

 ![Bildschirmfoto 2022-10-31 um 13.03.53](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/7/a7b530c5b53ebdb237ce89826fa66f7636f4a90d.png)

function-node:

```auto
const state_energy = Number(flow.get('dishwasher'));

msg.payload.isOn = msg.payload.event === "stop" ? "off" : "on";
if (msg.payload.event === "running" || msg.payload.event === "pre_start") {
    msg.payload.energy = state_energy + Number(msg.payload.energy_delta);
} else if (msg.payload.event === "stop") {
    msg.payload.energy = state_energy.toFixed(5);
} else {
    msg.payload.energy = state_energy;
}

return msg;

```

Debug-Node from "Dishwasher Energy Consumption" (this is updated every 3 seconds)

```auto
{"payload":{"name":"Dishwasher","event":"running","time":8615,"energy":152.0001,"energy_delta":0.0001,"isOn":"on"},"_msgid":"8c1bb9b25622932d"}

```

and here is my flow: (but since this can only be used if you use HomeAssistant, I'll add pictures of the individual settings)

```auto
[{"id":"2d779f71abba3b6c","type":"poll-state","z":"c7ab1fb989e3ea62","name":"Dishwasher Power Verbrauch","server":"","version":2,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"updateinterval":"3","updateIntervalType":"num","updateIntervalUnits":"seconds","outputinitially":false,"outputonchanged":false,"entity_id":"sensor.smart_socket_geschirrspueler_power","state_type":"str","halt_if":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"x":180,"y":300,"wires":[["ed6a596a49d4ee8b"]]},{"id":"ed6a596a49d4ee8b","type":"change","z":"c7ab1fb989e3ea62","name":"","rules":[{"t":"set","p":"dishwasher","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":440,"y":300,"wires":[["4b807df4e4ef8381"]]},{"id":"4b807df4e4ef8381","type":"power-monitor","z":"c7ab1fb989e3ea62","name":"Dishwasher","startthreshold":0,"stopthreshold":0,"startafter":"5","stopafter":"100","x":730,"y":300,"wires":[["e5c5d8d790a075e3"]]},{"id":"e5c5d8d790a075e3","type":"function","z":"c7ab1fb989e3ea62","name":"","func":"// const state = global.get('bathroomState');\nconst state_energy = Number(flow.get('dishwasher'));\n\nmsg.payload.isOn = msg.payload.event === \"stop\" ? \"off\" : \"on\";\nif (msg.payload.event === \"running\" || msg.payload.event === \"pre_start\") {\n msg.payload.energy = state_energy + Number(msg.payload.energy_delta);\n} else if (msg.payload.event === \"stop\") {\n msg.payload.energy = state_energy.toFixed(5);\n} else {\n msg.payload.energy = state_energy;\n}\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":960,"y":300,"wires":[["278ec9542ad2ce10"]]},{"id":"f75c3ddb6827657f","type":"ha-binary-sensor","z":"c7ab1fb989e3ea62","name":"GeschirrspuelerStatus","entityConfig":"118b28f8.cd4c77","version":0,"state":"payload.isOn","stateType":"msg","attributes":[{"property":"name","value":"payload.name","valueType":"msg"},{"property":"time","value":"payload.time","valueType":"msg"},{"property":"energy","value":"payload.energy","valueType":"msg"}],"inputOverride":"allow","outputProperties":[],"x":1260,"y":360,"wires":[[]]},{"id":"f17702e119c90e77","type":"ha-sensor","z":"c7ab1fb989e3ea62","name":"Dishwasher Energy Consumption","entityConfig":"0ce5f602ea5bbd3b","version":0,"state":"payload.energy","stateType":"msg","attributes":[{"property":"name","value":"payload.name","valueType":"msg"},{"property":"time","value":"payload.time","valueType":"msg"},{"property":"energy","value":"payload.energy","valueType":"msg"},{"property":"state_class","value":"total_increasing","valueType":"str"}],"inputOverride":"allow","outputProperties":[],"x":1300,"y":300,"wires":[[]]},{"id":"278ec9542ad2ce10","type":"junction","z":"c7ab1fb989e3ea62","x":1100,"y":300,"wires":[["f17702e119c90e77","f75c3ddb6827657f"]]},{"id":"118b28f8.cd4c77","type":"ha-entity-config","server":"5a3777c7.b71588","deviceConfig":"","name":"binary sensor config for GeschirrspülerStatus","version":"6","entityType":"binary_sensor","haConfig":[{"property":"name","value":"Geschirrspüler status"},{"property":"icon","value":"mdi:washing-machine"},{"property":"entity_category","value":""},{"property":"device_class","value":""}],"resend":true},{"id":"0ce5f602ea5bbd3b","type":"ha-entity-config","server":"5a3777c7.b71588","deviceConfig":"","name":"sensor config for DishwasherEnergy","version":"6","entityType":"sensor","haConfig":[{"property":"name","value":"Dishwasher Energy Consumption"},{"property":"icon","value":"mdi:lightning-bolt"},{"property":"entity_category","value":""},{"property":"device_class","value":"energy"},{"property":"unit_of_measurement","value":"kWh"},{"property":"state_class","value":""}],"resend":false}]

```

Image from Dishwasher Power Verbrauch

 ![Bildschirmfoto 2022-10-31 um 13.29.06](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/5/b5fadac2386cfe522fe4aa60966a372fc69b2e53.png)

Image from Dishwasher Energy Consumption

 ![Bildschirmfoto 2022-10-31 um 13.29.24](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/e/9eda3e59eb2b32f42994d51ecd7ac5f19ba1918a.png)

Image from the Sensor of Dishwasher Energy Consumption

 ![Bildschirmfoto 2022-10-31 um 13.29.40](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/3/d30ab13381beb262d7fb71fe8926a356317102d0.png)

Image from DishwasherStatus

 ![Bildschirmfoto 2022-10-31 um 13.30.01](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/e/ae6174b507872f937b172ce2994f82a62999a267.png)

Image from the Sensor of DishwasherStatus

 ![Bildschirmfoto 2022-10-31 um 13.30.19](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/a/6a349e7e6882042950e7cef5f7a80beccc9842b7.png)

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [31 October 2022 13:43 UTC](https://discourse.nodered.org/t/energy-sensor-from-nodered-to-homeassistant/69838/2 "2022-10-31T13:43:54Z")

</div>

> [@MrThiemann](#):
>
> `flow.get('dishwasher')`

Where is this dishwasher set ?

I also suspect that `state_energy.toFixed(5);` and `Number(flow.get('dishwasher'));` actually changes the flow variable because javascript makes shallow copies.

---

<div class="post-metadata">

**Author:** ![MrThiemann](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mrthiemann/32/68063_2.png) [@MrThiemann](https://discourse.nodered.org/u/MrThiemann)\
**Post date:** [31 October 2022 13:47 UTC](https://discourse.nodered.org/t/energy-sensor-from-nodered-to-homeassistant/69838/3 "2022-10-31T13:47:29Z")

</div>

i do that in the 2.node (change-node)  
 ![Bildschirmfoto 2022-10-31 um 14.46.05](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/4/54b9ea9104e03232d1dc5db52b467265b9cd27ec.png)

---

<div class="post-metadata">

**Author:** ![MrThiemann](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mrthiemann/32/68063_2.png) [@MrThiemann](https://discourse.nodered.org/u/MrThiemann)\
**Post date:** [31 October 2022 13:50 UTC](https://discourse.nodered.org/t/energy-sensor-from-nodered-to-homeassistant/69838/4 "2022-10-31T13:50:50Z")

</div>

My dishwasher is currently working too. if we also need live data?!  
the value is checked every 3 seconds

 ![Bildschirmfoto 2022-10-31 um 14.48.44](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/3/13148513a1904b3f5e1dc02c65063e3fea5516a2.png)  
 ![Bildschirmfoto 2022-10-31 um 14.48.58](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/c/9ca96c76eb6870e993c338cd26d7bc9de9abcb7c.png)  
 ![Bildschirmfoto 2022-10-31 um 14.49.16](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/5/752e34ca1317d0c8bdc6251bd3395395db8d292c.png)

---

<div class="post-metadata">

**Author:** ![MrThiemann](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mrthiemann/32/68063_2.png) [@MrThiemann](https://discourse.nodered.org/u/MrThiemann)\
**Post date:** [31 October 2022 13:56 UTC](https://discourse.nodered.org/t/energy-sensor-from-nodered-to-homeassistant/69838/5 "2022-10-31T13:56:20Z")

</div>

maybe this helps?

 ![Bildschirmfoto 2022-10-31 um 14.54.14](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/a/aa868359d7f620a1c0119f9a059bf75e7fe8c7b1.png)

 ![Bildschirmfoto 2022-10-31 um 14.55.41](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/5/c58c04653563185d4b88f11fb0c3fca0932e7f2d.png)

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [2 November 2022 06:09 UTC](https://discourse.nodered.org/t/energy-sensor-from-nodered-to-homeassistant/69838/6 "2022-11-02T06:09:55Z")

</div>

What are you trying to achieve ?

You need to understand the difference between instantaneous power and energy consumed

Firstly what is the device you are using to monitor the diswasher sending ? Is it energy consumed since the last reading or is it the highest reading in the elapsed time etc etc

Usually an energy plug will send you an instantaneous reading i.e. how much power was being used at that precise moment - it is up to you to then interpret that

So say it was using 1000w and the readings were every 3 seconds, you haev to decide is that 1000w every second for the 3 seconds - or is it 333w per second ?

Craig

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [2 November 2022 09:06 UTC](https://discourse.nodered.org/t/energy-sensor-from-nodered-to-homeassistant/69838/7 "2022-11-02T09:06:08Z")

</div>

> [@craigcurtin](#):
>
> So say it was using 1000w and the readings were every 3 seconds, you haev to decide is that 1000w every second for the 3 seconds - or is it 333w per second ?

Watts is a measure of rate of usage, think of speed in a car for example for example. If you measured the speed of a car at 100 mph and measured it every 3 seconds then it is not doing 100mph every 3 seconds, or 33 mph every second, it is just doing 100 mph. Then to get the distance travelled (the equivalent of Watt hours, or kilowatt hours) then multiply the speed by the time. So if the car did that speed for 30 minutes then it would travel 100 \* 30 / 60 miles. Similarly if the machine is consuming 1000W and continues to do that for 30 minutes then it would use 1000 \* 30 / 60 Watt hours, or 0.5 kWh which is what you pay for.

---

<div class="post-metadata">

**Author:** ![MrThiemann](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mrthiemann/32/68063_2.png) [@MrThiemann](https://discourse.nodered.org/u/MrThiemann)\
**Post date:** [2 November 2022 20:05 UTC](https://discourse.nodered.org/t/energy-sensor-from-nodered-to-homeassistant/69838/8 "2022-11-02T20:05:55Z")

</div>

> [@craigcurtin](#):
>
> What are you trying to achieve ?

I would like to display my electricity consumption in HomeAssistant.  
I would realize this in advance via nodered, since I "prepare" almost everything for HomeAssistant here

> [@craigcurtin](#):
>
> Firstly what is the device you are using to monitor the diswasher sending ? Is it energy consumed since the last reading or is it the highest reading in the elapsed time etc etc

As a device I currently have the sockets from Gosund.  
I have integrated Tuya for Ampere, Power and Voltage as sensors.  
But if I integrate them via local-Tuya, I only have them as attributes.

i created a sensor as follow:

```auto
template:
  - sensor:
      - name: "Smart Socket Dishwasher Power"
        unique_id: smart_socket_dishwasher_power
        state: |
          {{ state_attr('switch.smart_socket_socket_1', 'current_consumption') }}
        state_class: measurement
        unit_of_measurement: "W"

```

* * *

I "rebuilt" the flow and put the function node in front of the status and no longer in front of the status AND ConsumptionTotal.  
As I can observe, I now get "better" values.

 ![Bildschirmfoto 2022-11-02 um 21.13.05](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/d/fd8a01e8683fe2ca6299774b49d8ea6ace393228.png)

 ![Bildschirmfoto 2022-11-02 um 21.10.09](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/e/8e62bb7a30fa22879dd1531d20ad134a30c6b852.png)

---

<div class="post-metadata">

**Author:** ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)\
**Post date:** [3 November 2022 00:02 UTC](https://discourse.nodered.org/t/energy-sensor-from-nodered-to-homeassistant/69838/9 "2022-11-03T00:02:21Z")

</div>

OK good to see you have some progress - but you do need to work out what it is they are sending - whether it is a "spot/instantaneous" reading of power every 3 seconds, or whether it is the average over the 3 seconds, or the accumulated use over 3 seconds.

Once you have that info you can then better decide how to use and display the info.

The easiest way to find this info is to get a couple of standard lightbulbs - preferably not LED - but halogen or incandescant as they are constant power devices. You can then see pretty easily what the 3 second values represent

Craig

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [2 January 2023 00:02 UTC](https://discourse.nodered.org/t/energy-sensor-from-nodered-to-homeassistant/69838/10 "2023-01-02T00:02:50Z")

</div>

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