I have two template sensors that tell me who just got home less than 10 minutes ago and who has been home for longer than that. I would like to add a sensor that tells me who just left (i.e. is no longer home) within the last 10 minutes.
For example, when I arrive home after work my name shows up in the “people_just_arrived” sensor. Then 10 minutes later my name is removed from that sensor and shows up in the “people_already_home” sensor. Then when I go out for groceries I want my name to be removed from the “people_already_home” sensor and added to a new sensor “people_just_left”.
In my searches I haven’t been able to find a way to access the previous state or history of an entity.
Is there a way to accomplish the objective using only a template sensor, without creating any helpers or automations?
These are my existing sensors:
sensor:
- platform: template
sensors:
people_just_arrived:
friendly_name: 'People just arrived'
value_template: >
{% for person in states.person
if person.state_with_unit == 'home'
and (utcnow()-timedelta(minutes=10)) <= person.last_changed -%}
{%- if loop.first %}{% elif loop.last %} and {% else %}, {% endif -%}
{{ person.name -}}
{% if loop.last %}{% if loop.first %} is {% else %} are {% endif %}at {{person.state_with_unit}}.{% endif -%}
{%- endfor %}
people_already_home:
friendly_name: 'People already home'
value_template: >
{% for person in states.person
if person.state_with_unit == 'home'
and (utcnow()-timedelta(minutes=10)) > person.last_changed -%}
{%- if loop.first %}{% elif loop.last %} and {% else %}, {% endif -%}
{{ person.name -}}
{% if loop.last %}{% if loop.first %} is {% else %} are {% endif %}at {{person.state_with_unit}}.{% endif -%}
{%- endfor %}