Using static power value/sensor for non-metered devices running ESPHome

Hi,

I do play around with power metering and the new energy dashboard using ESPHome Devices. Some of my devices do have a metering unit integrated which report the “power” needed to populate data with the “Total Daily Energy Sensor

No I have some ESPHome devices which do not have any metering option, but I know the sources connected to it. Those are mainly light bulbs which have a fixed power usage which I could simply measure.
I read about people using HA to get the data calculated, but I would rather let ESPHome Devices report it directly to HA - which I find is more useful (perhaps also only in my scenario).

Can somebody help me in getting this working, some links would already be good.

What I found, but I am struggling to understand how and if I can convert this to ESPHome:

Also I am wondering if I really need Integration - Riemann sum integral - Home Assistant because it seems like the Energy Dashboard is doing this if the correct sensor “Total Daily Energy Sensor” is used with ESPHome Devices.

Thanks

I know you want to implement something within ESPHome itself, but take a look at this HACS repository, I use it almost exclusivley for all my sensors on the network as its extremely easy to use and does everthing for you.
Its called Power Calc https://github.com/bramstroker/homeassistant-powercalc

Looks great, little too much for what I am looking for, but I take it into consideration :slight_smile:
Anyhow, as you have stated, want to get this into ESPHome directly would be my preferred route.

In esphome create a template sensor with device_class: power. The template has two stares if on it is the known power, if off the value is zero.

Then configure the Total Daily Energy sensor with the template sensor as the power source.

thanks that helped me, even though I have some questions :slight_smile: but first what I have used to accomplish a power and energy information for devices without an energy sensor

switch:
  - platform: gpio
    name: "Relay"
    pin: GPIO12
    id: relay

sensor:
  - platform: template
    id: power
    name: "Power Sensor"
    device_class: power
    state_class: measurement
    unit_of_measurement: W
    accuracy_decimals: 0
    lambda: |-
      if (id(relay).state) {
        return 42.0;
      } else {
        return 0.0;
      }
    update_interval: 60s

  - platform: total_daily_energy
    name: "Total Daily Energy"
    power_id: power

It seems to work, but the lambda feature is a little mystery for me, or especially the if statement :slight_smile: the id part I get, referencing to the relay but the state behavior is unclear if it is true or false.
I tried to use things like on_turn_on or turn_on instead because I thought this would be the right one to get the this validated but I was too stupid.
Anyhow it seems to work but I cannot find information when state is true or false perhaps someone can help me and / or make the yaml even “better” :smiley:

Thanks

Hi.

The state of the relay is declared as a bool in the source code for a switch. So it considered as a c++ boolean. So that actually means that it ‘0’ when false and something else when it true.
So esphome is working by generating c++ code that is compiled. So if you want answerers you can checkout the source code.
Here is the state for the switch:

I hope it helps you with your understanding .
/Mattias

Actually it does :smiley:
THANKS :slight_smile: