Lazy man's automatically pausing/resuming media player based on room presence

I’d like to share something I really got used to when I am home alone. You all know this situation when you start a movie on your television in the livingroom and suddenly you realize that your snacks are still in the kitchen so you press pause on the remote, grab your snacks and return to the livingroom and press play again just until your realize that your beer is still in the kitchen so you press pause and grab your beer and get back to the livingroom and press play… you get the idea.

I’ve turned most of my media players into presence and state aware devices. When media is playing and I leave the room the media automatically stops and resumes as soon as I return to the room. Here is how I did this and what you need on the hardware side:

Every of my rooms is equipped with a motion sensor. In my case:

- binary_sensor.hue_motion_flur
- binary_sensor.hue_motion_wohnzimmer
- binary_sensor.hue_motion_kueche
- binary_sensor.hue_motion_room2
- binary_sensor.hue_motion_schlafzimmer

So the first step to get hass to know in which room you currently are is to create a sensor that sets it’s state to the name of the room where the last motion was triggered. I use a custom integration named home-assistant-variables for this. It is avaiable in HACS. So here is the automation:

- alias: Set Last Motion Variable
  initial_state: True
  trigger:
    - platform: state
      entity_id:
        - binary_sensor.hue_motion_flur
        - binary_sensor.hue_motion_kueche
        - binary_sensor.hue_motion_room2
        - binary_sensor.hue_motion_schlafzimmer
        - binary_sensor.hue_motion_wohnzimmer
      to: 'on'
  action:
    - service: variable.set_variable
      data:
        variable: last_motion
        attributes_template: >
          {
            "history_1": "{{ variable.state }}",
            "history_2": "{{ variable.attributes.history_1 }}",
            "history_3": "{{ variable.attributes.history_2 }}"
          }
      data_template:
        value: >
          {% if trigger.entity_id == "binary_sensor.hue_motion_flur" %} Flur
          {% elif trigger.entity_id == "binary_sensor.hue_motion_kueche" %} Küche
          {% elif trigger.entity_id == "binary_sensor.hue_motion_room2" %} Room2
          {% elif trigger.entity_id == "binary_sensor.hue_motion_schlafzimmer" %} Schlafzimmer
          {% elif trigger.entity_id == "binary_sensor.hue_motion_wohnzimmer" %} Wohnzimmer
          {% endif %}

The next step is to create a template sensor that reads the state of the newly set variable and displays a corresponding mdi:icon that represents the rooms:

- platform: template
  sensors:
    last_motion:
      friendly_name: 'Wo bims?'
      value_template: "{{ states.variable.last_motion.state }}"
      icon_template: >-
        {% if is_state('variable.last_motion', 'Küche') %}
          mdi:food-variant
        {% elif is_state('variable.last_motion', 'Wohnzimmer') %}
          mdi:sofa
        {% elif is_state('variable.last_motion', 'Flur') %}
          mdi:cube-unfolded
        {% elif is_state('variable.last_motion', 'Room2') %}
          mdi:guitar-electric
        {% elif is_state('variable.last_motion', 'Schlafzimmer') %}
          mdi:hotel
        {% else %}
          mdi:account-question
        {% endif %}

Fine! Now you have a sensor that shows in which room you are currently in. Now comes the fun part. For the following example I’ll use my Kodi media_player (media_player.beelink) that is in my livingroom (Wohnzimmer in german). Here is the basic automation that automatically pauses and resumes Kodi based on room presence:

- alias: Kodi Auto Pause
  initial_state: True
  trigger:
    - platform: state
      entity_id: sensor.last_motion
  condition:
    - condition: state
      entity_id: input_boolean.besuchermodus
      state: 'off'
  action:
    - service_template: >
        {% if not is_state("sensor.last_motion", "Wohnzimmer") and is_state("media_player.beelink", "playing") %}
          media_player.media_pause
        {% elif is_state("sensor.last_motion", "Wohnzimmer") and is_state("media_player.beelink", "paused") %}
          media_player.media_play
        {% endif %}
      data:
        entity_id: media_player.beelink

As you can see I am using a condition based on an input_boolean called input_boolean.besuchermodus (Besucher means visitor/guest in german). This is a boolean I manually trigger when there are more than one person at home. I strongly advise you to do the same because if multiple persons are in your house they aren’t necessarly in one room and the automation would cause inconvenience if someone is watching a movie.

Have fun trying it out. If anyone has an idea how to replace the custom variable integration to create the last motion sensor please let me know. There is probably a better solution out there for this sensor.

6 Likes

I hope you sit super still while watching your movies or media lol. This is kinda silly.

Why would it matter whether he sits still or not? All it does is trigger the living room motion sensor again, making him still present in the living room.

I think it’s a neat idea and works well for when you’re living alone!
Thanks for sharing

3 Likes

Exactly. You can moove freely in the room where the movie is playing.