One automation triggered at each restart, why?

I have this automation that is triggering each time I restart HA (Configuration > Server Controls > Restart) even if the trigger is not matching:

- alias: Notification - Ouvrir les fenetres, fait bon dehors
  trigger:
    - platform: state
      entity_id: binary_sensor.open_windows
      from: 'off'
      to: 'on'
  condition:
    - condition: state
      entity_id: group.all_person
      state: 'home'
    - condition: template
      value_template: "{{ now().month >= 5 and now().month <= 9 }}"
  action:
    - service: notify.all
      data:
        title: "Open"
        message: "windows!"
        data:
          push:
            sound: default

Binary sensor:

- platform: template
  sensors:
    open_windows:
      entity_id:
        - sensor.temperature_out
        - sensor.temperature_in
      value_template: >-
        {{ (states('sensor.temperature_out')|float + 1) <=
            states('sensor.temperature_in')|float }}

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

Is there a way to avoid this?

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 unknown prior 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.

Try this:

- platform: template
  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 %}
          {{'off'}}
        {% endif %}

Nice! it’s working fine now. But what when binary_sensor.open_windows will be on? :upside_down_face: :thinking:

Can we just set it to last state?

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. :slight_smile:

  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 %}
1 Like

Well it’s not that simple… It should be handled by integrations to be fine.
I will check when it will be on.

I’ve modified your code to make it work:

- platform: template
  sensors:
    open_windows: 
      entity_id:
        - sensor.temperature_out
        - sensor.temperature_in
      value_template: >-
        {% set outside = states('sensor.temperature_out') | float %}
        {% set inside = states('sensor.temperature_in') | float %}
        {% if outside is number and inside is number %}
          {{ (outside + 1) <= inside }}
        {% else %}
          {{ states('binary_sensor.open_windows') }}
        {% endif %}

If it doesn’t to work I may need an automation to store the last value in an input_number or something else and use that

Thank you for your help

Actually, your modification does the opposite.

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.

Ok just to let you know that I’ve done the opposite with a binary_sensor.close_windows :laughing:

Aaaaaannnnd it does not work :sleepy:

I don’t know what you mean by that. However, if it used float filters the way I pointed out above then it’s not likely to work.

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

I’ve done this sensor:

    close_windows: 
      entity_id:
        - sensor.temperature_out
        - sensor.temperature_in
      value_template: >-
        {% set outside = states('sensor.temperature_out')|float %}
        {% set inside = states('sensor.temperature_in')|float %}
        {% if outside is number and inside is number %}
          {{ (outside + 0.5) > inside }}
        {% else %}
          {{ states('binary_sensor.close_windows') }}
        {% endif %}

It includes the same error I pointed out in a previous post.

That is not what I am seeing in the template editor.

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 %}
2 Likes

mmmh ok but:

Yes, see my post above.