Energy Sensor from NodeRed to HomeAssistant

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?


function-node:

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)

{"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)

[{"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

Image from Dishwasher Energy Consumption

Image from the Sensor of Dishwasher Energy Consumption

Image from DishwasherStatus

Image from the Sensor of DishwasherStatus

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.

i do that in the 2.node (change-node)
Bildschirmfoto 2022-10-31 um 14.46.05

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



maybe this helps?

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

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.

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

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:

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.

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