Poor Man's Energy Sensor (Calculated Energy Value)?

So I’ve done a bit of searching and scratching my head, but I’ve not been able to find out how to set up a pseudo-energy sensor in the form of a calculated value based on the known power value for a circuit and cumulative time the device is on.

This would be useful for the majority of my lights, for instance. I can easily calculate the power draw of the standard bulbs I use in my fixtures. All I need access to is the cumulative time that switch has been ON and time x power = energy. That way I could plug these calculated sensors into the HA energy dashboard and get energy stats. I know it won’t be perfect as each bulb has slightly different draw and the voltage fluctuates a bit from the power company, but it will give me a really good idea what an individual light circuit is costing me.

All my switches are ESPHome devices so I’d love to use ESPHome to do the calculation or I’m open to doing it in HA itself if necessary. What I’m not seeing in ESPHome nor HA the cumulative time a device is on. Anybody know where I can find that to use in a calculation?

Strange that you can’t find anything because it has been dealt with several times.

The basic scheme is this. Say you have a light that uses 25W. Set up a template sensor for the power consumed by the light. The template simply returns a state of 25 or zero depending on the light state. Now we have a power sensor.

Use the riemann integration integration to get energy.

Job done.

Alternatively you can add a “power” attribute to the light entity, then use the integration integration (thanks to @123 for this):

  - platform: template
    sensors:
      balights_power:
        friendly_name: Bathroom light
        unit_of_measurement: Watt
        value_template: >
          {{ expand('light.bathroom_1') | selectattr('state', 'eq', 'on') | map(attribute='attributes.power') | map('int') | sum }}
          
  - platform: integration
    source: sensor.balights_power
    name: balights_kWh
    unit_prefix: k
    round: 5
    method: left
    

Or if you don’t want to create a whole bunch of template sensors there is this custom integration:

1 Like

Ah, I didn’t think about doing it with the integral sensor. :man_facepalming:t2: That’s a great idea.

Here is what I’ve come up with just to complete the loop here. I created a template sensor for reporting the calculated power (watts) used when the light is on. Then I used the Total Daily Energy sensor to calculate the energy used (KWh, or in this case Wh).

To change the power draw with the switch toggling I added sensor.template.publish commands to my relay/switch on_turn_on/off sections. This allows the power draw to update immediately upon change.

I also have added an off power draw since the smart switch actually draws some power even when the light is off.

substitutions:
  power_on: "18" # watts
  power_off: "1" # watts

sensor:
  - platform: template
    name: "${friendly_name} Power"
    id: calculated_power
    unit_of_measurement: W

  - platform: total_daily_energy
    name: "${friendly_name} Total Daily Energy"
    power_id: calculated_power

switch:
  - platform: gpio
    name: ${friendly_name}
    icon: ${icon}
    pin: GPIO12
    id: relay
    on_turn_on:
      then:
        - output.turn_on: red_led
        - sensor.template.publish:
            id: calculated_power
            state: ${power_on}
    on_turn_off:
      then:
        - output.turn_off: red_led
        - sensor.template.publish:
            id: calculated_power
            state: ${power_off}
    

Thanks guys for pointing me in the right direction! :slightly_smiling_face: