Conditional trigger value in action usage?

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

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

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:/

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' ]  %}