Help needed with automation with templating

Hello

I want to have an automation which warns me when a sensor changes his state.
I see that the trigger works but I have no action.

Can someone help me and correct the automation?

Thanks

     - alias: ketel alarm
      trigger:
        - platform: state
          entity_id: binary_sensor.ketel
          state: 'on'
        - platform: state
          entity_id: binary_sensor.ketel
          state: 'off'

      action:
        service_template: >
          {% if trigger.to_state.state == "on" %}
          - service: shell_command.run_sudo
            data:
              cmd: 'mpg123 -q /home/pi/sound/chime.mp3'
          - service: tts.google_say
            data:
              message: 'Hello Master, the heating is on.'
          {% elif trigger.to_state.state == "off" %}
          - service: shell_command.run_sudo
            data:
              cmd: 'mpg123 -q /home/pi/sound/chime.mp3'
          - service: tts.google_say
            data:
              message: 'Hello Master, thanks for turning the heating is off.'
          {% endif %}

A service_template can only replace what would normally be “service”. You can’t do a block of else/if and supply alternate data. It just takes the place of “service”. I think the easiest route would be to put each sub-task in its own script, then use service_template to pick the correct one

service_template: >
  {%- if trigger.to_state.state == "on" -%}
    script.heating_on_notify
  {%- elif trigger.to_state.state == "off" -%}
    script.heating_off_notify
  {%- endif %}

Thanks for the quick reply.
I will have to “life” with it as it is :slight_smile:

Regards

Herbert