Service template in automation

Hi!

i have this in an automation, and i have searched and tried alot of variations and non of them work. what am im doing wrong?

  - service_template: >
      {% if is_state('state.switch.mybraviatv', 'on') %}
        switch.turn_off
      {% endif %}
    entity_id: switch.mybraviatv

the complete automation, the script fires but non of the templates.

- id: '1522011348560'
  alias: When last person leave home
  trigger:
  - entity_id: group.home_booleans
    from: 'on'
    platform: state
    to: 'off'
  condition: []
  action:
  - data:
      entity_id: script.1545874111381
    service: script.turn_on
  - service_template: >
      {% if is_state('switch.coffiemachine', 'on') %}
        switch.turn_off
      {% endif %}
    entity_id: switch.coffiemachine
  - service_template: >
      {% if is_state('state.switch.mybraviatv', 'on') %}
        switch.turn_off
      {% endif %}
    entity_id: switch.mybraviatv

Unless I’m misunderstanding something, you don’t need a service template for this. You can just call switch.turn_off for any entities you want to turn off, or use entity_id: all if you want all of them turned off.

Example for single switch:

action:
- service: switch.turn_off
  entity_id: switch.coffiemachine

Example for all switches:

action:
- service: switch.turn_off
  entity_id: all

this is incorrect and should be

- service_template: > 
    {% if is_state('switch.mybraviatv', 'on') %} switch.turn_off 
    {% endif %}

you’d need an else clause though to be safe, in case the template doesnt evaluate to anything…

And yes, as @Tediore says, you can simply use the turn_off service, even when the entity would already be off, no harm done.
If you use a service_template always use If, else.

added to all that, I suspect your automation is made by the built-in editor? Id strongly suggest to write it like this, for readability’s sake:

- alias: When last person leave home
  id: '1522011348560'
  trigger:
    - platform: state
      entity_id: group.home_booleans
      from: 'on'
      to: 'off'
  condition: []
  action:
    - service: script.1545874111381 # <-- can call the script directly
    - service_template: >
        {% if is_state('switch.coffiemachine', 'on') %}  switch.turn_off
        {% endif %}
      entity_id: switch.coffiemachine
    - service_template: >
        {% if is_state('state.switch.mybraviatv', 'on') %}
          switch.turn_off
        {% endif %}
      entity_id: switch.mybraviatv

but, since you’d need an else clause for both service_templates, and there’s no builtin ‘do nothing’ service with that entity_id, you can’t use the service_template like this.

Creating 2 small scripts you call in the automation could solve that.

1 Like

I know i dont need to have one, its more for learning and trying to understand and take this further.

unfortuntaly nothing happend when i changed to this

  - service_template: >
      {% if is_state('switch.mybraviatv', 'on') %} switch.turn_off
      {% else %}
      {% endif %}
    entity_id: switch.mybraviatv

thats because you dont have a service after the else :wink: which is the point of my post above. to quickly test if its working add the same service switch.turn_off so in both cases it switches off

the added scripts I mention would use a do-nothing script, and a sub script with your current settings:

 - service_template: >
     script.{{'mybraviatv' if is_state('switch.braviatv', 'on')  else 'continue'}}
script:
  mybraviatv:
    alias: 'Mybraviatv'
    sequence:
      service: switch.turn_off
      entity_id: switch.mybraviatv

  continue:
    alias: 'Continue'
    sequence:
      delay: 00:00:00
  
1 Like

You can (should) also use the /dev-template page in the frontend to check the output of the template before actually deploying it.

the coffiemachine is off and there for broke the automaton. # marked that part and the TV shut of.

thank you!

BR Mathias

 - service_template: >
     script.{{mybraviatv if is_state('switch.braviatv', 'on')  else 'continue'}}

Curious as to why the continue is in quotes, but the mybraviatv is not. They both refer to scripts, right? Still learning templating…

that is a typo. sorry. should indeed be quoted. Ill edit the post above.

Ah, thanks. The inconsistency raised a flag. Cheers.