Adding values endlessly

Sorry for the inprecise title - I just don’t know how to put it better…

So what I want to do is really simple in theory.

I have a Sensor which sends values, and I want home assistant to simply add them endlessly.

So it initially sends 5, home assistant shows 5
it sends 7, home asistant shows 12
it sends 2, home asistsant shows 14
it sends 0.25, home asistsant shows 14.25
and so on

What I’m trying to do with this, is to create a theoretical power consumption sensor. After having tried that within HA last year (Calculate a device's theoretical power comsumption by the time it's on), I now made a simple Node Red flow which just outputs kWh from the time the device was on the last period. BUT I can’t add these numbers within Node Red (which would be even more elegant)…

Basically I want to simulate a Smart plug that counts kWhs by itself, like my trusty Xiaomi mi Smart Plug

Yes, buying additional smart plugs would be an option, but I don’t really see why, for devices that operate at the same wattage all the time (light bulbs, dehumidifiers, space heaters, fans, and so on)

Anyone got an Idea?

EDIT: It would be cool if Home Assistsant had this feature built in, so you can just activate theoretical power consumption for all switches :wink:

You have a power sensor. Why not just integrate that?

No the picture is just an example for what I want to do. Tracking the consumption from the Xiaomi switch works just as it should.

What I want to do is to simulate a power-meter for a switch, that does NOT have one built in. (in my case mostly Osram Smart+ Plugs, or Shelly 1)
The only thing I know about them is how long they are on - and I know how much Watt the devices connected to them use. That would be enough to calculate the power consumption … my space heater with 750W was on for 2 hours - that’s 1.5kWh power consumption. Simple… but how do I make a sensor that makes these calculations for me?!

The statistics sensor has a total attribute, but that’s probably not viable unless you only want the total for a day or so.

Try this:

sensor:
  - platform: template
    sensors:
      total_energy:
        friendly_name: "Total Energy"
        unit_of_measurement: 'kWh'
        entity_id: sensor.your_node_red_sensor_here
        value_template: "{{ states('sensor.total_energy')|float + states('sensor.your_node_red_sensor_here')|float }}"

My Node Red flow looks like this btw

the upper part is for when the switch was on and got tuned off (so it outputs the on time)
The lower part is for the off time (because amrt plugs also use energy when turned off :wink: )

The join node shows the rated power comsumption 300 and the time on in milliseconds - the function calculates kWh - this value I will send as an MQTT mesage to Home Assistant

(if you speak German, ignore the naming - it doesn’t make sense right now, because I just copied nodes)

I know nothing about node red, but that does not matter the template sensor should still add up the values.

hmmm - I’ll look into that, but I’m pretty sure I already tried it this way :thinking:

It doesn’t matter if it gets reset every day or so, because the Utility Meter only cares about positive changes - so if it gets set to 0 at midnight and starts from new, the Utility meter still shows correct values

Try the template sensor I wrote. It’ll use less resources.

1 Like

This didn’t work for some reason… the template sensor only showed the same thing my mqtt sensor showed. It didn’t att the values up :confused:

I managed to solve it within Node Red - the solution is very simple, I just had to send “0” after sending the last kWh, so the utility_meter always had a positive value to work with :wink:

I put the solution in my old thread, because some people might be interested in this too :wink: