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