Create a sensor based off power use (watts) that show TV is "on" or "off"

Hi HA team,

I have a TP link HS110 that can report energy use.

I would like to create a new sensor (TV Status) that if the watts is =< 2.0 watts, it will show as “off” and if > than 2.0 will show as “on”.

Here is the config I have that is not working based off: https://www.home-assistant.io/integrations/binary_sensor.template/

I was using the “Sensor Threshold” section.

      media_room_tv_status:
        friendly_name: "Media Room TV Status"
        device_class: power
        value_template: "{{ states('sensor.media_room_tv_watts')|float > 2.0 }}"

Anyone have any ideas to assist. Thank you very much.

Where is the platform: statement?

Sorry, i omitted.

I’ve got:

  - platform: template
    sensors:
       media_room_tv_status:
          friendly_name: "Media Room TV Status"
          device_class: power
          value_template: "{{ states('sensor.media_room_tv_watts')|float > 2.0 }}"

What does this mean?

Here is a similar example that does work:

#binary_sensor.yaml (depends on what you have setup might be configuration.yaml)
- platform: template
  sensors:
    dock_jellyfin_update:
      friendly_name: "Jellyfin Update Available"
      value_template: "{{ states('sensor.docker_image_jellyfin')|float > 1 }}"

Templating says you can use device class for binary, but for troubleshooting you might what to temporarily remove it.

Same use case over here, with working solution. Uses binary_sensor to indicate power on/off and and automation to signal:

configuration.yaml:

binary_sensor:
  - platform: template
    sensors:
      washing_machine:
        value_template: "{{ state_attr('switch.hs110_2','current_power_w') | float > 10.0 }}"

automations.yaml:

- id: 'washing_mashine_ready'
  alias: signal washing machine is ready
  trigger:
  - entity_id: binary_sensor.washing_machine
    for: 00:05:00
    from: 'on'
    platform: state
    to: 'off'
  action:
  - data:
      message: Washing mashine has finished. 
    service: notify.my_wife
1 Like

I’ve read here that it’s better to use state_attrib than states(). I can’t recall why but there is at least one thread here which goes into some detail. I only mention this because there are two working examples here. You may as well start with the “better” option. I had to go back and change a lot of my templates when states() caused some things to break for me.

Two different things, states() returns the current state of that entity which - for the TP-Link HS110 - is either on or off. To identify the state of the connected device (washing machine), I need to monitor the power consumption, which is reported as an attribute. The socket is still in state “on”, even if the washing maschine has come to rest.

Therefore state_attr() is required.

This has never been an issue. Your template must have been incorrect. Considering your misunderstanding of this:

It is the most likely scenario.

Read the warning from the documents:

Avoid using states.sensor.temperature.state , instead use states('sensor.temperature') . It is strongly advised to use the states() , is_state() , state_attr() and is_state_attr() as much as possible, to avoid errors and error message when the entity isn’t ready yet (e.g., during Home Assistant startup).

Templating - Home Assistant

Thank you.
So i have the following config in my config.yaml:

binary_sensor:
  - platform: template
    sensors:
      media_room_tv_status:
        friendly_name: "Media Room TV Status"
        value_template: "{{ state_attr('sensor.media_room_tv_watts','current_power_w') | float > 2.0 }}"

Which is giving me the sensor (yay):

image

But when I turn the TV on and it uses more that 2 watts, the sensor is not updating to ON. It does not automatically update either way.

Do i need to then do an automation to change the binary sensor state?

Apologies as this is my first template and new to me…

Thank you

could you print screen the info of your sensor under developer tools / states please? like this:

Hi @obaldius,

it is:

image

I mean from sensor.media_room_tv_WATTS

apologies:

image

binary_sensor:
  - platform: template
    sensors:
      media_room_tv_status:
        friendly_name: "Media Room TV Status"
        value_template: "{{ states('sensor.media_room_tv_watts')|float > 2.0 }}"

it just was an indentation problem. This should work

Ok thank you.
Will add and when home will try out and let you know.

Thank you again for helping!

np. by the way, may I ask, what TV do you have? can’t you integrate it?

I’ve got a Hisense TV its about 2 years old. can get the model when home for you.

This did the trick and now have the new binary sensor updating based off the watts.
Thank you @obaldius .

1 Like

BTW - hisense tvs have mqtt built it. Have a google!