Help with service_template and scripts

I’m trying to get this to work, and it never seems to execute any of the scripts even though the trigger (input_select.presence getting set to “Home”) is definitely happening.

- alias: Come home script
  trigger:
    - platform: state
      entity_id: input_select.presence
      to: 'Home'
  action:
    - service: script.home
    - service_template: >
        {% if is_state("input_select.mode", "Morning") %}
          script.morning
        {% elif is_state("input_select.mode", "Day") %}
          script.day
        {% elif is_state("input_select.mode", "Evening") %}
          script.evening
        {% elif is_state("input_select.mode", "Night") %}
          script.night
        {% else %}
        {% endif %}

I’ve looked through a number of examples of service_template and I can’t figure out what I’m doing wrong. I don’t see anything in home-assistant.log about this.

Try triggering the automation manually to see if the scripts run. If they do, then it’s the trigger. If not, then it’s the action. Also, are you able to execute the scripts manually (either by clicking on EXECUTE, or running them via the Services page)?

BTW, never have an empty {% else %}. There’s no point to your last {% elif ... %}. Just replace that with {% else %}.

1 Like

While looking through your automation I think your problems were with your actions. I have provided below a fixed solution that will hopefully fix your problem and get the result you were looking for.

- alias: Come home script
  trigger:
    - platform: state
      entity_id: input_select.presence
      to: 'Home'
  action:
    - service: script.turn_on
      data_template: 
        entity_id: >- 
          {% if is_state("input_select.mode", "Morning") %}
            script.morning
          {% elif is_state("input_select.mode", "Day") %}
            script.day
          {% elif is_state("input_select.mode", "Evening") %}
            script.evening
          {% elif is_state("input_select.mode", "Night") %}
            script.night
          {% endif %}

his actions are fine. You can call scripts directly instead of using script.turn_on. That’s what he’s doing. The issue has to lie within one of the following areas:

  • trigger isn’t actually working.
  • service_template input_select has the wrong entity id.
  • service_template input_select isn’t set to one of the following: Morning, Day, Evening, Night.

@rogersmj if you want to use a service template, follow @pnbruckner’s solution and make sure you have an else case. Even if you go with script.turn_on, you’ll need a script to execute during the ‘else case’.

You could use a condition to stop the script from executing if the selections are not in a desired list of selections.

Also, you can simplify your script because your unqiue selection names match your script names. So you can take your selection, make it lower case, and supply it as a script:

- alias: Come home script
  trigger:
    - platform: state
      entity_id: input_select.presence
      to: 'Home'
  action:
    - service: script.home
      # if the following condition is not met, scripts will not execute.  Eliminates the possibility of error.
    - condition: template
       value_template: >
         {{ states('input_select.mode') in ['Morning','Day','Evening','Night'] }}
    - service_template: >
        script.{{ state('input_select.mode') | lower }}
3 Likes

Thanks for the input everyone. Turns out…I had a really stupid logic error in some of those scripts this automation was calling (a condition) that was making it look like they didn’t run, even though they did. So this automation works correctly after all!

I’ve seen both > and >- used following *_template: before a multi-line template and wonder which is actually the correct usage, or are both correct? What’s the significance of the - if so?

https://yaml-multiline.info/

1 Like

Perfect! Thanks!

1 Like