Last time motion was detected (not last changed nor cleared)

How can I get the time from the last motion detection. Not last changed nor cleared.

1 Like

Last changed AND state on.

If I may join in here:

I am currently solving the problem with a “dummy” automation and a template sensor, which is working fine. This way I can output the attribute last_triggered as a timestamp.

Although I have now tried countless combinations, it is unfortunately still not clear to me how I can generate a timestamp using your solution.


"{{ states.binary_sensor.bewegungsmelder_garderobe.last_changed and is_state('binary_sensor.bewegungsmelder_garderobe', 'on') }}"

will give me True or False - so far so good.

But


"{{ as_timestamp(states.binary_sensor.bewegungsmelder_garderobe.last_changed) and is_state('binary_sensor.bewegungsmelder_garderobe', 'on')) }}"

is doing the same. I don‘t get it.

Try this:

sensor:
  - platform: template
    sensors:
      last_movement:
        friendly_name: "Last Movement"
        value_template: "{{ as_timestamp(states.binary_sensor.bewegungsmelder_garderobe.last_changed) if is_state('binary_sensor.bewegungsmelder_garderobe', 'on') else states('sensor.last_movement') }}"

Or written to be more easily read:

sensor:
  - platform: template
    sensors:
      last_movement:
        friendly_name: "Last Movement"
        value_template: >
          {% if is_state('binary_sensor.bewegungsmelder_garderobe', 'on') %}
            {{ as_timestamp(states.binary_sensor.bewegungsmelder_garderobe.last_changed) }}
          {% else %}
            {{ states('sensor.last_movement') }}
          {% endif %}

Or you could use the newly introduced trigger based sensors:

template:
  - trigger:
      - platform: state
        entity_id: binary_sensor.bewegungsmelder_garderobe
        to: 'on'
    sensor:
      - name: "Last Movement"
        state: "{{ as_timestamp(states.binary_sensor.bewegungsmelder_garderobe.last_changed) }}"
10 Likes

It’s awesome, I wouldn’t have thought of that in a million years!
Thank you very much.

And as soon as you can place the new sensors in packages, I’ll try that out too, thanks for pointing that out.

Hi Guys,

I’ve have a motion sensor which become unavailable occasionally, I want to see when was last time this sensor has detected motion.

If I use the solution provided by @tom_l I’m able to get the time stamp when it was last detected, But I want to see the value as 1 hours ago or 1 hours 15 mins ago instead or time stamp. (Just like how it shows in {{ relative_time(states.binary_sensor.pir.last_changed) }})

Can any one help me how can I do this.

FYI I tried the following but it is not working

- platform: template
  sensors:
    couch_last_movement:
      friendly_name: Couch Last Occupied
      value_template: >
        {% if states('binary_sensor.pir') in [ 'on', 'off' ] %}
          {{ relative_time(states.binary_sensor.pir.last_changed) }}
        {% else %}
          {{ states('sensor.couch_last_movement') }}
        {% endif %}

That looks fine to me. What does the template editor tell you?

Old post but to finish it off the state returned is I think correct but maybe not readable for me or I did something wrong:

  - trigger:
      - platform: state
        entity_id: binary_sensor.philips_motion_sensor_4_occupancy
        to: 'on'
    sensor:
      - name: "Last Movement"
        state: "{{ as_timestamp(states.binary_sensor.philips_motion_sensor_4_occupancy.last_changed) | timestamp_custom('%H:%M') }}"

You can see my minimal skills are trying to pipe it to something readable but I know this isnt correct.
The state returned with or without my experiment is

1678373802.119898

Can someone point me in the right direction most of the information I am finding show time returned that just has too much information like year that people what parsed out. I am not sure if that state is even a form of time


- trigger:
    - platform: state
      entity_id: 
      - binary_sensor.eingangstur
      from: 
      - 'off'
      - 'on'
      to: 
      - 'on'
      - 'off'
  sensor:
  - name: "Eingangstür {{ 'geschlossen' if trigger.to_state.state == 'off' else 'geöffnet' }}"
    unique_id: status_eingangstuer
    state: "{{ now().strftime('%T') }}"
    icon: mdi:door-{{ 'closed' if trigger.to_state.state == 'off' else 'open' }}

computes:

Bild

https://strftime.org/

Thanks for the initial work here; however, I noticed that (at least in my case) the logic for the last change is incorrect.
I wanted to use a template sensor to store when we have the last motion in a room; however, if you enter the room at the moment T0, then the value from the last_changed attribute is the correct one. My motion sensor is configured to report every 10 seconds. So, at the moment in time, T0+10s, if I move the value from last_changed, it will remain T0 and for me, this causes issues.

How I setup this sensor is:

    - name: "0.BJ last-motion"
      unique_id: 'sensor.0_bj_last_motion'
      icon: mdi:motion-sensor
      state: >
        {% set motionSensor = 'binary_sensor.0_bj_motion_occupancy' %}
        {% if is_state(motionSensor, 'on') %}
          {% set last_changed = as_timestamp(states[motionSensor].last_changed) %}
          {% set now_ts = as_timestamp(now()) %}
          {% if last_changed+10<now_ts %}
            {{ now_ts }}
          {% else %}
            {{ last_changed }}
          {% endif %}
        {% else %}
          {{ states('sensor.0_bj_last_motion') }}
        {% endif %}

even so, I’m not fully happy. I would have loved to do it based on triggers; basically, when attribute last_updated from ‘binary_sensor.0_bj_motion_occupancy’ sensor changes, then execute my logic.
Unfortunately, I tried such a trigger in automation, and it didn’t trigger, secondly, the documentation is useless as I do not know where to write the code and have the right indentation.

I’m relatively new to HA, so I have problems with replies like this. The code is self-explanatory, but nobody ever says where this code goes. Does this go in the yaml config of the device? Or does it go in configuration.yaml? Or somewhere else?

The way I wrote it the config goes in configuration.yaml.

It is a very old topic. Be careful, a lot can be out of date.

The best way to understand where it goes is to look up the integrations in the documentation.

1 Like