I have 6 entities, from which, I need to determine the most recent and least recent to change.
Using {{ (as_timestamp(states.switch.entity_1)) }}, I get a float returned.
I’m figuring that creating an array along these lines
{{ [as_timestamp(states.switch.entity_1.last_changed, as_timestamp(states.switch.entity_2.last_changed, as_timestamp(states.switch.entity_3.last_changed...etc] | min }}
it should be possible to look in the list for the highest or lowest value and return the entity.
But, I don’t know how do that yet.
Ultimately, I want two sensors that always return the entity that changed most recently and least recently.
Will the sensors auto update? If so, how frequently?
Any pointers?
Only if you were ok with limiting it to entities whose state is currently “on”… Otherwise, I think you would need to use a SQL sensor or use an automation to save the different times to helpers.
What do you mean by that? That does sound like what I need.
I’m trying to work out a queing and priority system for the entities to ensure that they all get a share of ‘on’ time when they need it. It’s to smooth out the high demand entities load on a generator and I’m only really interested in the on state.
The template above cannot differentiate whether the event that changed its last_changed value was turning ‘on’ or ‘off’. The only simple way to be sure that it is the “turn on” that is responsible for the current last_changed value in a template is to only look at entities that are currently “on”. That can be done by adding a filter to the template as follows:
# Most Recently turned on of the ones that are currently 'on'
{{ expand(('switch.entity_1','switch.entity_2','switch.entity_3'...etc) | select('is_state', 'on'))
| sort(attribute= 'last_changed', reverse=true) | map(attribute ='entity_id')
| first}}
# Least Recently turned on of the ones that are currently 'on'
{{ expand(('switch.entity_1','switch.entity_2','switch.entity_3'...etc) | select('is_state', 'on'))
| sort(attribute= 'last_changed') | map(attribute ='entity_id')
| first }}
Any of the switches that are currently “off” would have had their last_changed set when they were turned “off”, so the only way to know when they were last turned “on” would be to query the state history with a SQL sensor or set up a helper to track that data. If you need to do that (and you don’t feel like dealing with SQL) there is a custom integration available in HACS called Variables + History.
EDIT: Updated template to use is_state to reduce number of entities before expand().
I’m sorry to post a reply on this old topic, but this is almost exactly the same problem as I’m trying to solve.
I have a cheap Chinese door contact sensor. It creates 2 sensors: one for open, one for closed. So for example when I open the door, the ‘door open’ sensor will get a ‘on’ command. When I close the door, the ‘door close’ sensor will get a on command.
Using the template you posted above I have been able to create a template sensor which lists the entity that was changed most recently.
Can we also change this template to produce ‘on’ of ‘off’, depending on which sensor was most recently updated?
When I paste the above code in my template.yaml file, I get the error below:
Error loading /config/configuration.yaml: while parsing a block mapping
in "/config/subconfig/template.yaml", line 1, column 1
expected <block end>, but found '<block sequence start>'
in "/config/subconfig/template.yaml", line 73, column 2
Do I need to paste this somewhere else, or put a part of it somewhere else?
The configuration I posted is set up as if it were being placed in configuration.yaml (as is the convention on this forum). If you are placing it somewhere else, you need to share details about how you have structured your configuration such as which include method are you using for your template.yaml file.