Is this a bug or am I missing something? [SOLVED]

An automation failed this morning and when I checked the trace, I see why. The action for the door closing is not coded correctly. See below. Running 2022.3.5 on Intel NUC.

alias: Turn on radio when alarm is armed
id: 253c6df2-5405-421a-b761-3645d300b326
initial_state: true
trigger:
  platform: state
  entity_id: alarm_control_panel.home_alarm
  to: armed_away
action:
  - service: script.turn_on
    entity_id: script.radio_alarm
  - service_template: >-
      {% if is_state('light.garage_light_main' , 'on') %} light.turn_off {%
      endif %}
    entity_id: light.garage_light_main
  - service_template: '{% if is_state(''cover.garage'' , ''open'') %} cover.close_cover {% endif %}'
    entity_id: cover.garage

image

Have a look though at the actual automation. Why is there a difference?

## Turn on radio in rumpus and ensuite when alarm sets ##
  - alias: 'Turn on radio when alarm is armed'
    id: 253c6df2-5405-421a-b761-3645d300b326
    initial_state: true
    trigger:
        platform: state
        entity_id: alarm_control_panel.home_alarm
        to: 'armed_away'
## script in scripts folder [radio.yaml] ##
    action:
      - service: script.turn_on
        entity_id: script.radio_alarm
      - service_template: "{% if is_state('light.garage_light_main' , 'on')
         %} light.turn_off
         {% endif %}"
        entity_id: light.garage_light_main
      - service_template: "{% if is_state('cover.garage' , 'open')
         %} cover.close_cover
         {% endif %}"
        entity_id: cover.garage

Both of your templates are missing an else case.

e.g. If the garage light is not on, there is no service available for the service_template and an error occurs and the actions stop. Likewise if the cover is not open.

Also you no longer need to use service_template just service will do.

If you do not have an else case, use the choose action instead of a template.

    action:
      - service: script.turn_on
        entity_id: script.radio_alarm
      - choose:
          - conditions:
              - condition: state
                entity_id: light.garage_light_main
                state: 'on'
            sequence:
              - service: light.turn_off
                target:
                  entity_id: light.garage_light_main
      - choose:
          - conditions:
              - condition: state
                entity_id: cover.garage
                state: 'open'
            sequence:
              - service: cover.close_cover
                target:
                  entity_id: cover.garage
1 Like

Thank you. I was just reading about the deprecated service_template. It so hard to keep up these days! I’ll make some tweaks.

I just updated my comment with the choose example.

That is very much appreciated as that would of taken me about 2hrs!!

You may not need the templates/choose actions. Turning off a light that is already off is no issue. However closing a cover that is already closed may open it if the close and open actions are the same. So maybe this will work:

    action:
      - service: script.turn_on
        entity_id: script.radio_alarm
      - service: light.turn_off
        target:
          entity_id: light.garage_light_main
      - service: cover.close_cover
        target:
          entity_id: cover.garage

Or maybe you will need the choose action for the cover. You shouldn’t need it for the light.

Thanks again. Based on your feedback I’m searching out all of my service_templates and looking as to how I can improve those too.

Any chance you could chime in on this issue for me too?