How to shorten this code

Hi,

I am a beginner in YAML and HA and Automations.

How can I shorten this code and do more “do not repeat yourself”?

In other languages I can write a function with parameters but not in YAML.

Regards

alias: "Notification: Urgent Message: Full"
description: ""
trigger:
  - platform: state
    entity_id:
      - input_button.urgent_message_send_message
condition:
  - condition: and
    conditions:
      - condition: template
        value_template: "{{ states('input_text.urgent_messge_txt') != 'unknown' }}"
      - condition: template
        value_template: "{{ states('input_text.urgent_messge_txt') != '' }}"
action:
  - service: input_text.set_value
    data:
      value: >-
        {{ state_attr(((states.person | selectattr('attributes.user_id','==',
        states.input_button.urgent_message_send_message.context.user_id))| list
        | first or 'system').entity_id, 'friendly_name') }}
    target:
      entity_id: input_text.urgent_message_txt_sendername
  - if:
      - condition: state
        entity_id: input_select.urgent_message_cmb
        state: Sprachausgabe normal
    then:
      - if:
          - condition: state
            entity_id: input_boolean.urgent_message_p1
            state: "on"
        then:
          - service: notify.mobile_app_p1
            data:
              message: TTS
              data:
                tts_text: "{{ states('input_text.urgent_messge_txt') }}"
      - if:
          - condition: state
            entity_id: input_boolean.urgent_message_p2
            state: "on"
        then:
          - service: notify.mobile_app_p2
            data:
              message: TTS
              data:
                tts_text: "{{ states('input_text.urgent_messge_txt') }}"
      - if:
          - condition: state
            entity_id: input_boolean.urgent_message_p3
            state: "on"
        then:
          - service: notify.mobile_app_p1
            data:
              message: TTS
              data:
                tts_text: "{{ states('input_text.urgent_messge_txt') }}"
      - if:
          - condition: state
            entity_id: input_boolean.urgent_message_p4
            state: "on"
        then:
          - service: notify.mobile_app_p4
            data:
              message: TTS
              data:
                tts_text: "{{ states('input_text.urgent_messge_txt') }}"
      - if:
          - condition: state
            entity_id: input_boolean.urgent_message_p5
            state: "on"
        then:
          - service: notify.mobile_app_p5
            data:
              message: TTS
              data:
                tts_text: "{{ states('input_text.urgent_messge_txt') }}"
      - service: input_text.set_value
        data:
          value: Nachricht erfolgreich gesendet
        target:
          entity_id: input_text.urgent_messge_txt
    else:
      - if:
          - condition: state
            entity_id: input_select.urgent_message_cmb
            state: Sprachausgabe wichtig
        then:
          - if:
              - condition: state
                entity_id: input_boolean.urgent_message_p1
                state: "on"
            then:
              - service: notify.mobile_app_p1
                data:
                  message: TTS
                  data:
                    tts_text: "{{ states('input_text.urgent_messge_txt') }}"
                    media_stream: alarm_stream
          - if:
              - condition: state
                entity_id: input_boolean.urgent_message_p2
                state: "on"
            then:
              - service: notify.mobile_app_p2
                data:
                  message: TTS
                  data:
                    tts_text: "{{ states('input_text.urgent_messge_txt') }}"
                    media_stream: alarm_stream
          - if:
              - condition: state
                entity_id: input_boolean.urgent_message_p3
                state: "on"
            then:
              - service: notify.mobile_app_p1
                data:
                  message: TTS
                  data:
                    tts_text: "{{ states('input_text.urgent_messge_txt') }}"
                    media_stream: alarm_stream
          - if:
              - condition: state
                entity_id: input_boolean.urgent_message_p4
                state: "on"
            then:
              - service: notify.mobile_app_p4
                data:
                  message: TTS
                  data:
                    tts_text: "{{ states('input_text.urgent_messge_txt') }}"
                    media_stream: alarm_stream
          - if:
              - condition: state
                entity_id: input_boolean.urgent_message_p5
                state: "on"
            then:
              - service: notify.mobile_app_p5
                data:
                  message: TTS
                  data:
                    tts_text: "{{ states('input_text.urgent_messge_txt') }}"
                    media_stream: alarm_stream
          - service: input_text.set_value
            data:
              value: Nachricht erfolgreich gesendet
            target:
              entity_id: input_text.urgent_messge_txt
        else:
          - if:
              - condition: state
                entity_id: input_select.urgent_message_cmb
                state: Sprachausgabe dringend
            then:
              - if:
                  - condition: state
                    entity_id: input_boolean.urgent_message_p1
                    state: "on"
                then:
                  - service: notify.mobile_app_p1
                    data:
                      message: TTS
                      data:
                        tts_text: "{{ states('input_text.urgent_messge_txt') }}"
                        media_stream: alarm_stream_max
              - if:
                  - condition: state
                    entity_id: input_boolean.urgent_message_p2
                    state: "on"
                then:
                  - service: notify.mobile_app_p2
                    data:
                      message: TTS
                      data:
                        tts_text: "{{ states('input_text.urgent_messge_txt') }}"
                        media_stream: alarm_stream_max
              - if:
                  - condition: state
                    entity_id: input_boolean.urgent_message_p3
                    state: "on"
                then:
                  - service: notify.mobile_app_p1
                    data:
                      message: TTS
                      data:
                        tts_text: "{{ states('input_text.urgent_messge_txt') }}"
                        media_stream: alarm_stream_max
              - if:
                  - condition: state
                    entity_id: input_boolean.urgent_message_p4
                    state: "on"
                then:
                  - service: notify.mobile_app_p4
                    data:
                      message: TTS
                      data:
                        tts_text: "{{ states('input_text.urgent_messge_txt') }}"
                        media_stream: alarm_stream_max
              - if:
                  - condition: state
                    entity_id: input_boolean.urgent_message_p5
                    state: "on"
                then:
                  - service: notify.mobile_app_p5
                    data:
                      message: TTS
                      data:
                        tts_text: "{{ states('input_text.urgent_messge_txt') }}"
                        media_stream: alarm_stream_max
              - service: input_text.set_value
                data:
                  value: Nachricht erfolgreich gesendet
                target:
                  entity_id: input_text.urgent_messge_txt
            else:
              - if:
                  - condition: state
                    entity_id: input_select.urgent_message_cmb
                    state: Benachrichtigung
                then:
                  - if:
                      - condition: state
                        entity_id: input_boolean.urgent_message_p1
                        state: "on"
                    then:
                      - service: notify.mobile_app_p1
                        data:
                          title: "{{ states('urgent_message_txt_sendername') }}"
                          message: "{{ states('input_text.urgent_messge_txt') }}"
                  - if:
                      - condition: state
                        entity_id: input_boolean.urgent_message_p2
                        state: "on"
                    then:
                      - service: notify.mobile_app_p2
                        data:
                          title: "{{ states('urgent_message_txt_sendername') }}"
                          message: "{{ states('input_text.urgent_messge_txt') }}"
                  - if:
                      - condition: state
                        entity_id: input_boolean.urgent_message_p3
                        state: "on"
                    then:
                      - service: notify.mobile_app_p1
                        data:
                          title: "{{ states('urgent_message_txt_sendername') }}"
                          message: "{{ states('input_text.urgent_messge_txt') }}"
                  - if:
                      - condition: state
                        entity_id: input_boolean.urgent_message_p4
                        state: "on"
                    then:
                      - service: notify.mobile_app_p4
                        data:
                          title: "{{ states('urgent_message_txt_sendername') }}"
                          message: "{{ states('input_text.urgent_messge_txt') }}"
                  - if:
                      - condition: state
                        entity_id: input_boolean.urgent_message_p5
                        state: "on"
                    then:
                      - service: notify.mobile_app_p5
                        data:
                          title: "{{ states('urgent_message_txt_sendername') }}"
                          message: "{{ states('input_text.urgent_messge_txt') }}"
                else:
                  - service: input_text.set_value
                    data:
                      value: Nachricht erfolgreich gesendet
                    target:
                      entity_id: input_text.urgent_messge_txt
