Power monitoring only when light is on

Hi, I have this configuration for Athom 15W Bulb which doesn’t have power consumptions and I created a fake template sensor. All ok but even if the light is off, will report same wattage.
Where can I add some “if” statement to report 0 watt consumtion when turned off?
Thanks

 substitutions:
  devicename: "livingroomlamp"
  friendly_name: "Livingroom Lamp"
  project_name: "athom.rgbct-light"
  project_version: "1.0"
  light_restore_mode: RESTORE_DEFAULT_ON
  color_interlock: 'true'
  power_consumption: "8"

esphome:
  name: $devicename
  name_add_mac_suffix: false
  project:
    name: "${project_name}"
    version: "${project_version}"

esp8266:
  board: esp8285
  restore_from_flash: true

api:

ota:

logger:

web_server:
  port: 80
  auth:
    username: admin
    password: !secret web_server_password

# WiFi connection
wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  fast_connect: on

  manual_ip:
    static_ip: 192.168.5.169
    gateway: 192.168.5.1
    subnet: 255.255.255.0
    
  ap:
    ssid: "${friendly_name} Hotspot"
    password: !secret hotspot_password
    ap_timeout: 5min
captive_portal:

time:
  - platform: homeassistant
    id: homeassistant_time

dashboard_import:
  package_import_url: github://athom-tech/athom-configs/athom-rgbct-light.yaml

binary_sensor:
  - platform: status
    name: "${friendly_name} Status"

sensor:
  - platform: total_daily_energy
    name: "${friendly_name} Todays Usage"
    accuracy_decimals: 3
    power_id: power_consumption_watt
    filters:
      # Multiplication factor from W to kW is 0.001
      - multiply: 0.001
    unit_of_measurement: kWh
    icon: mdi:calendar-clock
    
  - platform: template
    name: "${friendly_name} Power Usage"
    id: power_consumption_watt
    device_class: power
    state_class: measurement
    unit_of_measurement: W
    accuracy_decimals: 0
    filters:
      - heartbeat: 30s
  - platform: uptime
    name: "${friendly_name} Uptime Sensor"
    id: uptime_sensor
    update_interval: 60s
    on_raw_value:
      then:
        - text_sensor.template.publish:
            id: uptime_human
            state: !lambda |-
              int seconds = round(id(uptime_sensor).raw_state);
              int days = seconds / (24 * 3600);
              seconds = seconds % (24 * 3600);
              int hours = seconds / 3600;
              seconds = seconds % 3600;
              int minutes = seconds /  60;
              seconds = seconds % 60;
              return (
                (days ? to_string(days) + "d " : "") +
                (hours ? to_string(hours) + "h " : "") +
                (minutes ? to_string(minutes) + "m " : "") +
                (to_string(seconds) + "s")
              ).c_str();
              
  - platform: wifi_signal
    name: "${friendly_name} WiFi Signal"
    update_interval: 60s
    
button:
  - platform: restart
    id: restart_button
    name: "${friendly_name} Restart"

output:
  - platform: esp8266_pwm
    id: red_output
    pin: GPIO4
  - platform: esp8266_pwm
    id: green_output
    pin: GPIO12
  - platform: esp8266_pwm
    id: blue_output
    pin: GPIO14
  - platform: esp8266_pwm
    id: white_output
    pin: GPIO5
  - platform: esp8266_pwm
    id: ct_output
    inverted: true
    pin: GPIO13

light:
  - platform: rgbct
    name: "${friendly_name}"
    restore_mode: ${light_restore_mode}
    red: red_output
    green: green_output
    blue: blue_output
    white_brightness: white_output
    color_temperature: ct_output
    cold_white_color_temperature: 153 mireds
    warm_white_color_temperature: 500 mireds
    color_interlock: ${color_interlock}
    default_transition_length: 50ms

text_sensor:
  - platform: wifi_info
    ip_address:
      name: "${friendly_name} IP Address"
      disabled_by_default: false
  - platform: template
    name: "${friendly_name} Uptime"
    id: uptime_human
    icon: mdi:clock-start
  - platform: version
    name: "${friendly_name} ESPHome Version"
    
interval:
  - interval: 1min
    then:
      - sensor.template.publish:
          id: power_consumption_watt
          state: $power_consumption

You could just use a on_turn_on on on_turn_off on your light to have it published the power to the template sensor and get rid of some code you have already (like interval).

Or you could use a custom component from @nuttytree which actually just exactly covers your use case :point_down:

1 Like

You could do it with this, works great for devices with no power sensors

