My teenagers are playing music (some of it pretty offensive) on their google minis at night. Problem is they fall asleep and the music keeps playing all night. I am trying to create and automation that will stop the media player at 11pm which I have accomplished. What I also want to do it prevent them from starting the music up again.
I tried making the automation trigger when the state of the media player group changes to on but this is not working.
Thanks
Greg
alias: Google Boys room stop music 11pm
trigger:
- entity_id: group.google_boys
platform: state
to: 'on'
for:
hours: 0
minutes: 0
seconds: 5
condition:
- after: '23:00:00'
before: '05:00:00'
condition: time
weekday:
- mon
- tue
- wed
- thu
- fri
- sat
- sun
action:
- data: {}
entity_id: group.google_boys
service: media_player.media_stop
initial_state: true
mode: single
Good point I am now triggering on playing. The automation works if they attempt to play music during the specified time it turns the media player off. But if they attempt to play another song after the automation has triggered it does not stop the player.
I think the reason it does now work on the second attempt to play music is that the state does not change from playing (which is weird) to off. It stays on the playing state which would be why the automation does not trigger again. Not sure how to work around this issue…
I tried changing it to a device trigger like this:
Try this and check the media_player’s state after the service call is finished.
alias: Google Boys room stop music 11pm
trigger:
- platform: state
entity_id: media_player.nesthub0327
to: playing
condition:
- condition: time
after: '10:00:00'
before: '05:00:00'
action:
- service: media_player.media_stop
target:
entity_id: '{{ trigger.entity_id }}'
I assume you have the Time Condition set to after 10:00 just for testing purposes (because the original requirement is for after 11pm which is 23:00).
Yes it is set to 10am for testing. I tried your code, same result. I start music on the test device, state changes playing but no music can be heard. State stays as playing. Wait 5 secs or so and ask google to play music again. State still as playing but this time music can be heard.
It seems that when the media_player.media_stop triggers it does not change the state to stop and leaves it in playing state. The only way I can change the state to stop using the device is to tell it to “ok google stop” then the state changes to stop.
Glad to hear that a different service call succeeded.
All of the media_player service calls are designed to control media_player entities. Why do you have a group entity listed as one of the target entities for media_player.media_stop?
You can safely remove it; it’s superfluous because the State Trigger is monitoring only one media_player and the action explicitly references it.
FWIW, the example I posted uses trigger.entity_id to reference the entity listed in the State Trigger. I wrote it like that for a reason and that’s so you can, if you every require it, add other media_player’s to the State Trigger and the action will turn off whichever one triggered the automation.
For example, whichever one of the three listed media_players triggers this automation, that’s the one that will be turned off.
alias: example
trigger:
- platform: state
entity_id:
- media_player.nesthub0327
- media_player.another one
- media_player.yet_another
to: playing
condition:
- condition: time
after: '10:00:00'
before: '05:00:00'
action:
- service: media_player.turn_off
target:
entity_id: '{{ trigger.entity_id }}'
For future reference, if you want the Time Condition to apply for all days of the week, there’s no need to list every day of the week because that’s the default.
I was using the group entity because I have multiple children each with their own google. I like your solution better as it seems easy to manage if I have to add more devices.