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.
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
Waw, thank you! Will try to figure out i have hassos.
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 Thank you again
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:
Unfortunately the variable_last_motions remains âNot setâ:
I checked tha automation and I believe it is triggered:
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
I used another custom integration. It had the same name but it didnât worked.
Yours works! thanks!
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
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.