1 Like

Or use the template sensor with a lambda in Esphome, using the state of the light. On =15W off=0W

Ok, but where should I use a on_turn_on on on_turn_off on your light to have it published the power to the template sensor?

light.on_turn_on / light.on_turn_off Trigger :point_down:

1 Like

Not sure why you’re using MQTT when you are using the api?

This will generate a sensor in HA that is 15W when on and 0W when off and a daily energy usage sensor that update every 60s.

Just need the addition of an id in the light section and a lambda in the template sensor.
No need for the interval: section

Issue is that if you dim it and it is using less than 15W it will still say 15W, using the custom component above will approximate the correct power usage for different dim levels using Linear mode:


 substitutions:
  devicename: "livingroomlamp"
  friendly_name: "Livingroom Lamp"
  project_name: "athom.rgbct-light"
  project_version: "1.0"
  light_restore_mode: RESTORE_DEFAULT_ON
  color_interlock: 'true'
  power_consumption: "8"

esphome:
  name: $devicename
  name_add_mac_suffix: false
  project:
    name: "${project_name}"
    version: "${project_version}"

esp8266:
  board: esp8285
  restore_from_flash: true

api:

ota:

logger:

web_server:
  port: 80
  auth:
    username: admin
    password: !secret web_server_password

# WiFi connection
wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  fast_connect: on

  manual_ip:
    static_ip: 192.168.5.169
    gateway: 192.168.5.1
    subnet: 255.255.255.0
    
  ap:
    ssid: "${friendly_name} Hotspot"
    password: !secret hotspot_password
    ap_timeout: 5min

captive_portal:

time:
  - platform: homeassistant
    id: homeassistant_time

dashboard_import:
  package_import_url: github://athom-tech/athom-configs/athom-rgbct-light.yaml

binary_sensor:
  - platform: status
    name: "${friendly_name} Status"

sensor:
  - platform: total_daily_energy
    name: "${friendly_name} Todays Usage"
    accuracy_decimals: 3
    power_id: power_consumption_watt
    filters:
      # Multiplication factor from W to kW is 0.001
      - multiply: 0.001
    unit_of_measurement: kWh
    icon: mdi:calendar-clock

  - platform: template
    name: "${friendly_name} Power Usage"
    id: power_consumption_watt
    device_class: power
    state_class: measurement
    unit_of_measurement: W
    accuracy_decimals: 0
    lambda: |-
      if (id(light).current_values.is_on()) {
        return 15;
      } else {
        return 0;
      }
    update_interval: 60s

  - platform: uptime
    name: "${friendly_name} Uptime Sensor"
    id: uptime_sensor
    update_interval: 60s
    on_raw_value:
      then:
        - text_sensor.template.publish:
            id: uptime_human
            state: !lambda |-
              int seconds = round(id(uptime_sensor).raw_state);
              int days = seconds / (24 * 3600);
              seconds = seconds % (24 * 3600);
              int hours = seconds / 3600;
              seconds = seconds % 3600;
              int minutes = seconds /  60;
              seconds = seconds % 60;
              return (
                (days ? to_string(days) + "d " : "") +
                (hours ? to_string(hours) + "h " : "") +
                (minutes ? to_string(minutes) + "m " : "") +
                (to_string(seconds) + "s")
              ).c_str();
              
  - platform: wifi_signal
    name: "${friendly_name} WiFi Signal"
    update_interval: 60s
    
button:
  - platform: restart
    id: restart_button
    name: "${friendly_name} Restart"

output:
  - platform: esp8266_pwm
    id: red_output
    pin: GPIO4
  - platform: esp8266_pwm
    id: green_output
    pin: GPIO12
  - platform: esp8266_pwm
    id: blue_output
    pin: GPIO14
  - platform: esp8266_pwm
    id: white_output
    pin: GPIO5
  - platform: esp8266_pwm
    id: ct_output
    inverted: true
    pin: GPIO13

light:
  - platform: rgbct
    name: "${friendly_name}"
    id: light
    restore_mode: ${light_restore_mode}
    red: red_output
    green: green_output
    blue: blue_output
    white_brightness: white_output
    color_temperature: ct_output
    cold_white_color_temperature: 153 mireds
    warm_white_color_temperature: 500 mireds
    color_interlock: ${color_interlock}
    default_transition_length: 50ms

text_sensor:
  - platform: wifi_info
    ip_address:
      name: "${friendly_name} IP Address"
      disabled_by_default: false
  - platform: template
    name: "${friendly_name} Uptime"
    id: uptime_human
    icon: mdi:clock-start
  - platform: version
    name: "${friendly_name} ESPHome Version"

