How to show last motion movement [LIST]?

I have several motion sensors and i would like to have a list, last detected motion lets say last 10 and updated when somebody trigger the sensor.
Screenshot_114

2 Likes

I have this done by first installing “hass-variables” custom integration thru HACS.

then I configure the variable:

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

then create the updated variable with an automation:

- alias: 'AS Update Last Motion'
    initial_state: 'on'
    trigger:
      - platform: state
        entity_id:
          - binary_sensor.comp_rm_motion_template
          - binary_sensor.mbr_motion
          - binary_sensor.spare_bedroom_motion
        to: 'on'
      - platform: state
        entity_id:
          - sensor.computer_room_camera_motion
          - sensor.livingroom_camera_motion
          - sensor.diningroom_camera_motion
          - sensor.garage_camera_motion
          - sensor.kitchen_camera_motion
          - sensor.deck_camera_motion
        to: 'Detected'
    action:
      - service: variable.set_variable
        data:
          variable: last_motion
          attributes:
            history_1: "{{ states('variable.last_motion') }}"
            history_2: "{{ state_attr('variable.last_motion','history_1') }}"
            history_3: "{{ state_attr('variable.last_motion','history_2') }}"
            history_4: "{{ state_attr('variable.last_motion','history_3') }}"
            history_5: "{{ state_attr('variable.last_motion','history_4') }}"
            history_6: "{{ state_attr('variable.last_motion','history_5') }}"
            history_7: "{{ state_attr('variable.last_motion','history_6') }}"
            history_8: "{{ state_attr('variable.last_motion','history_7') }}"
            history_9: "{{ state_attr('variable.last_motion','history_8') }}"
            history_10: "{{ state_attr('variable.last_motion','history_9') }}"
      - service: variable.set_variable
        data:
          variable: last_motion  
          value: >
            {{ trigger.to_state.attributes.friendly_name }} : {{ as_timestamp(trigger.to_state.last_changed)| timestamp_custom('%x, %X') }}

finally I use a custom “entity-attributes” card available in HACS to display the list:

  - type: entities
    title: Motion Sensor History
    show_header_toggle: false
    state_color: true
    entities:
      - type: custom:entity-attributes-card
        heading_name: Name
        heading_state: State
        entity: variable.last_motion
        filter:
          include:
            - variable.last_motion.history_1
            - variable.last_motion.history_2
            - variable.last_motion.history_3
            - variable.last_motion.history_4
            - variable.last_motion.history_5
            - variable.last_motion.history_6
            - variable.last_motion.history_7
            - variable.last_motion.history_8
            - variable.last_motion.history_9
            - variable.last_motion.history_10

ex

3 Likes

Waw, thank you! Will try to figure out i have hassos.

:frowning:

2021-05-08 22:20:22 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'dict object' has no attribute 'to_state' when rendering '{{ trigger.to_state.attributes.friendly_name }} : {{ as_timestamp(trigger.to_state.last_changed)| timestamp_custom('%x, %X') }}'
2021-05-08 22:20:22 ERROR (MainThread) [homeassistant.components.automation.lastmotionstate] lastmotionstate: Error executing script. Error for call_service at pos 2: Error rendering data template: UndefinedError: 'dict object' has no attribute 'to_state'
2021-05-08 22:20:23 ERROR (MainThread) [homeassistant.components.automation.lastmotionstate] Error while executing automation automation.lastmotionstate: Error rendering data template: UndefinedError: 'dict object' has no attribute 'to_state'

Ok now its working, the error apear when i did’nt yet filled all the history :slight_smile: Thank you again :slight_smile:

1 Like

So far I try to use the same ‘mechanism’ but unfortunately it doesn’t work.

Firstly, I couldn’t install the custom integration ‘variables’, HA won’t restart. Finally after searching I found I had to add version to manifest.json.

In my configuration.yaml I have:

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

My automation.yaml contains:

