Simplify script templates

I have currently a dozen turn_on_xxx and turn_off_xxx scripts. Below is just a sample.
As you can see they all do the same action different times.

turn_on_xxx
set swtich to on
start timer
send notification

turn_off_xxx
set swtich to off
stop timer
send notification

Is there a way to template the above 3 action to just have only 2 scripts turn_on_xxx and turn_off_xxx

automation:

  - id: start_scenes
    initial_state: 'true'
    alias: 'start scenes'
    trigger:
    - platform: state
      entity_id: switch.stp_keuken_microgolf,switch.stp_keuken_oven
      from: 'off'
      to: 'on'
    action:
    - service: python_script.easyplus
      data_template:
        script_id: "turn_on_{{ trigger.to_state.name }"

  - id: stop_scenes
    initial_state: 'true'
    alias: 'stop scenes manual'
    trigger:
    - platform: state
      entity_id: switch.stp_keuken_microgolf,switch.stp_keuken_oven
      from: 'on'
      to: 'off'
    action:
    - service_template: "script.turn_off_{{ trigger.to_state.name }}"


script:

  turn_on_microgolf:
    sequence:
    - service: homeassistant.turn_on
      entity_id: switch.stp_keuken_microgolf
    - service: timer.start
      entity_id: timer.microgolf
    - delay:
        seconds: 4
    - service: notify.telegram
      data_template:
        message: 'microgolf program: {{states.switch.stp_keuken_microgolf.state}}'

  turn_off_microgolf:
    sequence:
    - service: homeassistant.turn_off
      entity_id: switch.stp_keuken_microgolf
    - service: timer.cancel
      entity_id: timer.microgolf
    - delay:
        seconds: 4
    - service: notify.telegram
      data_template:
        message: 'microgolf program: {{states.switch.stp_keuken_microgolf.state}}'

  turn_on_oven:
    sequence:
    - service: homeassistant.turn_on
      entity_id: switch.stp_keuken_oven
    - service: timer.start
      entity_id: timer.oven
    - delay:
        seconds: 4
    - service: notify.telegram
      data_template:
        message: 'oven program: {{states.switch.stp_keuken_oven.state}}'

  turn_off_oven:
    sequence:
    - service: homeassistant.turn_off
      entity_id: switch.stp_keuken_oven
    - service: timer.cancel
      entity_id: timer.oven
    - delay:
        seconds: 4
    - service: notify.telegram
      data_template:
        message: 'oven program: {{states.switch.stp_keuken_oven.state}}'
1 Like

may sound like a silly question, but why do you need the script at all?
Why not move your script actions to the automation itself?
Then you could use the {{trigger.to_state}} elements

(btw you’ve forgotten a double quote (") at the end of your first automation above)

You can enter all of the entities into the on/off automation triggers then based on the trigger you can create a script data element based on the data of the trigger to_state and pass all that to the scripts.

automation:
  id: start_scenes
    initial_state: 'true'
    alias: 'start scenes'
    trigger:
    - platform: state
      entity_id: switch.stp_keuken_microgolf,switch.stp_keuken_oven
      from: 'off'
      to: 'on'
    - other triggers
    action:
    - service: script.turn_on_xxx
      data_template:
        entity: '{{ trigger.to_state.entity_id }}'
        timer: 'timer.{{ trigger.to_state.attributes.friendly_name }}'
        friendly_name: '{{ trigger.to_state.attributes.friendly_name }}'
        state: '{{ trigger.to_state.state }}'

script:
  turn_on_xxx:
    sequence:
    - service: homeassistant.turn_on
      data_template:
        entity_id: '{{ entity }}'
    - service: timer.start
      data_template:
         entity_id: '{{ timer }}'
    - delay:
        seconds: 4
    - service: notify.telegram
      data_template:
        message: '{{ friendly_name }} program: {{state}}'

Or something along those lines. I think it should work OK.

Obviously I haven’t tested it but it should give you an idea on how it might work.

2 Likes

I want to eliminate the needs of creating multiple automations, goals is have 1 automation to the turn off script and the other the turn on.

@finity thank you very much for your proposal, very nice solution indeed.

I’ll test and report back.

If you want to reduce the number of scripts / automations, You could end up with a single automation that handles on/ off for everything… Just saying :wink:

1 Like

Remember scripts are not re-entrant under HA so multiple events needing to call the same script at the same time will fail.

You will get the “script XXXX already running” errors in the log.

Automations do not have this issue.

1 Like