Thank you all for the ideas, with my little knowlege I put some pieces side by side and created a sensor with this integration ESPHome-Devices/components/gpio_switch_with_power at df2c532d13f1f4ba2a4d9ad7b390999fb124c41c · nuttytree/ESPHome-Devices · GitHub

substitutions:
  devicename: "livingroomlamp"
  friendly_name: "Livingroom Lamp"
  project_name: "athom.rgbct-light"
  project_version: "1.0"
  light_restore_mode: RESTORE_DEFAULT_ON
  color_interlock: 'true'

esphome:
  name: $devicename
  name_add_mac_suffix: false
  project:
    name: "${project_name}"
    version: "${project_version}"

esp8266:
  board: esp8285
  restore_from_flash: true

api:

ota:

logger:

web_server:
  port: 80
  auth:
    username: !secret web_server_username
    password: !secret web_server_password

# WiFi connection
wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  fast_connect: on

  manual_ip:
    static_ip: 192.168.5.169
    gateway: 192.168.5.1
    subnet: 255.255.255.0
    
  ap:
    ssid: "${friendly_name} Hotspot"
    password: !secret hotspot_password
    ap_timeout: 5min
captive_portal:

time:
  - platform: homeassistant
    id: homeassistant_time

dashboard_import:
  package_import_url: github://athom-tech/athom-configs/athom-rgbct-light.yaml

binary_sensor:
  - platform: status
    name: "${friendly_name} Status"

sensor:
  - platform: total_daily_energy
    name: "${friendly_name} Todays Energy"
    power_id: "${devicename}_watt"
    filters:
      # Multiplication factor from W to kW is 0.001
      - multiply: 0.001
    unit_of_measurement: kWh
    icon: mdi:calendar-clock
  - platform: uptime
    name: "${friendly_name} Uptime Sensor"
    id: uptime_sensor
    update_interval: 60s
    on_raw_value:
      then:
        - text_sensor.template.publish:
            id: uptime_human
            state: !lambda |-
              int seconds = round(id(uptime_sensor).raw_state);
              int days = seconds / (24 * 3600);
              seconds = seconds % (24 * 3600);
              int hours = seconds / 3600;
              seconds = seconds % 3600;
              int minutes = seconds /  60;
              seconds = seconds % 60;
              return (
                (days ? to_string(days) + "d " : "") +
                (hours ? to_string(hours) + "h " : "") +
                (minutes ? to_string(minutes) + "m " : "") +
                (to_string(seconds) + "s")
              ).c_str();
              
  - platform: wifi_signal
    name: "${friendly_name} WiFi Signal"
    update_interval: 60s
    
button:
  - platform: restart
    id: restart_button
    name: "${friendly_name} Restart"

output:
  - platform: esp8266_pwm
    id: red_output
    pin: GPIO4
  - platform: esp8266_pwm
    id: green_output
    pin: GPIO12
  - platform: esp8266_pwm
    id: blue_output
    pin: GPIO14
  - platform: esp8266_pwm
    id: white_output
    pin: GPIO5
  - platform: esp8266_pwm
    id: ct_output
    inverted: true
    pin: GPIO13

light:
  - platform: rgbct
    name: "${friendly_name}"
    restore_mode: ${light_restore_mode}
    red: red_output
    green: green_output
    blue: blue_output
    white_brightness: white_output
    color_temperature: ct_output
    cold_white_color_temperature: 153 mireds
    warm_white_color_temperature: 500 mireds
    color_interlock: ${color_interlock}
    default_transition_length: 50ms
    on_turn_on:
      - switch.turn_on: "${devicename}_switch"
    on_turn_off:
      - switch.turn_off: "${devicename}_switch"   

text_sensor:
  - platform: wifi_info
    ip_address:
      name: "${friendly_name} IP Address"
      disabled_by_default: false
  - platform: template
    name: "${friendly_name} Uptime"
    id: uptime_human
    icon: mdi:clock-start
  - platform: version
    name: "${friendly_name} ESPHome Version"
    
external_components:
  - source: github://nuttytree/ESPHome-Devices
    components: [ gpio_switch_with_power ]
    
switch:
  - platform: gpio_switch_with_power
    id: "${devicename}_switch"
    name: "${friendly_name} Switch"
    pin: 4
    power:
      id: "${devicename}_watt"
      name: "${friendly_name} Watt"
      device_wattage: 8.0
      update_interval: 60s
    disabled_by_default: true