Light.turn_on/_off with service_template

Hi!
I am new to HA and still struggling with the configuration. The following does not want to work. It also shows a “Unsupported action:” error in the GUI.

- id: '1570730536258'
  alias: Schuhzimmer an aus
  trigger:
    - platform: state
      entity_id: binary_sensor.tradfri_motion_sensor
      to: 'on'
    - platform: state
      entity_id: binary_sensor.tradfri_motion_sensor
      to: 'off'
      for:
        minutes: 10
  action:
    entity_id: switch.fibaro_system_fgs213_switch_switch_2
    service_template: >
      {% if trigger.to_state.state == 'on' %}
        light.turn_off
      {% elif trigger.to_state.state == 'off' %}
        light.turn_on
      {% else %}
      {% endif %}

Anybody has a clue what I am doing wrong?

Thanks!
M

first of all, you have no service in your else clause.
What do you want to do if the if, nor the else are evaluated to true?

your probably be better of using

    service_template: >
      light.turn_{{trigger.to_state.state}}
    entity_id: switch.fibaro_system_fgs213_switch_switch_2

must say I havent used the same trigger entity_id for 2 different states in 1 automation, so you’d have to see what happens

1 Like

Thanks for the quick response, but unfortunately that one does not work either.

of course, it should be switch.turn_off/on…

Change this:

      light.turn_{{trigger.to_state.state}}

to this:

      switch.turn_{{trigger.to_state.state}}

… ninja’d by Marius

hahaha yes we were heavily occupied with the exact same thought! cool. should have seen it in my earlier post, sorry for that.

Ha, what’s a domain between friends ? :rofl:

I have data_template instead of entity_id in my service_template.
Hope that works for you.

  - service_template: >
      light.turn_{{trigger.to_state.state}}
    data_template:
      entity_id: "light.basement_{{'corridor' if trigger.to_state.object_id[-1:] == 'stair' else 'stair'}}"

Your example needs to include data_template because the data supplied to the service includes templates (your example’s entity_id uses a template). In MatthiasR’s example, none of the data supplied to the service uses a template, so it doesn’t need data_template.

You are correct, thanks for pointing that out. So OP is missing the data:

  - service_template: >
      {% if is_state('light.dinner_table_light','on') %}
        automation.turn_off
      {% else %}
        automation.turn_on
      {% endif %}
    data:
      entity_id: automation.smart_kitchen_lights_off

Not sure what you are trying to do here.
Why do you want to turn on/off an automation ?

Mutt, that’s part of my automation with motion sensor, when the dinner_table_light is on, i.e. eating dinner, I don’t want the motion sensor to control kitchen lights at all.

No, in the OP’s example, data: is not required because the only data presented is entity_id (which is understood to be data for the service without explicitly supplying the option word data:).

That is poor coding, just put a condition in the other automation.
The danger being that you ‘may’ leave the automation in an unknown state should something unexpected happen (say a reboot)

1 Like

I know this is getting off topic, but thanks to 123 and Mutt, all true, always things to improve and correct.