PIR - Motion Last Seen Sensor

I have Xiaomi wireless buttons the control the lights. They have 3 functions: single tap, double tap and long press. The below sets: single tap=ON, double tap=OFF, long press=AUTO.

kitchen_remote_button_1.yaml

alias: Kitchen Remote Button 1
initial_state: 'on'
trigger:
  - platform: event
    event_type: click
    event_data:
      entity_id: binary_sensor.switch_158d0002241b37
      click_type: single
  - platform: event
    event_type: click
    event_data:
      entity_id: binary_sensor.switch_158d0002241b37
      click_type: double
  - platform: event
    event_type: click
    event_data:
      entity_id: binary_sensor.switch_158d0002241b37
      click_type: long_click_press

action:
  - service: script.toggle_to_max
    data_template:
      entity_id: '{% if trigger.event.data.click_type == "single" %} light.kitchen_light_level {% endif %}'
  - service: automation.turn_off
    data_template:
      entity_id: '{% if trigger.event.data.click_type == "single" %} automation.kitchen_light_auto_on_motion {% endif %}'
  - service: automation.turn_off
    data_template:
      entity_id: '{% if trigger.event.data.click_type == "single" %} automation.kitchen_light_off_after_2mins {% endif %}'

  - service: light.turn_off
    data_template:
      entity_id: '{% if trigger.event.data.click_type == "double" %} light.kitchen_light_level {% endif %}'
  - service: automation.turn_off
    data_template:
      entity_id: '{% if trigger.event.data.click_type == "double" %} automation.kitchen_light_auto_on_motion {% endif %}'
  - service: automation.turn_off
    data_template:
      entity_id: '{% if trigger.event.data.click_type == "double" %} automation.kitchen_light_off_after_2mins {% endif %}'
light.kitchen_light_level {% endif %}'
  - service: automation.turn_on
    data_template:
      entity_id: '{% if trigger.event.data.click_type == "long_click_press" %} automation.kitchen_light_auto_on_motion {% endif %}'
  - service: automation.turn_on
    data_template:
      entity_id: '{% if trigger.event.data.click_type == "long_click_press" %} automation.kitchen_light_off_after_2mins {% endif %}'

kitchen_light_auto_on.yaml

alias: Kitchen light auto ON motion
initial_state: 'on'
trigger:
  - platform: state
    entity_id: binary_sensor.kitchen_multi_sensor_sensor
    to: 'on'
condition:      # look at changing this to use room light level only
  condition: or
  conditions:
  - condition: sun
    after: sunset
    after_offset: '-00:10:00'
  - condition: sun
    before: sunrise
    before_offset: '-01:00:00'
  - condition: template
    value_template: '{{ states.sensor.kitchen_multi_sensor_luminance.state | int < 10 }}'
action:
  service: light.turn_on
  data_template:
    entity_id: light.kitchen_light_level
    brightness: >
      {%- if now().strftime('%H')| int >= 21 %}
        25
      {%- elif now().strftime('%H')| int < 5 %}
        25
      {%- elif now().strftime('%H')| int >= 5 %}
        255
      {%- endif %}

kitchen_light_auto_off.yaml

alias: Kitchen light OFF after 2mins
initial_state: 'on'
trigger:
  - platform: state
    entity_id: binary_sensor.kitchen_multi_sensor_sensor
    to: 'off'
    for:
      minutes: 2
action:
  - service: homeassistant.turn_off
    entity_id: light.kitchen_light_level

script.toggle_to_max.yaml

toggle_light_to_max:
  sequence:
    - service: script.toggle_light_to_off
      data_template:
        light: "{{ light }}"
    - service: script.toggle_light_to_on
      data_template:
        light: "{{ light }}"

script.toggle_to_off.yaml

toggle_light_to_off:
  sequence:
    - condition: template
      value_template: '{{ is_state(light, "on") }}'
    - service: light.turn_off
      data_template:
        entity_id: "{{ light }}"

script.toggle_to_on.yaml

toggle_light_to_on:
  sequence:
    - condition: template
      value_template: '{{ is_state(light, "off") and (as_timestamp(now()) - as_timestamp(states.light[light|replace("light.","")].last_changed) > 3) }}'
    - service: light.turn_on
      data_template:
        entity_id: "{{ light }}"
        brightness: 255
2 Likes

I have PIR detectors with the Satel integra alarm (binary_sensor). Can I also determine the last move with them?

At the code:

>  sensor:
>    - platform: template
>         sensors:
>           template_last_motion:
>             friendly_name: 'Last Motion'
>             icon_template: 'mdi:walk'
>             entity_id:
>               - binary_sensor.biuro
>               - binary_sensor.natalka
>               - binary_sensor.niebieski
>             value_template: >
>               {%- set sensors = [binary_sensor.biuro, binary_sensor.natalka, binary_sensor.niebieski] %}
>               {% for sensor in sensors %}
>                 {% if as_timestamp(sensor.last_changed) == as_timestamp(sensors | map(attribute='last_changed') | max) %}
>                   {{ sensor.name }}
>                 {% endif %}
>               {% endfor %}

