Well, first you can combine this into a single automation using templates. Second, you can get the current volume using templates. To get the current volume and set it back, you need to store the information. This can be a pain in the ass. To get around that, we can simply create a script that handles this, then pass a variable to it. That variable can then be used anywhere in the script. We can make this script handle any light as well, also using variables.
automation
- alias: kitchenLightsOn
trigger:
- entity_id: switch.keuken
platform: state
condition:
condition: template
value_template: "{{ trigger.from_state.state != trigger.to_state.state }}"
action:
service: script.announce_light_change
data_template:
alexa_device: media_player.bas_echo_dot
alexa_volume: "{{ state_attr('media_player.bas_echo_dot', 'volume') }}"
alexa_message: >
{{ state_attr(trigger.entity_id, 'friendly_name') }} {{ trigger.to_state.state }}
script
announce_light_change:
sequence:
- service: media_player.volume_set
data_template:
entity_id: "{{ alexa_device }}"
volume_level: 0.5
- service: service: media_player.alexa_tts
data_template:
entity_id: "{{ alexa_device }}"
message: "{{ alexam_message }}"
- service: media_player.volume_set
data_template:
entity_id: "{{ alexa_device }}"
volume_level: "{{ alexa_volume }}"
So what this does is it passes 3 variables. Those variables are alexa_device, alexa_volume, and alexa_message.
When you add them to the data, or data_template section for the script service, the values in the data section are passed via those variables. So if I make this data section with the following script service:
service: script.announce_light_change
data_template:
alexa_device: media_player.my_alexa_device
alexa_volume: 1.0
alexa_message: "SUPER LOUD"
The script will recieve 3 variables, alexa_device (which contains media_player.my_alexa_device), alexa_volume (containing 1.0), and alexa_message (contianing SUPER LOUD).
The script accesses the variables through what is called âtemplatingâ.
It is an advanced topic. Anyways, the script accesses the variables through templating in each data_template section.