Hello everyone,
I would like to make a sensor that keeps track of which lamp was last turned on and off.
I’ve tried everything, but it won’t work.
Can anyone help me with this?
Thanks for every response.
Hello everyone,
I would like to make a sensor that keeps track of which lamp was last turned on and off.
I’ve tried everything, but it won’t work.
Can anyone help me with this?
Thanks for every response.
Can you clarify what you want? Does current state matter?
For example:
If lights turn on in order A, B, C then turn off in order C, A… should the sensor return C because it was the last turned on or B because it was the last turned on that is still on?
The following will give you the last light that was turned on, that is still on:
template:
- sensor:
- name: "Last Light On"
unique_id: last_light_on_001
state: |
{{ states.light | selectattr('state', 'eq', 'on') | sort(attribute = 'last_changed', reverse=1 )
| map(attribute='entity_id') | first }}
attributes:
count: "{{states.light | selectattr('state', 'eq', 'on') | list | count}}"
availability: |-
{{states.light | selectattr('state', 'eq', 'on') | list | count > 0 }}
- name: "Last Light Off"
unique_id: last_light_off_001
state: |
{{ states.light | selectattr('state', 'eq', 'off') | sort(attribute = 'last_changed', reverse=1 )
| map(attribute='entity_id') | first }}
attributes:
count: "{{states.light | selectattr('state', 'eq', 'off') | list | count}}"
availability: |-
{{states.light | selectattr('state', 'eq', 'off') | list | count > 0 }}
If you are interested in the most recent light that was turned on, no matter its current state, you will need the above and the following:
- trigger:
- platform: state
entity_id: sensor.last_light_on
not_to:
- unknown
- unavailable
not_from:
- unknown
- unavailable
sensor:
- name: "Most Recently Turned On Light"
unique_id: most_recent_turned_on_001
state: |
{% set current = this.state %}
{% set new = trigger.to_state.attributes.count > trigger.from_state.attributes.count %}
{{ trigger.to_state.state if new else current }}
- trigger:
- platform: state
entity_id: sensor.last_light_off
not_to:
- unknown
- unavailable
not_from:
- unknown
- unavailable
sensor:
- name: "Most Recently Turned Off Light"
unique_id: most_recent_turned_off_001
state: |
{% set current = this.state %}
{% set new = trigger.to_state.attributes.count > trigger.from_state.attributes.count %}
{{ trigger.to_state.state if new else current }}
FWIW, there are a lot of light entities that you might not consider useful for your purposes like indicator leds or displays. In that case you might want to consider setting up a group for just lights that you actually want to track.
Any light turned off last should be in turned on state, no?
At this point I would say that any light turned off last should be either off or on…
How can I use this with a group of lights?
Basically, replace
states.light | selectattr('state', 'eq', 'on')
with
state_attr('light.YOUR_LIGHT_GROUP', 'entity_id') | select('is_state','on')| expand
This is awesome! works perfectly.
is it possible to add a brightness attribute?
i tried it, but i cannot make it work.
i tried this before giving up:
brightness: “{{ state_attr(‘light.YOUR_LIGHT_GROUP’, ‘entity_id’) | select(‘is_state’,‘on’)| expand | sort(attribute = ‘last_changed’, reverse=1 ) | map(attribute=‘brightness’) | first }}”
But the brightness attribute shows no status.
You’re 95% there… You just need to adjust map
to go into the attributes of the state object:
{{ state_attr('light.YOUR_LIGHT_GROUP', 'entity_id')
| select('is_state','on') | expand
| sort(attribute = 'last_changed', reverse=1 )
| map(attribute='attributes.brightness') | first | default(0, true) }}
One thing to note is that this will return a brightness, if you want a percentage you’ll need to divide the output by 2.55.
{{ (state_attr('light.YOUR_LIGHT_GROUP', 'entity_id')
| select('is_state','on') | expand
| sort(attribute = 'last_changed', reverse=1 )
| map(attribute='attributes.brightness') | first
| default(0, true) / 2.55) | int }}