Service_template with multiples action

Hello,
I have to know how to write a service template with multiple action:

 - service_template: > 
       {% if states(' input_boolean.home_presence_state') == 'on' %}
        - homeassistant.turn_on
          entity_id: switch.lampes_sous_sol
          
        - service: notify.email
          data:
          title: "Home assistant warning"
          message: "Allumage lampe sous-sol par camera et home presence"
          
        - delay: '00:10:00'
        
        - homeassistant.turn_off
          entity_id: switch.lampes_sous_sol
          
        - service: notify.email
          data:
          title: "Home assistant warning"
          message: "Extinction lampes sous-sol après 10 minutes"            
        
       {% else %}
       {% endif %}

Another question:
if I have another service template (as action) when the second service_template is executed ?
Is it after the end of the first service template )
Is it in another thread ie in the same time as the first service_template ?

Many thanks for your help writing automation !
Regards

That looks like you’d be better off with choose

You can’t write a service_template like that so that fact invalidates your other two questions. A service_template can call a service and cannot contain other YAML code like service or delay.

Unless you are using an old version of Home Assistant, you can now use the term service with a template and it’s no longer necessary to use the term service_template.

As suggested by Tinkerer, what you want to do is achieved with choose (see: Choose a group of actions).

For example, this is the equivalent of an “if-else” using choose

action:
  - choose:
      - conditions: "{{ is_state(' input_boolean.home_presence_state', 'on') }}"
        sequence:
          - service: homeassistant.turn_on
            entity_id: switch.lampes_sous_sol
          - service: notify.email
            data:
              title: "Home assistant warning"
              message: "Allumage lampe sous-sol par camera et home presence"
          - delay: '00:10:00'
          - service: homeassistant.turn_off
            entity_id: switch.lampes_sous_sol
          - service: notify.email
            data:
              title: "Home assistant warning"
              message: "Extinction lampes sous-sol après 10 minutes"            
        default:
          - service: homeassistant.turn_off
            entity_id: switch.lampes_sous_sol
2 Likes

Many thanks.
It work like a charme…
Many thanks for your help.
Yaml is not so easy…
Thanks to the home assistant comunity !
Regards
Thierry

1 Like