My new template sensor helper uses its previous state when the input is invalid. It parses the content of last_notification
and if a regex matches, the state is updated. Otherwise, it uses the current state with this.state
:
{% set grade = (states('sensor.oneplus_last_notification') | regex_findall(find='\d{1,3}\.?\d*(?=%\) in .+ Integrated Math)', ignorecase=False))[0] %}
{% if grade is defined %}
{{ grade }}
{% else %}
{{ this.state }}
{% endif %}
It works as expected until HA restarts. Then the state changes to unknown
.
The same happens using states()
instead of this.state
.
I guess the explanation is that at some point during the restart the state of the sensor is in fact unavailable but doesn’t this.state
reference the last valid (non-unavailable
or unknown
) state?