LinuxLover9
(Linux Lover9)
September 10, 2017, 7:08am
1
I would like to use the MQTT trigger payload in the action section.
I’m trying the following, but it’s giving me an error.
- id: Light_Driveway
alias: Driveway light
trigger:
platform: mqtt
topic: light/driveway/set
action:
service: template
value_template: "{% if ( {{ trigger.payload }}=='1' ) %}on{% else %}off{% endif %}"
turn_on:
service: light.turn_on
entity_id: light.driveway_light_4
turn_off:
service: light.turn_off
entity_id: light.driveway_light_4
LinuxLover9
(Linux Lover9)
September 11, 2017, 2:30am
2
OK, have changed my approach:
- id: Light_Driveway
alias: Driveway light
trigger:
platform: mqtt
topic: light/driveway/set
action:
service: light.turn_on
entity_id: light.driveway_light_4
data_template:
brightness_pct: "{% if ( {{ trigger.payload }} == '1' ) %}50{% else %}0{% endif %}"
The Idea is that even though the event is to turn on, a brightness level of 0, would turn the light off.
However I’m getting the error:
2017-09-10 22:18:20 ERROR (MainThread) [homeassistant.config] Invalid config for [automation]: invalid template (TemplateSyntaxError: expected token ':', got '}') for dictionary value @ data['action'][0]['data_template']['brightness_pct']. Got None. (See /home/butler/.homeassistant/configuration.yaml, line 148). Please check the docs at https://home-assistant.io/components/automation/
No clue where it gets the ‘}’ from?
Need an extra pair of eyes to find the fault in my logic…
1 Like
B1tMaster
(Dmitry)
September 28, 2017, 12:12pm
3
got the same problem today… same exact error and I am doing very similar thing as well.
Did you solve the issue?
Code:
follow_me_lights_on:
alias: "Follow Me Lights script"
sequence:
- service: homeassistant.turn_on
data_template:
entity_id: >
{{ light_id }}
- service: homeassistant.turn_off
data_template:
entity_id: >
{% set light_remove = [ {{ light_id }} ] %}
{% set all_lights = [ 'light.living_room_ceiling_light_1_level', 'light.living_room_ceiling_light_2_level' , 'switch.bedroom_ceiling_light_switch' , 'switch.family_room_ceiling_light_switch', 'switch.office_lights_switch_2' ] %}
{% for light in all_lights if (light not in light_remove) %}
{{ light }} {{ "," }}
{% endfor %}
Error:
Sep 28 19:06:45 HomeControl hass[11055]: 2017-09-28 19:06:45 ERROR (MainThread) [homeassistant.config] Invalid config for [script]: invalid template (TemplateSyntaxError: expected token ':', got '}') for dictionary value @ data['script']['follow_me_lights_on']['sequence'][1]['data_template']['entity_id']. Got '\n{% set light_remove = [ {{ light_id }} ] %} {% set all_lights = [ \'light.living_room_ceiling_light_1_level\', \'light.living_room_ceiling_light_2_level\' , \'switch.bedroom_ceiling_light_switch\' , \'switch.family_room_ceiling_light_switch\', \'switch.office_lights_switch_2\' ] %} {% for light in all_lights if (light not in light_remove) %} {{ light }} {{ "," }} {% endfor %}\n'. (See /home/homeassistant/.homeassistant/configuration.yaml, line 158). Please check the docs at https:/
B1tMaster
(Dmitry)
September 28, 2017, 12:21pm
4
you may need to move your automation to the next line and indent it… Also remove the “” from around it.
and you are missing > char. Ie: and last try change {% to {%-
try it:
brightness_pct: >
{%- if ( {{ trigger.payload }} == '1' ) %}50{% else %}0{% endif -%}
But why I am getting the problem … hmm…
This is how I got it to work:
- id: Light_Driveway
alias: Driveway light
trigger:
platform: mqtt
topic: light/driveway/set
action:
service: light.turn_on
entity_id: light.driveway_light_4
data_template:
brightness_pct: >
{% if( trigger.payload|int > 1) and ( states.light.driveway_light_4.state == 'off') %}
50
{% else %}
{% if trigger.payload|int < 100 %}
{{ trigger.payload|int }}
{% else %}
100
{% endif %}
{% endif %}
In your example, maybe this works?
{% set light_remove = [ light_id ] %}
or
{% set light_remove = [ 'light_id' ] %}