Turn on lamp when TV is on

Hey Mike thanks for the response. I get the following when I input my value_template into the template-dev tool:

Error rendering template: TemplateSyntaxError: expected token 'end of print statement', got 'consumption'

Looking at it I believe this is because I have a space between: states.switch.living_room_tv.attributes.current consumption

However, on the state attribute for the plug the current consumption has a space. Is there a value I can use for a space to get around this?

Just out of curiosity what happens if you plug your template into the dev tool and use voltage instead of current consumption? Does it return the correct value?

You may need to get some quotes in there to avoid the break between current and consumption, but I’m not sure of what the syntax would be for that.

OK I got my config to validate by using the following for the space:

value_template: "{{ states.switch.living_room_tv.attributes.current" + ' ' + "consumption | float }}"

However, turning the TV on didn’t turn the lamp on. Curiously if I put that line of code in the template dev-tool then I get an error:

Error rendering template: TemplateSyntaxError: expected token 'end of print statement', got 'string'

If I change the current consumption attribute to voltage instead, it just returns: value_template: ""

Bit confused now

did you try current_consumption (with underscore), because that’s the way how HA “normalizes” strings to identifier?

1 Like

Try '{{ states.switch.living_room_tv.attributes["Current consumption"] | replace(" W", "") | float }}'

Found here: TPLINK HS110 switch, use energy consumption as sensor

2 Likes

Thanks again Mike. The value template now returns the actual value of the “current consumption”. However, the lamp still doesn’t turn on when the TV is on (and the current consumption goes above 30). Any ideas?

Hey Dan,

Could you show me the part of the log where the automation kicks in turning light.lamp on?
I wonder what it says. Avoids me asking the obvious questions like “are you sure the lamp is called light.lamp?” or "have you tried turning the lamp on and off first to make sure it is in an OFF state?

Which light platform are you using? Actual Philips Hue or some other RGBW lamp?

Have you tried this?

  action:
    service: homeassistant.turn_on
    entity_id: light.lamp

Hi Dennis,
Looking at the log nothing appears to happen when I switch the TV on. If I look at the state of the automation on the HA dash it says it’s never been triggered. I’m using the actual Philips Hue bulbs and I’m certain the it’s called lamp. The state of the lamp is indeed “off” when I check on the dev tools “states” section.

By the looks of it the trigger isn’t firing, which I find odd because the value of “current consumption” goes above 30 when I turn the TV on. I tried the action you suggested but it made no difference. Here’s the automation code again:

automation:
  - alias: "Turn on lamp when TV is on"
    trigger:
      platform: numeric_state
      entity_id: switch.living_room_tv
      value_template: "{{ states.switch.living_room_tv.attributes['Current consumption'] | replace(' W', '') | float }}"
      above: 30
    action:
      service: homeassistant.turn_on
      entity_id: light.lamp

Anything in home-assistant.log?

Can you see what happens if you do this

automation:
  - alias: "Turn on lamp when TV is on"
    trigger:
      platform: numeric_state
      entity_id: switch.living_room_tv
      value_template: '{{ states.switch.living_room_tv.attributes['Current consumption'] | replace(' W', '') | float }}'
      above: 30
    action:
      service: homeassistant.turn_on
      entity_id: light.lamp

When you go to dev-template and paste following line in template section you see the correct value?

'{{ states.switch.living_room_tv.attributes['Current consumption'] | replace(' W', '') | float }}

That’s what I had originally but it didn’t work as you need to use different quotes at the start/end of the value_template statement and between the ‘current consumption’ and replace ‘w’ sections, otherwise you get a expected block end but found ’ scalar ’ error.

Anyway, if I input the the value_template into the dev-template I get the correct value. Nothing happens on the log when I turn the TV on and the current consumption value goes from around 20 to over 50. The change in value is noted in the log:

17-03-06 11:49:06 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=switch.living_room_tv, old_state=<state switch.living_room_tv=on; Current consumption=17.7 W, Total consumption=0.41 kW, Voltage=247.34 V, Current=0.1 A, Daily consumption=0.21 kW, friendly_name=Living Room TV @ 2017-03-06T10:59:36.886453+00:00>, new_state=<state switch.living_room_tv=on; Current consumption=57.6 W, Total consumption=0.41 kW, Voltage=247.10 V, Current=0.4 A, Daily consumption=0.21 kW, friendly_name=Living Room TV @ 2017-03-06T10:59:36.886453+00:00>>

So I have no idea why the automation isn’t being triggered

Just to be sure. You want the light to be ON or OFF when TV is ON?

Your Automation title says “Turn of lamp when TV is on”

How about this

- platform: template
  sensors:
    living_tv:
     value_template: "{%- if states.switch.living_room_tv.attributes['Current consumption'] | replace(' W', '') | float | float < 20 %}on{% else %}off{%- endif %}"

Take/Copy the state value from sensor.light_tv which is off or on in your automation and your good.

Thanks for continued assistance on this Dennis.

That was a typo I want the lamp to turn ON when the TV turns on.
Apologies but I’m not sure where I should be placing that code you’ve suggested?

Plug that into your config file. Then your automation would look something like this:

automation:
  - alias: "Turn on lamp when TV is on"
    trigger:
      platform: state
      entity_id: sensor.living_tv
      to: 'on'
    action:
      service: homeassistant.turn_on
      entity_id: light.lamp

I think this float < 20 %}on{% else %}off{%- endif %}
should be float > 20 %}on{% else %}off{%- endif %}
You want things “on” if the value of your sensor is greater than 20.

Your correct. Was a typo there.

Thank you both. I have set this up now. The template dev-tool returns the correct value when I run the value_template through it (‘on’ when TV is on and vice versa) but it still doesn’t trigger! If I manually hit the “trigger” button on the automation panel on the HA dash then the lamp switches on so the action must be fine. I have no idea why the trigger isn’t working though. This is what I got:

sensor:
  - platform: template
    sensors:
      living_tv:
        value_template: "{%- if states.switch.living_room_tv.attributes['Current consumption'] | replace(' W', '') | float | float > 20 %}on{% else %}off{%- endif %}"

automation:
  - alias: "Turn on lamp when TV is on"
    trigger:
      platform: state
      entity_id: sensor.living_tv
      to: 'on'
    action:
      service: light.turn_on
      entity_id: light.lamp

Could you chance

to this

      service: homeassistant.turn_on
      entity_id: light.lamp

and see if this works?

I am still banking on you having the correct entity_id of the lamp in here.

When your TV is off what does sensor.living_tv show as it’s state in the states-dev tool? Should be ‘off’. Now while watching that sensor, turn your TV on do you see its state change to ‘on’?