Automation to stop media player

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

Probably easier to do some of this Google Home Parental Controls Complete Guide | Protect Young Eyes

They can bypass the music controls by using another music vendor such as spotify

Have you tried multiple triggers and actions without the group?

I don’t think they have an “on” state. I see “off” “paused” or “playing”, so no point in looking for “on”.

As nick mentioned. playing is what you need to trigger on.

block them with the router.

1 Like

I am testing with a single entity now. I will worry about getting the group to work once I can get the test entity figured out.

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:

trigger:
  - platform: device
    device_id: 332e848f299947d5a48577710ea9d03b
    domain: media_player
    entity_id: media_player.boy_s_room_speaker
    type: playing

but that did not work at all

Here is the updated code:

alias: Google Boys room stop music 11pm
trigger:
  - platform: state
    entity_id: media_player.nesthub0327
    to: playing
condition:
  - after: '10:00:00'
    before: '05:00:00'
    condition: time
    weekday:
      - mon
      - tue
      - wed
      - thu
      - fri
      - sat
      - sun
action:
  - data: {}
    service: media_player.media_stop
    target:
      entity_id:
        - group.google_boys
        - media_player.nesthub0327
initial_state: true
mode: restart

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).

Thanks for the response.

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.

If that’s how it behaves with media_player.media_stop then use a different service call, like media_player.media_pause or media_player.turn_off.

If no service call is able to change the state from playing to anything else then there’s something very odd going on.

media_player.turn_off worked! Thanks!!!

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.

1 Like

@tika911: if you mark this thread as solved it helps others. :wink:

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.