Hi,
I’m a total newbie to this but I’m determined to get the hang of it. Please could somebody point me in the right direction as to why this automation rule I set up doesn’t work? I’m trying to get my living room lamp to stay on when my TV is turned on. My TV is plugged into a smart plug and my lamp uses a hue bulb called “lamp”. To achieve this my code is supposed to look at the current consumption of the plug and if it’s above a certain value then turn the lamp on. The state attributes of the plug are:
automation:
- alias: "Turn of 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 | float }}"
above: 30
action:
service: light.turn_on
entity_id: light.lamp
This is the error I’m getting:
Testing configuration at C:\Users\nutman\AppData\Roaming\.homeassistant
17-03-05 13:15:06 ERROR (MainThread) [homeassistant.bootstrap] Invalid config for [automation]: invalid template (TemplateSyntaxError: expected token 'end of print statement', got 'consumption') for dictionary value @ data['trigger'][0]['value_template']. Got None. (See C:\Users\nutman\AppData\Roaming\.homeassistant\configuration.yaml, line 64). Please check the docs at https://home-assistant.io/components/automation/
Failed config
I’m still a noob at this myself but I believe your states may be strings and not integers. If you plug your value_template into the template-dev tool (4th icon under dev tools in the left menu) what gets returned?
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?
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?
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 calledlight.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?
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
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
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.
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