I have a script for making announcements that is basically the base for many other scripts.
the script does the following:
- take variables from the automation ie target speaker and service (either alexa or googletts)
- contains the announcement message for that particular automation
- contains the template for the speakers and the room it is in
- check if there is anyone in the target speaker’s room or wait until someone is there
- makes the announcement if the condition for the announcement is still true
- makes the announcement using which sequence depending on whether to use alexa or googletts.
part 3 and 6 are the same for all the different announcement scripts.
i would like to break up the script like this:
- take variables from the automation ie target speaker and service (either alexa or googletts)
- contains the announcement message for that particular automation
- get a file which contains the template for the speakers and the room it is in
- check if there is anyone in the target speaker’s room or wait until someone is there
- makes the announcement if the condition for the announcement is still true
- get a file that makes the announcement using which sequence depending on whether to use alexa or googletts.
i looked at the docs https://www.home-assistant.io/docs/configuration/splitting_configuration/ but I couldn’t get make it work. where do i save the yaml files, and how should i define it inside the script?
are all the data variables from the automation that called the first script passed to another script that is called from inside the script? are the variables from the first script passed to the second script?
here is my script.
alias: Announce Gate Left Open
sequence:
- variables:
announcement_msg: >-
'{{ ( "Did you forget to close the gate?","Someone left the gate
open","Did someone forget to close the gate?","I think someone forgot to
close the gate","The gate has been open for a while... did someone
forget to close it?","Somebody forgot to close the gate...","The gate is
still open" ) | random }}'
old_volume: '{{ state_attr(speaker_device,''volume_level'') | float }}'
mediaplayer_State: '{{ states(speaker_device) }}'
- wait_template: |-
{{ (speaker_device == 'media_player.echo_show' and
is_state('input_boolean.room_occupancy_study', 'on')) or
(speaker_device == 'media_player.sonos_beam' and
is_state('input_boolean.room_occupancy_living_dining', 'on')) or
(speaker_device == 'media_player.kitchen_2' and
is_state('input_boolean.room_occupancy_kitchen', 'on')) or
(speaker_device == 'media_player.landing_2' and
is_state('input_boolean.room_occupancy_1st_floor', 'on')) }}
timeout: '00:30:00'
continue_on_timeout: false
- condition: state
entity_id: binary_sensor.zone_gate_open
state: 'on'
- choose:
- conditions:
- condition: template
value_template: '{{ speaker_service == ''notify.alexa_media'' }}'
sequence:
- service: media_player.media_pause
target:
entity_id: '{{ speaker_device }}'
- service: media_player.volume_set
data:
volume_level: '{{ announce_volume }}'
target:
entity_id: '{{ speaker_device }}'
- service: '{{ speaker_service }}'
data:
data:
type: tts
message: '{{ announcement_msg }}'
target: '{{ speaker_device }}'
- service: media_player.volume_set
data:
volume_level: '{{ old_volume }}'
target:
entity_id: '{{ speaker_device }}'
- condition: template
value_template: '{{ mediaplayer_State == ''playing'' }}'
- delay:
hours: 0
minutes: 0
seconds: 10
milliseconds: 0
- service: media_player.media_play
target:
entity_id: '{{ speaker_device }}'
- conditions:
- condition: template
value_template: '{{ speaker_service == ''tts.google_translate_say'' }}'
sequence:
- service: media_player.media_pause
target:
entity_id: '{{ speaker_device }}'
- service: media_player.volume_set
data:
volume_level: '{{ announce_volume }}'
target:
entity_id: '{{ speaker_device }}'
- service: '{{ speaker_service }}'
data:
entity_id: '{{ speaker_device }}'
message: '{{ announcement_msg }}'
- delay:
hours: 0
minutes: 0
seconds: 1
milliseconds: 0
- delay:
hours: 0
minutes: 0
seconds: '{{ state_attr(speaker_device,''media_duration'') | float }}'
milliseconds: 0
- service: media_player.volume_set
data:
volume_level: '{{ old_volume }}'
target:
entity_id: '{{ speaker_device }}'
- condition: template
value_template: '{{ mediaplayer_State == ''playing'' }}'
- delay:
hours: 0
minutes: 0
seconds: 3
milliseconds: 0
- service: media_player.media_play
target:
entity_id: '{{ speaker_device }}'
default:
- stop: ''
mode: parallel
max: 5
i will appreciate any advice!
p/s i’m just starting out, so I may be doing this the wrong way, but please teach me patiently