Useful Sensor: Motion Last Seen & Meta Motion Sensor

ConfigHome AssistantSmart HomeUseful Sensor

Written by:


You know what’s great about motion sensors? They are very, very cheap to build yourself. All it takes is an esp8266 module like the Wemos D1 Mini, some PIR sensors, and the ESP Easy firmware and you can have a bunch up and running in a few minutes. I haven’t DIYed a battery powered one yet, but there are plenty of great Z-Wave ones available.

Once you have a couple of motion sensors in your smart home, you can have Home Assistant track the last place it saw motion. This is a useful bit of info – useful as a condition for your automations, or as an input for a bayesian binary sensor. See below for YAML to create a meta-motion sensor with a history.

You need to install the variable custom component in order to use my exact yaml for this first part, otherwise you can use a different type to track the value like input_select or input_text. The advantage of using the variable component is we can save the history as attributes.

Pastebin link:


    value: 'Unknown'
    restore: true
      icon: mdi:map-marker
      name: "Last Motion"


# Update Last Motion variable
- alias: "Update Last Motion"
    - platform: state
      entity_id: binary_sensor.bathroom_motion, binary_sensor.closet_motion, binary_sensor.entry_motion_meta, binary_sensor.dining_motion_meta, binary_sensor.kitchen_motion_meta, binary_sensor.livingroom_motion_meta, binary_sensor.office_motion
      to: 'on'
    - service: variable.set_variable
        variable: last_motion
        attributes_template: >
              "history_1": "{{ variable.state }}",
              "history_2": "{{ variable.attributes.history_1 }}",
              "history_3": "{{ variable.attributes.history_2 }}"
        value: "{{ trigger.to_state.attributes.friendly_name }}"

Optionally, if you have multiple motion sensors in one room you can combine them into one meta-sensor using a template.


      friendly_name: 'Kitchen Motion'
      device_class: motion
      value_template: >-
        {%- if is_state("binary_sensor.kitchen_motion", "on")
        or is_state("binary_sensor.fridge_motion", "on")
        or is_state("binary_sensor.crawl_space_door", "on")
        {%- else -%}
        {%- endif %}

Pastebin link:

For an example of using this sensor as a condition in an automation, see #3 in Basic Node-Red Flows for Lighting.

Any binary sensor can be used, so you may want to include your door sensors as well. This opens up some useful possibilities with the history attributes of our variable.last_motion. If you have a defined route through your house – say walking in from the garage, through the entry door, to the kitchen – you can have Home Assistant detect this and react accordingly. If you need a longer history, simply adjust the update automation.

RELATED >>  Presence Detection Part 2: Improving Presence with Node-Red

3 Replies to “Useful Sensor: Motion Last Seen & Meta Motion Sensor”

  1. Skep says:

    hi Brad, what does your groups.yaml look like? I tried searching your github and couldn’t find it.

    I’ve used a lot of your stuff, but I don’t know how to present it in groups.yaml.

    Mind sharing that?

    Thanks again for all your help!

  2. […] Assistant controls the lights and keeps track of what room I was last in (using my useful Motion Last Seen […]

Leave a Reply

Your email address will not be published. Required fields are marked *