Calculate realtime kWh (units of electricity consumption) from Power (W) sensor

it looks OK, wonder if some of those ct sensors goes offline and produces the NaN …

Hi, I still can’t get this working, I keep checking the longs NaN ver shows up in the logs on ESPHome and when I check the watts value it is always returning a float, so I am out of ideas why the integration sensor is returning NaN.

Any thing you can help me with? Thanks in advance.

Few things to try:

  • Integrate each sensor separately, see if that works.
  • I don’t know if the integration sensor has to be running for at least one unit_time. But if it does you can try to add this to see if you get some values right away:
    unit_time: s

Thanks for responding much appreciated. When you say integrate each one separately can you explain what that means? I don’t quite follow sorry.

E.g. start with only energy_ct1_watts

Just a general strategy to debug; Start with the simplest stuff you can get working, add from there.
E.g.

  - platform: integration
    name: energy_ct1_watts_integral
    source: sensor.energy_ct1_watts
    method: trapezoidal
    unit_time: s
    round: 2

1 Like

Hi Diogo,

      yesterday_server_room_kwh:
        friendly_name: "Server Room Yesterday"
        unit_of_measurement: 'kwh'
        value_template: "{{ state_attr('sensor.energy_serverroom_daily', 'last_period') }}" 

Does that code looks correct? The sensor in the value template is from the utility meter sensor. It always just returns 0, I know the daily is working as it is registering data,
Thanks,
Dave

Looks absolutely fine to me (actually tried it out in /developer-tools/template , replacing your sensor name with one of mine), all working.

Same here. This returns a value:

value_template: "{{ states.sensor.monthly_energy.state|float}}"

This returns zero:

value_template: "{{ state_attr('sensor.monthly_energy', 'last_period')}}"
1 Like

Do you have a value in the attribute last_period?

Hi all

Can someone please tell me what is wrong in this??

#kwhsensor
sensor:
 - platform: statistics
   name: power_per_minute
   entity_id: sensor.w_watts
   sample_size: 60

 - platform: template
   sensors:
   realtime_power:
   friendly_name: Realtime Power
   value_template: "{{ state_attr('sensor.w_watts', 'total') / (1000 * 60) }}"
   unit_of_measure: kwH

Thanks all

The indentation and it’s unit_of_measurement not unit_of_measure and xou should use the statistics sensor you created not the original sensor

 - platform: template
   sensors:
     realtime_power:
       friendly_name: Realtime Power
       value_template: "{{ state_attr('sensor.power_per_minute', 'total') / (1000 * 60) }}"
       unit_of_measurement: kwH

Also does your original sensor provide a new value every second?

1 Like

Hi @Burningstone

I saw the error you said about the unit but it keeps getting the error, now i have this :

#kwhsensor
sensor:
 - platform: statistics
   name: power_per_minute
   entity_id: sensor.w_watts
   sample_size: 60

 - platform: template
   sensors:
     realtime_power:
       friendly_name: Realtime Power
       value_template: "{{ state_attr('sensor.power_per_minute', 'total') / (1000 * 60) }}"
       unit_of_measurement: kwH

and the error is :
duplicated mapping key at line 128, column -339:
sensor:
^
Yes, i already checked the sensor, is getting information by second.

That’s a good point. The monthly sensor has no data in “last_period”, so i used the hourly sensor and it works now. :slight_smile:
But i’m not sure which method is more accurate. “last_period” or just the float value.

Then you have two times sensor: in your condiguration.yaml, you can only have one sensor: section and all sensors need to be in the same section.

1 Like

@Burningstone

Thanks so much for the help. Worked.

tha strange thing now is that the statistics are getting value and i dont know why

imagem

the sensor from that statistics have the value 0.

imagem

Did you check the history of sensor.w_watts?

I have history on that sensor but it´s supossed to pick that history?

forget this answer its obvious.

thanks for the help

Ehm yes, that’s exactly what the statistics sensor does.
No offence, but do you even understand what you are doing here or just blindly copy code?