Tonkin
(Rune Bay)
October 19, 2017, 6:35am
1
From a four way Z-wave wall switch (ZHC5010) I use two buttons to control my kitchen Sonos (entity_id: media_player.kkken).
One button to turn_on/toggle radio chanel and a second to turn_off radio.
In order to reuse my code base I have put my radio URLs in a separate script, and this is where my problems begin:
I seems like the script “play_radio” is not receiving the variables.
The error log shows the following:
2017-10-19 12:23:55 ERROR (MainThread) [homeassistant.core] Invalid service data for media_player.play_media: Entity ID media_player. is an invalid entity id for dictionary value @ data['entity_id']. Got 'media_player.'
How do I pass variables/values from script “next_preset_radio” to script “play_radio”?? Any Ideas to get my scripts working?
script:
next_preset_radio:
alias: 'Set Next Preset Radio'
sequence:
#NOTE TO SELF: Look into input_select.select_next
- service: input_select.select_option
data_template:
entity_id: input_select.next_radio_dropdown
option: >
{% if is_state('input_select.next_radio_dropdown', 'P1') %}
P6
{% elif is_state('input_select.next_radio_dropdown', 'P6') %}
24syv
{% elif is_state('input_select.next_radio_dropdown', '24syv') %}
P3
{% elif is_state('input_select.next_radio_dropdown', 'P3') %}
P1
{% endif %}
- service: media_player.volume_set
#states.media_player[where].attributes.media_duration
data_template:
entity_id: "{{ 'media_player.' ~ where }}"
volume_level: >
{% if is_state('media_player.kkken', 'playing') %}
{{ states.media_player[where].attributes.volume_level }}
{% else %}
{{ volume|default(0.2) }}
{% endif %}
- service: script.play_radio
data_template:
data:
variables:
where: "{{ where }}"
station: "'{{ states('input_select.next_radio_dropdown') }}'"
Second script:
play_radio:
alias: 'Play preset radio on Sonos'
sequence:
- service: media_player.play_media
data_template:
entity_id: "{{ 'media_player.' ~ where }}"
media_content_id: >-
{% if station == 'P1' %}
x-rincon-mp3radio://live-icy.gss.dr.dk:80/A/A03H.mp3
{% elif station == 'P3' %}
x-rincon-mp3radio://live-icy.gss.dr.dk:80/A/A05H.mp3
{% elif station == 'P6' %}
x-rincon-mp3radio://live-icy.gss.dr.dk:80/A/A29H.mp3
{% elif station == '24syv' %}
x-rincon-mp3radio://http://streaming.radio24syv.dk/Web
{% endif %}
media_content_type: "MUSIC"
And my automations:
automation:
#Radio On
- alias: KITCHEN - NODE 3
hide_entity: True
trigger:
platform: state
entity_id: switch.switch_kitchen_main_switch_kitchen_n3
action:
service: script.turn_on
entity_id: script.next_preset_radio
data:
variables:
where: 'kkken'
volume: 0.2
#Radio Off
- alias: KITCHEN - NODE 4
hide_entity: True
trigger:
platform: state
entity_id: switch.switch_kitchen_main_switch_kitchen_n4
action:
service: media_player.media_pause
entity_id: media_player.kkken
Tonkin:
data:
variables:
Remove these two lines in your first script.
And in your Automation, I will use the following:
action:
service: script.next_preset_radio
data:
where: 'kkken'
volume: 0.2
Instead
On another note, I like the way you used the square brackets below:
{{ states.media_player[where].attributes.volume_level }}
Never knew that worked. Thanks for the example
Regards
1 Like
Tonkin
(Rune Bay)
October 20, 2017, 6:55am
3
@Odianosen25 YOU ARE A CHAMP!
I still need to fix the small bit of hard code I the script next_preset_radio:
{% if is_state('media_player.kkken', 'playing') %}
But first CELEBRATION!
The code now looks like this:
Scripts
next_preset_radio:
alias: 'Set Next Preset Radio'
sequence:
- service: input_select.select_next
entity_id: input_select.next_radio_dropdown
- service: media_player.volume_set
#states.media_player[where].attributes.media_duration
data_template:
entity_id: "{{ 'media_player.' ~ where }}"
volume_level: >
{% if is_state('media_player.kkken', 'playing') %}
{{ states.media_player[where].attributes.volume_level }}
{% else %}
{{ volume|default(0.2) }}
{% endif %}
- service: script.play_radio
data_template:
where: "{{ where }}"
station: "{{ states('input_select.next_radio_dropdown') }}"
play_radio:
alias: 'Play preset radio on Sonos'
sequence:
- service: media_player.play_media
data_template:
entity_id: "{{ 'media_player.' ~ where }}"
media_content_id: >-
{% if station == 'P1' %}
x-rincon-mp3radio://live-icy.gss.dr.dk:80/A/A03H.mp3
{% elif station == 'P3' %}
x-rincon-mp3radio://live-icy.gss.dr.dk:80/A/A05H.mp3
{% elif station == 'P6' %}
x-rincon-mp3radio://live-icy.gss.dr.dk:80/A/A29H.mp3
{% elif station == '24syv' %}
x-rincon-mp3radio://http://streaming.radio24syv.dk/Web
{% endif %}
media_content_type: "MUSIC"
Automations
#Radio On
- alias: KITCHEN - NODE 3
hide_entity: True
trigger:
platform: state
entity_id: switch.switch_kitchen_main_switch_kitchen_n3
action:
service: script.next_preset_radio
data:
where: ‘kkken’
volume: 0.2
#Radio Off
- alias: KITCHEN - NODE 4
hide_entity: True
trigger:
platform: state
entity_id: switch.switch_kitchen_main_switch_kitchen_n4
action:
service: media_player.media_pause
entity_id: media_player.kkken
You welcome and glad I could be of help
Do u mean you have a problem with the above line of code?
Regards
Tonkin
(Rune Bay)
October 20, 2017, 9:43am
5
The idea is to make to code reusable on all media players, but in the code snippet above, I have hard coded the media_player named “kkken”.
This must be fixed, but for now, Im just happy that it all works!
Have you been into HASS for a long time? I my self got into it about a year ago, but only recently have I really begun complex automations - so a lot of learning and a lot of frustrations
/Tonkin
Tonkin
(Rune Bay)
October 20, 2017, 9:47am
6
Looking at the code this dissent look all that hard!?!
If I in next_preset_radio change:
{% if is_state('media_player.kkken', 'playing') %}
To:
{% if is_state( states.media_player[where] , 'playing') %}
Should work. I’ll try soon!
Hello @Tonkin ,
Well I am into HA for less than a year to be honest, but I love programming as a person and did a bit of C++ a long time ago when in school.
As regards the below
I think you should try this if it doesn’t work
{% if 'states.media_player.' ~ where ~ '.state' == 'playing' %}
And please let me know how it goes, as I will like to know the result myself. I would have tried it, but not home now to access my system
Regards
caple
(Caple)
April 23, 2018, 3:41am
8
Hello,
Did you ever get this media player variable to work? I’m have something similar and cant seem to get to work.
If you mean based on what I wrote here
Which was wrong, this is the right way to do it instead
{% if states('media_player.' ~ where) == 'playing' %}
Regards
caple
(Caple)
April 23, 2018, 4:48am
10
That did it, thank you so much. What I ended up doing was to pass the entire entity_id as the variable and then reference is as
{% if is_state(where , 'playing' %}
But I like your way better so i’m switching.
caple
(Caple)
April 24, 2018, 2:10am
11
hello Odianosen25
with your help the if statement works, however, i cannot seem to get the entity_id to work. Please help.
- service_template: >
{% if states('media_player.' ~ where) == 'off' %}
media_player.turn_on
{% endif %}
entity_id: 'media_player.' ~ where