I get the message:
Could not render template Last Motion: UndefinedError: ‘binary_sensor’ is undefined. Please help

You forgot ‘states’ in front of sensor names in array:

 {%- set sensors = [states.binary_sensor.biuro, states.binary_sensor.natalka, states.binary_sensor.niebieski] %}
1 Like

I’ve got a problem with the following piece of YAML:

  {%- set deuren = [states.binary_sensor.0x00158d0001dcafe4_contact,states.binary_sensor.xiaomi_mijia_door02_contact, states.binary_sensor.xiaomi_mijia_door04_contact,states.binary_sensor.xiaomi_mijia_door05_contact,states.binary_sensor.xiaomi_mijia_door06_contact] %}
  {% for deur in deuren %}
    {% if as_timestamp(deur.last_changed) == as_timestamp(deuren | map(attribute='last_changed') | max) %}
      {{ deur.name }}
    {% endif %}
  {% endfor %}

If I run this piece of code in the dev-template tool I get this error:

Error rendering template: TemplateSyntaxError: expected token ',', got 'x00158d0001dcafe4_contact'

If I replace the the code with this:

  {%- set deuren = [states.binary_sensor['0x00158d0001dcafe4_contact'],states.binary_sensor.xiaomi_mijia_door02_contact, states.binary_sensor.xiaomi_mijia_door04_contact,states.binary_sensor.xiaomi_mijia_door05_contact,states.binary_sensor.xiaomi_mijia_door06_contact] %}
  {% for deur in deuren %}
    {% if as_timestamp(deur.last_changed) == as_timestamp(deuren | map(attribute='last_changed') | max) %}
      {{ deur.name }}
    {% endif %}
  {% endfor %}

The dev-tool gives the correct sensor. But if I put the code in my yaml file and restart hassio, the piece of code returns all (if triggered) but the states.binary_sensor[‘0x00158d0001dcafe4_contact’] sensor. This sensor is ignored if triggered.

Does anybody know what causes these behaviours?

This is brilliant - apart from the fact - as some here have already noted - that the “off/clear” message is seen as a form of activity. When it’s the reverse :joy:

Anyone solved this with a template?

Thanks!!

        {%- set pirs = [states.binary_sensor.bridget, states.binary_sensor.front_door_area, states.binary_sensor.kitchen] %}
        {% for pir in pirs %}
          {% if as_timestamp(pir.last_changed) == as_timestamp(pirs | map(attribute='last_changed') | max) %}
            {{ pir.name }}
          {% endif %}
        {% endfor %}

You could set off_delay: 1 in the binary sensor config in order to have the PIR sensor reset immediately after trigger so that last_changed represents the last motion trigger.

It would be a problem only if you tied the on state for the binary sensor to a specific action (such as keeping the light on) but it can be easily unbundled.

Hello,
none of the above methods shows me the last hour of activity.


#- platform: template
#  sensors:
#    template_last_motion:
#      friendly_name: 'Ostatni ruch klatka'
#      icon_template: 'mdi:walk'
#      entity_id:
#        - binary_sensor.klatka_gora_motion
#      value_template: >
#        {%- set sensors = [states.binary_sensor.klatka_gora_motion] %}
#        {% for sensor in sensors %}
#          {% if as_timestamp(sensor.last_changed) == as_timestamp(sensors | map(attribute='last_changed') | max) %}
#            {{ sensor.name }}
#          {% endif %}
#        {% endfor %}      
        
        
        
- platform: template
#  sensors:
#    template_last_motion:
#      friendly_name: 'Last Motion'
#      icon_template: 'mdi:walk'
#      entity_id:
#        - binary_sensor.klatka_gora_motion
#      value_template: >
#        {%- set sensors = [states.binary_sensor.klatka_gora_motion] %}
#        {% for sensor in sensors %}
#          {% if as_timestamp(sensor.last_changed) == as_timestamp(sensors | map(attribute='last_changed') | max) %}
#            {{ sensor.name }}
#          {% endif %}
#        {% endfor %}        
        
#  sensors:
#      template_last_motion:
#        friendly_name: 'Laatste Beweging'
#        value_template: >
#          {%- set pirs = [binary_sensor.klatka_gora_motion] %}
#          {% for pir in pirs %}
#            {% if as_timestamp(pir.last_changed) == as_timestamp(pirs | map(attribute='last_changed') | max) %}
#              {{ pir.name }} {{pir.last_changed}}
#            {% endif %}
#          {% endfor %}

what I’m doing wrong ?

I added the template sensor then realized that I can add the “last-changed” attribute to lovelace to see when the motion sensor was last changed.

I know this is an old thread, but wanted to pass this along in case anyone was looking for a lovelace based solution.

6 Likes