Oh dear, strange, it works now. Sorry to say this, but your indentation is indeed wrong; I have adjusted it.
But in any case, thank you very much for the help again.
Have you changed the light states since reloading the edited and/or new sensor config…? It can’t know something that happened prior to it being configured. This is the expected behavior any time you edit the config of a trigger-based sensor.
If you are using a sensor, I don’t think its possible to record the entire state of the light since the state class of sensors is different to lights (you will just get the on/off state).
Given you only want to update the sensor if the state actually changes.
And that you don’t want unknown or unavailable states.
The only time that the last state would be different to “NOT current state” would be if someone changed the light state while HA is turned down/restarting.
Is that the use case you are trying to handle or are you trying to achieve something different?
Thank you for your response. The intention is simply that the state of the sensor remains the same after startup as before startup.
So if the state of the sensor is ‘on’ when shutting down HA, I would like it to be ‘on’ after startup as well. And I am unable to get this to work. It is possible that this is not possible!
You need a sensor that is restored after restart, which requires a sensor with a unit of measurement.
A light does not have a unit of measurement, so you need to be a bit creative and maybe use a voltage template sensor and have 1 as on and 0 as off.
Beware that the sensor will still be unavailable a few seconds in the boot up, until the restoring occurs, so any automations need to take this into account.
Nope. They don’t want the state to be recorded when it changes to unknown/unavailable. Changing from unknown/unavailable to a valid state should be recorded.
I realize it’s counter-intuitive, but this is how it needs to be done when using trigger.from_state.state
Otherwise, in the original code, the sensor triggers when it changes from unknown to on and then the template is evaluated which will render as unknown. That is why this template sensor is not working.
I suppose the alternative is to leave it as not_to but then replace the template to trigger.to_state.state