Reuse actions in automations?

Hi!

I find that I often use the same sequence of actions in automations. For example, I often use TTS to speak notifications on my Google Home speaker. For this, I need to add several different actions as a sequence and specify different options like this:

- sequence:
    - action: media_player.turn_on
      target:
        entity_id: media_player.kok
    - action: media_player.volume_set
      target:
        entity_id: media_player.kok
      data:
        volume_level: 0.75
    - delay:
        hours: 0
        minutes: 0
        seconds: 1
        milliseconds: 0
    - action: tts.speak
      data:
        media_player_entity_id: media_player.kok
        cache: false
        message: "Fel pĂĄ robotdammsugaren."
        options:
          voice: Achernar

It’s easy to copy this yaml and reuse it for other automations BUT if I later want to change for example the voice: option I need to manually change in all automations.

Is it somehow possible in HA to add these actions to a separate yaml-file or something and use this in automations? I would preferable want to be able to change some options like message: for each automation.

That way I could easily change all automations using these actions at the same time. I do this using groups for entities (it’s easier changing a group than changing all automations) but I haven’t thought about doing it for automations.

Is it possible? If yes, how?

Yes, with a script.

Scripts

Script Syntax

You call the script from your automation using the script.turn_on action or by using the script’s entity_id. You can also pass variables to the script (so the script can do different things depending on the values it receives).

1 Like

Aha! I’ve never had reason to dig into the scripts feature but it looks like it’s exactly what I was trying to ask about… Seems a bit hard to get used to but now I know where to look!

1 Like