How to reuse actions?

Hi.

How can a define an action at a common place and reuse it in several yaml scripts?

Eg I want to use this action in several yaml files and would like to define it only once:

action: media_player.join
metadata: {}
data:
  entity_id: media_player.kitchen
  group_members:
    - media_player.living
    - media_player.floor

Thanks in advance.

Put it in a script on its own, and call the script from your other scripts/automations. You can use a field to pass data.

For example, this creates a voice assistant response to the question “Where’s my phone/keys/watch?” The field is tts_sentence.

  action:
    - variables:
        last_seen: "{{ device }}_last_seen"
    - choose:
        - conditions: "{{ states(device) in ('unavailable', 'unknown') }}"
          sequence:
            - action: script.tts_response
              data:
                tts_sentence: >-
                  {% if device == "sensor.keys_tag_area" %}
                     Last time I saw them, they were in the {{ states(last_seen) }}. I don't know where they are now.             
                  {% else %}
                     Last time I saw it, it was in the {{ states(last_seen) }}. I don't know where it is now.
                  {% endif %}                  
        - conditions: "{{ has_value(device) }}"
          sequence:
            - action: script.tts_response
              data:
                tts_sentence: >-
                  {% if device == "sensor.keys_tag_area" %}
                      They're in the {{ states(device) }}              
                  {% else %}
                      It's in the {{ states(device) }}
                  {% endif %}  
1 Like

Thanks. The hint regarding scripts was very helpful.
Further docu I found here: