I’m trying to create a (seemingly) simply automation that will play/pause music when a button is pushed. This is the current YAML
alias: Music Switch
description: ''
trigger:
- device_id: a86c942e096f44a1a3fbe0f7a2bf9e89
domain: zha
platform: device
type: remote_button_short_press
subtype: remote_button_short_press
condition: []
action:
- service: media_player.select_source
target:
device_id: 6ba7046731e4292e1892f54b3ed7fd2f
- service: media_player.media_play_pause
target:
device_id: 6ba7046731e4292e1892f54b3ed7fd2f
mode: single
I can easily omit the first action and the automation works if the media player is currently playing or the media player has recently been paused but I run into issue when the media player is idle. The above automation gets around this issue by sitting the source for the media player and it will boot it up and pick back up where it left off. However if I press the button again the automation doesn’t work and if I look in the trace timeline (a great recent addition to HA) I get the error “required key not provided @ data[‘source’]”
There’s surely someone out there that has created an automation that addresses the exact issue that might be able to help me out
Have you set a source in the form?Try setting one. That error means it’s required to add a source value in the data field, which you didn’t.
What music integration are you using?
Hi sorry I should have provided a little more info. Here’s what it looks like in the visual editor
The ‘Bedroom’ device represents an alexa speaker group that is accessible through the alexa_media_player integration. I’ve thought about creating a data_template type action for set the source (ie only set the source if the media player is currently off or idle) but I haven’t been able to find the right syntax for it yet
Are you using spotify ? or what?
when it goes idle, if you use the media_player.media_play_pause, it will do nothing, you need to re-request the playlist, that can be done but what music provider are you using?
EDIT:
correct me if I’m wrong but what you are trying to achieve is that anytime you push the button, the music continues exactly in the same spot it was the last time , right? if you press it again, music is paused.
It does eventually play through Spotify. Selecting the source as the bedroom group works to start the speaker up after it’s been idle for a while but if I try to pause it I get that error and if I manually pause it on my dashboard then try and play it again I also get that error. So it only seems to work when the media player is currently idle
And as a direct reply to your edit, that’s the exact behaviour I’m looking for
I’ve played around with something like this but get an error for invalid template rendered
I can’t test it and it’s giving me some error I don’t understand but hipefully it’ll give you some ideas, give it a go and let me know. I had a similar problem in the past and I ended just retriggering the playlist when it was idle. If you want to do that let me know, you’ll need to install the Spotify integration. This code below might work.
You need to replace media_player.your_media_player_here with the entity_id of your group, you can find it under Developer tools/states
you have to replace media_player.any_media_player_here with the entity_id of an individual speaker, it doesn’t matter, just in case don’t use the entity_id of a group
Replace Continue the music in X group with the voice command you’d use to achieve that goal. Maybe Continue in Spotify or something like that
Do you usually play playlists or individual songs??
I still believe it would be much more reliable to just retrigger the playlist/song, that would be the most effective way as you might come upon various problems like, for example, getting the current state of your group wrong or the info of your last played media not being stored anywhere since, at some point, both amazon and Spotify delete that information. And it’s not possible, AFAIK, in HA to select track X from playlist Y. If you don’t use playlists and usually play indiividual songs, that would be much easier to achieve
alias: Music Switch
description: ''
trigger:
- device_id: a86c942e096f44a1a3fbe0f7a2bf9e89
domain: zha
platform: device
type: remote_button_short_press
action:
- choose:
- conditions:
- condition: state
entity_id: media_player.your_media_player_here
state: idle
sequence:
- service: media_player.play_media
data:
entity_id: media_player.any_media_player_here
media_content_type: custom
media_content_id: "Continue the music in X group"
default:
- service: media_player.media_play_pause
entity_id: media_player.your_media_player_here