I’m pretty good with If/Then statements but this had me stumped for an embarassingly long time understanding how homeassistant nested statements and triggers. So I thought I’d share for the community because I’ve been unable to find anything else like it.
I wanted a single button (Insteon Keypad scene switch) to trigger a particular YoutubeMusic playlist to start playing, but I wanted the button to pause the music when the button was pressed again, and then play again when pressed a third time without starting the entire station over again.
I also had to add some safegaurds so if the station timed out or HA was restarted, the station would also be spooled up to play.
Couple that with the fact that the Youtube playlists would start from the beginning every time it played the station. So I had to add a shuffle function to “True” after it starts playing (because I often toggle shuffe back off)
So this automation will start the requested station if its not already set to play, and just toggle the play function if it is. It will pause the music when the switch is in the off state, so its ready for the play button to toggle again and you don’t have to start a new song every time you pause.
alias: Garage Keypad (A) Top 40
description: ""
trigger:
- platform: state
entity_id:
- switch.keypadlinc_on_off_20_41_d0_button_a
condition: []
action:
- if:
- condition: state
for:
hours: 0
minutes: 0
seconds: 0
entity_id: switch.keypadlinc_on_off_20_41_d0_button_a
state: "on"
- condition: and
conditions:
- condition: or
conditions:
- condition: state
entity_id: media_player.mass_living_room_speaker
state: "off"
- condition: or
conditions:
- condition: state
entity_id: media_player.mass_living_room_speaker
state: standby
- condition: or
conditions:
- condition: state
entity_id: media_player.mass_living_room_speaker
state: idle
then:
- service: media_player.play_media
target:
entity_id: media_player.mass_living_room_speaker
data:
media_content_id: media-source://mass/database://playlist/3
media_content_type: audio/flac
metadata:
title: Today's Hits Clean 2023 - Clean Songs Playlist - Clean Music 2023
thumbnail: >-
https://images.weserv.nl/?w=200&url=https://yt3.googleusercontent.com/k7lNzFinL4OYymm5XnFmyDaLqpTBNQ4xyBrtGLW9NIQqOQG_RaCKZDBnQn3G8qK8N3LGsdwT7Fo=s1200
media_class: playlist
children_media_class: null
navigateIds:
- {}
- media_content_type: app
media_content_id: media-source://mass
- media_content_type: music
media_content_id: media-source://mass/playlists
- service: media_player.shuffle_set
data:
shuffle: true
target:
entity_id: media_player.mass_living_room_speaker
else:
- if:
- condition: state
entity_id: switch.keypadlinc_on_off_20_41_d0_button_a
state: "on"
- condition: and
conditions:
- condition: state
entity_id: media_player.mass_living_room_speaker
state: paused
then:
- service: media_player.media_play
data: {}
target:
entity_id: media_player.mass_living_room_speaker
- if:
- condition: state
entity_id: switch.keypadlinc_on_off_20_41_d0_button_a
state: "off"
then:
- service: media_player.media_pause
data: {}
target:
entity_id: media_player.mass_living_room_speaker
mode: single