Hi,
looking for a way to see list of 10 last breached motion sensors/ doors/ windows.
Any idea how to achieve it best way in HA?
best regards,
Michal
Hi,
looking for a way to see list of 10 last breached motion sensors/ doors/ windows.
Any idea how to achieve it best way in HA?
best regards,
Michal
I use a variable custom integration available in HACS (hass-variables).
variable:
last_motion:
value: 'Not set'
restore: true
attributes:
icon: mdi:map-marker
friendly_name: 'Last Motion'
then for the automation I use this:
- alias: 'AS Update Last Motion'
initial_state: 'on'
mode: parallel
trigger:
- platform: state
entity_id:
- binary_sensor.computer_room_camera_motion
- binary_sensor.deck_camera_motion
- binary_sensor.diningroom_camera_motion
- binary_sensor.front_entrance_camera_motion
- binary_sensor.garage_camera_motion
- binary_sensor.kitchen_camera_motion
- binary_sensor.livingroom_camera_motion
- binary_sensor.rear_parking_camera_motion
- binary_sensor.upper_hall_camera_motion
- binary_sensor.front_entrance_camera_motion_cam
- binary_sensor.front_porch_motion
- binary_sensor.computer_room_motion_sensor
- binary_sensor.garage_motion
- binary_sensor.kitchen_door_motion
- binary_sensor.main_hallway_motion
- binary_sensor.mbr_motion
- binary_sensor.spare_bedroom_motion
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') }}
and this is the lovelace card to display it:
- type: entities
title: Motion Sensor History
show_header_toggle: false
state_color: true
entities:
- variable.last_motion
- 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
The following Trigger-based Template Sensor records the name and time when a monitored binary_sensor changes state to on
. It keeps the ten most recent events.
template:
- trigger:
- platform: state
entity_id:
- binary_sensor.front_door
- binary_sensor.kitchen_window
- binary_sensor.garden_gate
from: 'off'
to: 'on'
sensor:
- name: Recent Openings
state: "{{ now().timestamp() | timestamp_custom() }}"
attributes:
openings: >
{% set current = this.attributes.get('openings', []) %}
{% set new = [{
"name": trigger.to_state.name,
"time": now().isoformat() }] %}
{{ (new + current)[:10] }}
You can display the recorded events using a Markdown card with the following template:
|Name||Time|
|:----|:-:|:----|
{% for x in state_attr('sensor.recent_openings', 'openings') | default([], true) -%}
|{{x.name}}||{{x.time|as_timestamp|timestamp_custom}}|
{% endfor -%}
Correction. Convert time string to timestamp before submitting to timestamp_custom
filter.
@123 What a great solution/alternative you have provided. It would take me days or weeks to archieve this kind of result. So, thank you so much for this!
Now, on the template used to list the sensors changed I think there is need to make this change:
{{as_timestamp(x.time) | timestamp_custom}}
instead of:
Otherwise it will return None and the timestamp_custom cannot handle it.
Thanks for bringing it to my attention. I have corrected the example posted above.
Thank you for sharing this awesome stuff
I leave here my sensor config that can be usefull for somebody:
- trigger:
- platform: state
entity_id:
- binary_sensor.visonic_z01
- binary_sensor.visonic_z02
- binary_sensor.visonic_z0x
from: 'off'
to: 'on'
sensor:
- name: 'alarm_sensors_last_change_state'
unique_id: alarm_sensors_last_change_state
icon: mdi:shield-refresh-outline
state: "{{ now().timestamp() | timestamp_custom() }}"
attributes:
on_state: >
{% set current = this.attributes.get('on_state', []) %}
{% set new = [{
"entityid": trigger.to_state.entity_id,
"entityname": trigger.to_state.name,
"entityicon": trigger.to_state.attributes.icon,
"time": now().isoformat() }] %}
{{ (new + current)[:20] }}
And the Markdown card using the sensor icon as a link to the history:
type: markdown
content: >-
|Sensor Zone| |Last Time Triggered| |<ha-icon icon=mdi:shield-refresh-outline></ha-icon> |
|:----------|:-|:------------------|:-|:----------------------------------------------------|
{% for x in state_attr('sensor.alarm_sensors_last_change_state', 'on_state') |
default([], true) -%}
|{{x.entityname}}||{{as_timestamp(x.time) | timestamp_custom('%T - %d/%m/%Y')}}||[<ha-icon icon {{x.entityicon}}></ha-icon>](/history?entity_id={{x.entityid}})|
{% endfor -%}
And the final result:
In the future I will try to have just one single entry per sensor (the last one) but I fear thar my Jinja knowledge it is not advanced enough…
Thank you for sharing.
What would be nice is to count the movement when it occurs on the same sensor that would look like this:
Sala De Estar (2) 23:03:17
Hall Piso (1) 23:00:00
Sala De Estar (4) 23:00:14
Hall Piso (1) …
Sotao (1) …
That way you can keep a longer history espcialy when there are lots of motion in one room.
Any idea how to achieve this?