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: https://pastebin.com/bCRPTtHY

configuration.yaml

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

automation.yaml

# Update Last Motion variable
- alias: "Update Last Motion"
  trigger:
    - 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'
  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: "{{ 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.

binary_sensors.yaml

    kitchen_motion_meta:
      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")
          -%}
        True
        {%- else -%}
        False
        {%- endif %}

Pastebin link: https://pastebin.com/bCRPTtHY

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 >>  Location Aware Notification Lights with Node-Red and IFTTT

16 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 […]

  3. Don says:

    “I haven’t DIYed a battery powered one”
    That seems to be the status quo across the hass webiverse. Those Zwave motion sensors are so expensive! I wish someone would post a DIY battery powered motion sensor design that utilized deep sleep for long battery life.

    • brad says:

      The reality is that WiFi just uses a lot of power. I have seen projects that use batteries but they only get, at most, a few dozen triggers before it drains it. Z-wave is designed for low power applications.

      • Donnie Fontaine says:

        Rob at TheHookup uses an ESP-01 (WiFi) for this…
        http://www.thesmarthomehookup.com/5-diy-wireless-mqtt-smart-home-window-sensors-updated
        He gets stellar battery life using a CR2032 battery.

        When I asked Rob about adapting his window sensor to a motion sensor, he said, “I’ve heard of people using a PIR sensor connected to the reset pin of an ESP-01 and using that to break the esp out of deepSleep whenever there is motion.“

        I understand that his window sensor is fully off (no deep sleep, no micro current draw) most of the time — but its also using a coin battery! If you used 3 NiMH AAA or even AA, couldn’t you potentially get good battery life using deep sleep?

        I would try this if I had anywhere near your knowledge/skills but I’m a total novice.

        • brad says:

          Interesting I do have some ESP-01s laying around. It makes sense to me to connect the PIR to the reset pin. I wonder what kind of delay there is for it to boot up, connect to WiFi, and report the sensor state. Seems like it would be too much of a delay for motion to be useful but I haven’t played around much with booting these cold.

    • Brian H says:

      The answer you seek is Xiaomi Motion Sensors they run on button cell batteries and last months. They can be had for as low $10.99 on Aliexpress. You can either use a Xiaomi Hub (but not the new one with Homekit support), or Zigbee2MQTT. I personally use their Hub because it just works.

      I have Window/Door sensors, Motion sensors, (buttons to log the last time someone took their medicine), Leak Sensors under our sinks, and I recently ordered some Humidity sensors so I can control the bathroom fan that nobody ever remembers to turn off. I even have Xiaomi Plant sensors in the garden (those use Bluetooth Low Energy though).

  4. Jake says:

    Hi Brad,

    I used this code and in fact I copied it, so I have 2 versions of it running on my setup. It was working as ‘last motion’ and I don’t know if I ever looked at the last 3 history or not at that time. I added the second copy and changed the sensors to all of my burglar alarm sensors so I now have ‘last alarm sensor’ as well. The ‘last 3’ history works on the alarm sensor, but not the motion.

    I copy/pasted both of them into text files and ran them through a file comparison tool, and the only things that are different are the names and sensors, yet they behave differently.

    Any thoughts on how to troubleshoot?

    Thanks for the great ideas you have shared.

    • brad says:

      That’s strange, I am not sure. I would check your logs to see if there are any clues. Maybe raise an issue with the author of the variable component.

      I just upgraded to HA 0.81.6 and am having no issues with the history.

  5. Jake says:

    Hi Brad,

    I hadn’t checked in a while, assuming it was still broken, and now it shows up as expected. No explanation. Oh well, the magic of automation….

    Jake

  6. Mike Stewart says:

    is the variable custom component still working in v 0.84.1 I’ve upgraded and i get an error in my config that the variable component is not found

  7. Hans Van der Drift says:

    Brad,

    Just love your posts, insightful and well structured for the medium amount of knowledge I have.

    Would you have an example of this for me?

    “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.”

    I have been thinking about this and would like to make automation with exactly this logic.

    I am unable to translate the idea into a node red flow…

  8. Peter says:

    Hi. A very nice idea.
    I would like to know how to make such a nice card, where I have the last move, an hour, and underneath the history in the form of icons as you presented at the beginning of the article?

    In lovelance i can give:

    – type: glance
    title: Ostatni ruch
    show_header_toggle: false
    entities:
    – variable.last_motion

    I’m a beginner. 🙂 please help 🙂

  9. Patrick says:

    Hi Brad,

    Thanks for your simple explanations and great resources from your site ! I have one question, I set up up your flow for last presence seen and i’m trying to figure out how to Use the history in nodered. The reason is simple. I use the motion last seen to trigger an input boolean presence office, or presence kitchen etc for each room. Now the problem is that we are now two working from home( Yes here in quebec it has been six weeks of confinement who nows how long left …) , so if last presence detected was in the office, my kitchen input boolean turns off, then all my automations are shut out since HAss thinks there is only someone in the office. I have been trying to use the History of variable last motion and cant it to work, how would you read the history in nodered. I hope my long story is clear, and thanks for a response in advance.

    Cheers,

Leave a Reply

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