Hello! I’m trying to get started with YAML based automation instead of using Node Red (because it just feels a bit complicated for what I want). As part of a script/automation I need the previous value of the brightness of a smart bulb. I believe the correct way of doing this is with a template, so I wrote the following:
template:
- trigger:
- platform: state
entity_id:
- light.bedside_lights
attribute: brightness
not_to:
- "unknown"
- "Unknown"
- "none"
- "None"
- "unavailable"
sensor:
- name: Bedside Lights Brightness History
state: "{{ this.attributes['val1'] }}"
attributes:
val0: "{{ state_attr('light.bedside_lights', 'brightness') }}"
val1: "{{ this.attributes['val0'] }}"
As I understand it, this should:
- Trigger any time the brightness value updates
- Not trigger if the light turns off, in which case the value appears as “None” or “Unknown”, depending where I look
- Update the value of
val0
to the current value of the brightness - Update the value of
val1
to the current value ofval0
(before it is updated) - Give me a sensor whose state is the previous or current value of
val1
, I don’t really care which, as I can remove a value if it’s not what I expect.
However, it doesn’t work. When I watch the values changing in the sensor history they’re just a mess, but as far as I can follow them:
- All three values (state, val0 and val1) update every time the value of brightness changes, regardless of whether the light has been turned off, thus transitioning the brightness to None, or Undefined.
- If I only update the brightness,
val0
updates to the latest value, as expected,state
updates to the previous value (sort of as expected?) andval1
does it’s own thing. I think it may be getting an intermediate value between the previous and next brightness. - If I switch off the light,
val0
updates to undefined,val1
updates to the previous value ofval0
, states updates to the value ofval1
. If I switch it on again,val0
takes on the new value,val1
changes to undefined,state
changes to the value ofval1
. If I turn it off again,state
goes undefined, however if I just update the brightness,state
gets the value fromval0
.
Basically, what I’m saying is, I’m lost, and would appreciate any help anyone can give!