- alias: 'Motion Update'
#  initial_state: 'on'
  trigger:
  - platform: state
    entity_id:
      - binary_sensor.motionsensor_boven_occupancy
      - binary_sensor.motionsensor_garage_occupancy
      - binary_sensor.motionsensor_keuken_occupancy
      - binary_sensor.motionsensor_hall_occupancy
    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 }}",
            "history_4": "{{ variable.attributes.history_3 }}",
            "history_5": "{{ variable.attributes.history_4 }}",
            "history_6": "{{ variable.attributes.history_5 }}",
            "history_7": "{{ variable.attributes.history_6 }}",
            "history_8": "{{ variable.attributes.history_7 }}",
            "history_9": "{{ variable.attributes.history_8 }}",
            "history_10": "{{ variable.attributes.history_9 }}"
          }
      data_template:
        value: "{{ trigger.to_state.attributes.friendly_name }} : {{ as_timestamp(trigger.to_state.last_changed)| timestamp_custom('%X') }}"

It seems ‘initial_state: ‘no’’ is not accepted. I get an error ‘suspected boolean’. For this reason I made a comment line for it. Maybe this is my missing part…

As you see in my automation I have 4 motion sensors. When motion is detected the state goes from ‘off’ to ‘on’, see check in Developer Tools:
Schermafbeelding 2021-10-15 om 18.47.26

Unfortunately the variable_last_motions remains ‘Not set’:
Schermafbeelding 2021-10-15 om 18.48.17

I checked tha automation and I believe it is triggered:
Schermafbeelding 2021-10-15 om 18.58.33

Any idea what’s wrong or missing?
Thanks for your help anyway!

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

Automation:

alias: lastmotionstate
trigger:
  - platform: state
    entity_id:
      - binary_sensor.krb_pir
      - binary_sensor.bazen_pir
      - binary_sensor.garaz_pir
      - binary_sensor.jidelna_pir
      - binary_sensor.kuchyn_pir
      - binary_sensor.loznice_pir
      - binary_sensor.obyvak_pir
      - binary_sensor.pracovna_pir
      - binary_sensor.st_pokoj_pir
      - binary_sensor.telocvicna_pir
      - binary_sensor.vstup_pir
      - binary_sensor.zadni_vstup_pir
      - binary_sensor.dvere_vstup_mag
      - binary_sensor.vrata_garaz_mag
      - binary_sensor.zadni_dvere_mag
    to: 'on'
action:
  - service: variable.set_variable
    data:
      variable: last_motion
      attributes:
        history_1: '{{ states(''variable.last_motion'') }}'
        history_2: '{{ state_attr(''variable.last_motion'',''history_1'') }}'
        history_3: '{{ state_attr(''variable.last_motion'',''history_2'') }}'
        history_4: '{{ state_attr(''variable.last_motion'',''history_3'') }}'
        history_5: '{{ state_attr(''variable.last_motion'',''history_4'') }}'
        history_6: '{{ state_attr(''variable.last_motion'',''history_5'') }}'
        history_7: '{{ state_attr(''variable.last_motion'',''history_6'') }}'
        history_8: '{{ state_attr(''variable.last_motion'',''history_7'') }}'
        history_9: '{{ state_attr(''variable.last_motion'',''history_8'') }}'
        history_10: '{{ state_attr(''variable.last_motion'',''history_9'') }}'
  - service: variable.set_variable
    data:
      variable: last_motion
      value: >
        {{ trigger.to_state.attributes.friendly_name }} : {{
        as_timestamp(trigger.to_state.last_changed)| timestamp_custom('%x, %X')
        }}
initial_state: 'on'
mode: single

Working for me

1 Like

I used another custom integration. It had the same name but it didn’t worked.
Yours works! thanks!

1 Like

Hi,

I have this up and running (I think). I can’t seem to locate any record/reference to the history_1, history_2 states…

I was wondering how I actually locate/use the “history_n” variables.

if you wouldn’t know how to use that, you might not need it :wink:

with the same triggers, you could probably live with the core trigger template sensor, and dispense with the CC variable, which is against Core dev teams advice anyways:

template:

  - trigger:
      - platform: state
        entity_id:
          - binary_sensor.laundry_sensor_motion
          - binary_sensor.frontdoor_sensor_motion
          - binary_sensor.library_sensor_motion
etc etc
        to: 'on'
    sensor:
      - unique_id: last_motion_sensor_triggered
        name: Last motion sensor triggered
        icon: mdi:motion-sensor
        state: >
          {{trigger.to_state.name.split(' sensor Motion')[0]}}
        attributes:
          entity_id: >
            {{trigger.entity_id}}

add it to you recorder settings and you will get an officially supported list of last triggered’s

Look in your states page in dev tools for the entity “variable.last_motion” and then look in the attributes column.