When I restart HA, binary_sensor.open_windows is off and not on and it does not change from off to on. If the binary_sensor is on I do not receive the notification.
Note that this automation is triggered at shutdown and not at HA start (I receive the notification at the same time as the notification of HA shutdown)
You stated the notification is received at shutdown, not startup. It implies the binary_sensor’s state changes from off to on in the final moments before shutdown (and triggers the automation). However you also stated:
How are you confirming this in the last moment before shutdown?
Ok you’re right, just before shutting down, binary sensor goes to on (and this state is restored at startup)… I can see this in the history tab and in logbook.
I don’t understand why, outside temp is way above inside temp… Maybe an issue with temp sensors? One is from the min_max integration and the other one RFLink
Now that you have confirmed the binary_sensor’s does, in fact, change to on in the final moments, we can examine its template and theorize why that might be happening.
What if, in the last moments, the state of temperature_out becomes unknown or unavailable. The states() function will report that as unknown, the float filter converts that to 0.0 and then that gets added to 1 resulting in a value of just 1.0. That is less than temperature_in and the template reports true (binary_sensor changes state to on).
However, this theory assumes only temperature_out becomes unknown and not temperature_in as well, or at least it becomes unknownprior to temperature_in also becoming unknown.
Which one is based on the min_max integration and which one on the RFlink integration?
Either way, the solution will be to enhance the template so that it gracefully handles the situation when the sensor’s value becomes non-numeric.
Outside temp is from RFLink, inside temp is an average (min_max integration) from 3 temp sensors : one from ESPHome, 2 from Deconz.
I’ve checked directly in the DB: all RFLink sensors goes to unknown, so is the min_max sensor. So both sensors from my binary are unknown when HA is shutting down.
That would be ideal but at the final moment prior to restart, how do you get that binary_sensor’s “last state”?
It’s tempting to just replace: {{'off'}}
with this: {{ states('binary_sensor.open_windows') }}
However, the reason we are in the {% else %} section is because the state of both sensors is non-numeric (i.e. unknown). So all we will have accomplished is to set the binary_sensor’s state to unknown.
I suggest you test the example I provided when the binary_sensor reports on in the final moments prior to restarting. After startup see if the binary_sensor continues to report on (or it’s now off).
If it reports off, give the other technique a shot (shown below) because, after all, I may be wrong.
sensors:
open_windows:
entity_id:
- sensor.temperature_out
- sensor.temperature_in
value_template: >-
{% set outside = states('sensor.temperature_out') %}
{% set inside = states('sensor.temperature_in') %}
{% if outside is number and inside is number %}
{{ (outside | float + 1) <= inside | float }}
{% else %}
{{ states('binary_sensor.open_windows') }}
{% endif %}
You added float filters to the first two statements which means even if the sensor’s value is unknown, the filter will convert it to 0 (a number). So the next statement, that checks if the values are numbers, will always evaluate to true. Therefore this template will never execute {% else %}.
For testing purposes, you can temporarily alter this template {{ (outside + 1) <= inside }} to make it easier to have the binary_sensor’s state set to on. Perhaps just change the < to a > instead.
Effectively, the original problem posed in the first topic has had its root-cause identified and a mitigation technique has been offered and proven to solve the issue. Restoring the binary_sensor’s state is something that remains to be tested.
For the benefit of other users who may be experiencing a similar problem, and are searching for answers, please mark my post (the one containing the explanation or the one containing the example code) with the Solution tag. This will automatically place a check-mark next to the topic’s title which signals to other users that this topic has a solution. Thank you.
I tested your code and the binary sensor was always false. So I put your code in Developer Tools > Template and {% if outside is number and inside is number %} was never true.
If I don’t put float before, outside and inside where never numbers.
I’ve tried with {% set outside = 'unknown' | float %} and it is not a number
Everyone here is focusing on 'unknown' when the real issue is that {{ '7' is number }} is false. It’s a string. So even if we did have a number you’d get the same outcome everytime, which is what OP is seeing.
{%- set invalid = ['unknown', 'unavialable'] %}
{%- set outside = states('sensor.temperature_out') %}
{%- set inside = states('sensor.temperature_in') %}
{%- if outside not in invalid or inside not in invalid %}
{{ (outside | float + 0.5) > inside | float }}
{%- else %}
{{ states('binary_sensor.close_windows') }}
{%- endif %}