Time last seen motion

Hi,

I’m moving from OpenHAB2 to Home Assistant and can’t find how to display the last seen motion of a motion sensor on the frontend.
I have 3 motion sensors (aeotec multisensor 6) working under Home Assistant but can’t figure out how print the date and time of the last detected event.

Pulled from @pkozul floorpan setup:

template_last_motion:
  friendly_name: 'Last Motion'
  value_template: >
    {%- set sensors = [states.binary_sensor.garaged_sensor_6_0, states.binary_sensor.backd_sensor_7_0, states.binary_sensor.frontd_sensor_5_0, states.binary_sensor.multisensor1_sensor_9_0, states.binary_sensor.server_sensor_18_0, states.group.bedroom_lights, states.group.ceiling_fan_light, states.group.dining_room, states.group.kitchen, states.light.back_porch_light, states.light.bedroomlamp_level_17_0, states.light.front_porch_light, states.light.hall_light_1, states.light.hall_light_2, states.light.lrlamp_level_14_0, states.light.tablelamp_level_15_0] %}
    {% for sensor in sensors %}
      {% if as_timestamp(sensor.last_changed) == as_timestamp(sensors | map(attribute='last_changed') | max) %}
        {{ sensor.name }}
      {% endif %}
    {% endfor %}

Change the sensors listed to match yours then add sensor.template_last_motion into your configuration.

If I’m reading this correctly, this shows the last sensor that saw motion.

Incidentally, my Zwave motion sensors don’t have a last_changed attribute (or similar attribute) on the states page. I get a weird time (12:44) for last_changed, not an error. But I get the same 12:44 for all of my sensors.

But I don’t have Aeotecs. But my guess is that Zwave motion sensors are going to report the same.

I have this set up to show ‘last device’ at the moment. I don’t have too many motion sensors integrated at the moment but will soon (thanks to @bruhautomation). If you only include your motion sensors, it should show you the last one triggered.

You should be able to play with {{ sensor.name }} (maybe to sensor.last_changed or something similar.

OR… Use this to generate the sensor.last_changed and then extract it per sensor. tt may require using this as a template per sensor such as:

  friendly_name: 'Kitchen Sensor'
  value_template: >
    {%- set sensors = [states.kitchen_sensor_0] %}
    {% for sensor in sensors %}
      {% if as_timestamp(sensor.last_changed) == as_timestamp(sensors | map(attribute='last_changed') | max) %}
        {{ sensor.name }}
      {% endif %}
    {% endfor %}

Not sure… not my strongest area.

I found a way to do it.
First:
I have enabled mqtt and use it to store the time that a motion sensor changes state from ‘off’ to ‘on’ the following rule in automations.yaml

  • id: ‘1495702442415’
    alias: Store Motion Sensor0
    trigger:
    • platform: state
      entity_id: binary_sensor.aeotec_zw100_multisensor_6_sensor_4_0
      from: ‘off’
      to: ‘on’
      action:
    • service: mqtt.publish
      alias: MQTT store sensor0 ON time
      data:
      payload_template: ‘{{as_timestamp(states.binary_sensor.aeotec_zw100_multisensor_6_sensor_4_0.last_changed) | timestamp_custom("%d-%m-%y %H:%M:%S")}}’
      topic: “hass/storage/motion/on_time/sensor0”

Second:
I have created a mqtt sensor that listens to the above topic by adding following sensor in the file sensors.yaml

  • platform: mqtt
    name: “Last Motion Sensor0”
    state_topic: “hass/storage/motion/on_time/sensor0”
5 Likes

Very clever.

I got my BRUH Multisensors working together with these few changes. Thank you for your code!

automation:
  - alias: Store Motion Sensor1
    trigger:
      - platform: state
        entity_id: sensor.sn1_pir
        from: 'standby'
        to: 'motion detected'
    action:
      - service: mqtt.publish
        alias: MQTT store sensor1 ON time
        data:
            topic: 'hass/storage/motion/on_time/sensor1'
            payload_template: '{{ as_timestamp(states.sensor.sn1_pir.last_changed) | timestamp_custom("%d-%m-%y %H:%M:%S") }}'
            retain: 'true'
  - alias: Store Motion Sensor2
    trigger:
      - platform: state
        entity_id: sensor.sn2_pir
        from: 'standby'
        to: 'motion detected'
    action:
      - service: mqtt.publish
        alias: MQTT store sensor2 ON time
        data:
            topic: 'hass/storage/motion/on_time/sensor2'
            payload_template: '{{ as_timestamp(states.sensor.sn2_pir.last_changed) | timestamp_custom("%d-%m-%y %H:%M:%S") }}'
            retain: 'true'

And

sensor:
  #Listen for MQTT topic: Automations will timestamp the PIR/motion sensors that go off
  - platform: mqtt
    name: "Last Motion Sensor1"
    state_topic: "hass/storage/motion/on_time/sensor1"
  - platform: mqtt
    name: "Last Motion Sensor2"
    state_topic: "hass/storage/motion/on_time/sensor2"

Though, now I’m trying to do something different (like this forum topic) and figure out how to parse the most recent timestamp of three sensors that saw motion. I only have two to play with right now, but will build a third soon:

sensor:
  - platform: template
    sensors:
      last_sensor_with_motion:
        friendly_name: 'Last sensor with motion:'
        #values are all stored in as_timestamp() form, so can be compared (???)
        value_template: >-
          {%- if states.sensor.last_motion_sensor1.state > states.sensor.last_motion_sensor2.state %}
              SN1
          {%- elif states.sensor.last_motion_sensor1.state < states.sensor.last_motion_sensor2.state %}
              SN2
          {% else %}
              Unknown
          {%- endif %}

I could do a bunch of greater than compares, but I’d like this to be scalable if I add more sensors later. It would also be nice to be able to know which last two sensors went off (for a thermostat controller I’d like to build). I’ll update the other thread with code if I make some progress.

2 Likes

i have 3 of BRUH multi sensor will you help what are the changes i have to make if i have 3 PIR sensors? noob here.

just help me out with this if i have 3 sensors please.

I’m also stuck on this. Someone already know how to compare timestamps for 3 or more sensors?

I kludged this for my thermostat project:

I had automations monitor ‘from no_motion to motion’ changes on each sensor. Whenever that event happened for a given sensor, each sensor’s automation would store the name of its sensor into the same common input_select box. Another automation would check the input_select box to read the name of the latest sensor that went off.

Code here:

Thanks for your help. I’ll take a look.

Hey, I stumbled across this post that might have the answer:

This is the same code recommended by the “Floor Plan” custom addon