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

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

  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

Leave a Reply

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