mode: single

You can write a script that takes parameters, and also add variables inline in the script or automation action if you want to further simplify any logic below.

https://www.home-assistant.io/integrations/script/#passing-variables-to-scripts
https://www.home-assistant.io/docs/scripts#variables

I use them all the time to create reusable code and simplify logic. In your case, all of the service: and data: portions can be written using template variables and you can use template conditions, all using the passed in variables.

As an example, I would create a script like this:

send_urgent_message:
  sequence:
    - condition:  template
      vaiue_template: "{{ is_state('input_boolean.urgent_message_p' ~ priority, 'on' }}"
    - service: notify.mobile_app_p{{ priority }}
      data:
        message: TTS
          data:
            tts_text: "{{ states('input_text.urgent_messge_txt') }}"

then use a Counted Repeat structure to repeatedly call it with differnet “p” levels: https://www.home-assistant.io/docs/scripts#counted-repeat

          - if:
              - condition: state
                entity_id: input_select.urgent_message_cmb
                state: Sprachausgabe dringend
            then:
              - repeat:
                  count: 5
                  sequence:
                    - if: "{{ is_state('input_boolean.urgent_message_p' ~ repeat.item, 'on') }}"
                      then:
                        - service: "notify.mobile_app_p{{ repeat.item }}"
                          data:
                            message: TTS
                            data:
                              tts_text: "{{ states('input_text.urgent_messge_txt') }}"
                              media_stream: alarm_stream_max

is there a way to have a if-then-else statement here to “change” the value?

Pseudocode

media_stream: {{ input_select.urgent_message_cmb == 'Sprachausgabe dringend' ? 'alarm_strem_max' : 'alarm_stream' }}
media_stream: "{{ iif(states('input_select.urgent_message_cmb') == 'Sprachausgabe dringend', 'alarm_strem_max', 'alarm_stream') }}"

Reference: Immediate If