which shows the time when the last time the state changed, I want first time some one was detected home and the the last time when they left i.e detected ‘away’ in a day. Is it possible? Thanks in advance
template:
- trigger:
- platform: state
entity_id: person.a
to:
- home
not_from:
- unknown
- unavailable
sensor:
- name: First Arrived Home
device_class: timestamp
state: |
{% set current = this.state | as_datetime if this is defined else now() %}
{{ current if now().date() == current.date() else now() }}
- trigger:
- platform: state
entity_id: person.a
not_to:
- unknown
- unavailable
from:
- home
sensor:
- name: Left Home Time
device_class: timestamp
state: "{{ now() }}"
- trigger:
- platform: time
at: "23:59:59"
sensor:
- name: Last left home
device_class: timestamp
state: "{{ states('sensor.left_home_time') }}"
As Troon explained the trio of sensors will be unknown until each of their triggers occur. There is likely a way to get the information using a SQL query but that’s outside my wheelhouse…
okay, I waited for state to change so that it can record the time, but as soon as the state changed to home, it went from unknown to unavailable.
Note that i tweaked the code little bit and have removed the not_from and not_to parts as my state often goes to unknown (I’m using HA container and it happens when the pod restarts). So that it can record time when person becomes home, doesn’t matter if its transitioned from unknown or away state. Here’s my code:
template:
- trigger:
- platform: state
entity_id: person.khushal_mer
to:
- home
sensor:
- name: Khushal Arrived
device_class: timestamp
state: |
{% set current = this.state | as_datetime if this is defined else now() %}
{{ current if now().date() == current.date() else now() }}
- trigger:
- platform: state
entity_id: person.khushal_mer
not_to:
- home
sensor:
- name: Khushal Left Time
device_class: timestamp
state: "{{ now() }}"
- trigger:
- platform: time
at: "23:00:00"
sensor:
- name: khushal left
device_class: timestamp
state: "{{ states('sensor.khushal_left_time') }}"