I am trying to pass all the defined variables in Automation to the script called in the same Automation action without again declaring them separately in the in the data:
part. Can anyone help me out please?
Automation:
alias: Music Automation
description: Music Automation
trigger:
- platform: time_pattern
seconds: /5
condition: []
action:
- variables:
status_keys: "{{ ['off', 'idle', 'paused'] }}"
playback_volume_key: music_automation_playback_volume_
spotify_account_slug: home_assistant
spotify_uri: spotify:playlist:37i9dQZF1DXdwTUxmGKrdN
- if:
- condition: time
after: "09:00:00"
before: "00:00:00"
then:
- service: script.turn_on
target:
entity_id: script.cast_spotify
data:
variables:
status_keys: "{{ status_keys }}"
spotify_account_slug: "{{ spotify_account_slug }}"
spotify_uri: "{{ spotify_uri }}"
target_id: >-
{% set max_vol = states.input_number | selectattr( 'entity_id',
'search', playback_volume_key ) | selectattr( 'state', '>', 0 |
float | round(2) | string ) | map( attribute = 'state' ) | map(
'float' ) | list %} {% set max_vol = max_vol | min if (
states('input_boolean.music_automation_device_selection_by_minimum_volume')
| string | lower == 'on' ) else max_vol | max %} {% set
selecteded_device = states.input_number | selectattr( 'entity_id',
'search', playback_volume_key ) | selectattr( 'state', 'eq',
max_vol | round( 2 )| string ) | list | first %} {{ dict( volume =
selecteded_device.state | float | multiply( 0.01 ) | round( 2 ),
name = selecteded_device.entity_id | replace( 'input_number.'~
playback_volume_key, 'media_player.' ) ) }}
else:
- service: media_player.turn_off
data: {}
target:
entity_id: "{{ target_id.name }}"
mode: single
Script:
alias: Music Automation - Script
sequence:
- if:
- condition: template
value_template: >-
{{ target_id.volume != state_attr( target_id.name, 'volume_level' ) |
float | round(2) }}
then:
- service: media_player.volume_set
target:
entity_id: "{{ target_id.name }}"
data:
volume_level: "{{ target_id.volume }}"
- if:
- condition: template
value_template: >-
{{ states( target_id.name ) in status_keys }}
then:
- service: spotcast.start
data:
account: "{{ spotify_account_slug }}"
limit: 20
force_playback: false
random_song: "{{ 'off' if ( random_song == empty) else random_song }}"
repeat: "{{ 'off' if ( repeat == empty) else repeat }}"
shuffle: "{{ 'off' if ( shuffle == empty) else shuffle }}"
offset: 0
ignore_fully_played: false
entity_id: "{{ target_id.name }}"
uri: "{{ spotify_uri }}"
mode: single
icon: mdi:cast-audio