Play Sonos on Xiaomi Motion Sensor

I have just acquired an extra Sonos Play1 and would like to have a simple automation to turn it on/off.

I have a Xiaomi motion sensor in the bathroom where the Sonos Play1 is.

Simply want to have the Sonos Play on detecting motion (someone in the bathroom) and Pause when there is no motion for 2 minutes (no-one in bathroom any more).

This will be my first automation with Home Assistant and just wondering if anyone has something similar or if they know the simplest way of doing it with automations.

OK Got it working to turn the Sonos on with the following in case anyone wants to know a fairly simple way of turning your Sonos music on with a Xiaomi sensor, just trying to work out how to get it to stop when there is no presence for 1-2 minutes or so.

  • id: ‘1553260834833’
    alias: Bathroom Sonos on Xiaomi Sensor
    initial_state: true
    trigger:
    • entity_id: binary_sensor.motion_sensor_158d0002488d6c
      from: ‘off’
      platform: state
      to: ‘on’
      condition: []
      action:
    • service: media_player.media_play_pause
      data:
      entity_id: media_player.bathroom
  • id: ‘1553260834833’
    alias: Bathroom Sonos off Xiaomi Sensor
    trigger:
    • entity_id: binary_sensor.motion_sensor_158d0002488d6c
      from: ‘on’
      platform: state
      to: ‘off’
      condition: []
      action:
    • service: media_player.media_play_pause
      data:
      entity_id: media_player.bathroom

Talking to myself here but maybe will help someone whos just starting with automations who wants to do the same thing.

I looked around here and have now got it fully working, my Sonos plays music whilst people are in the bathroom and then 2 minutes after no motion is detected the music stops.

My Automations.Yaml looks like this

################################################################################

Music in Bathroom Motion

################################################################################

  • alias: Sonos in Bathroom on Motion Timed
    trigger:
    • platform: state
      entity_id: binary_sensor.motion_sensor_158d0002565c5c
      to: ‘on’
      action:
    • service: timer.start
      entity_id: timer.timer_bath
    • service: media_player.media_play
      entity_id: media_player.bathroom
  • alias: End of Timer for Sonos in Bathroom
    trigger:
    • platform: event
      event_type: timer.finished
      event_data:
      entity_id: timer.timer_bath
      action:
      service: media_player.media_pause
      entity_id: media_player.bathroom

and I added this to the configurations.yaml

timer:
timer_bath:
duration: ‘00:02:00’

1 Like

Congrats on figuring out your first automation by your self.

It would be really helpful for other new starters if you could format your code as per the blue box at the top of this page. That way people can read your code better as formatting is critical to yaml.

OK I will go back and change it, it’s not quite working correctly though - the music only comes on for 2 minutes, rather than 2 minutes from when motion was last detected.

Cant work out how to do it, it just puts the first line in the box

literally this is me pressing that button and cut and pasting

- id: SonosBathMotionOn 

alias: “Sonos in Bathroom on Motion Timed”
trigger:
- platform: state
entity_id: binary_sensor.motion_sensor_158d0002565c5c
to: ‘on’
action:
- service: timer.start
entity_id: timer.timer_bath
- service: media_player.media_play
entity_id: media_player.bathroom

  • id: SonosBathMotionOff
    alias: “End of Timer for Sonos in Bathroom”
    trigger:
    • platform: event
      event_type: timer.finished
      event_data:
      entity_id: timer.timer_bath
      action:
      service: media_player.media_pause
      entity_id: media_player.bathroom`

This is mine, fully working.

- alias: Bewegungssensor Badezimmer an
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_158d000836cdde
    from: 'off'
    to: 'on'
  condition:
    condition: and
    conditions:
      - condition: time
        after: '06:00:00'
      - condition: time
        before: '23:00:00'
  action:
    - service: media_player.play_media
      data: 
        entity_id: media_player.mikes_echo_dot
        media_content_id: KISS FM BERLIN - Oldschool R'n'B
        media_content_type: TUNEIN
        
- alias: Bewegung Badezimmer aus
  trigger:
    platform: state
    entity_id: binary_sensor.motion_sensor_158d000836cdde
    from: 'on'
    to: 'off'
    for:
      seconds: 10
  action:
    - service: media_player.media_stop
      data: 
        entity_id: media_player.mikes_echo_dot
1 Like