One automation triggered at each restart, why?

Yes you’ve replied before me :slight_smile:

Will try that, thanks

Putting it all together here (with the typo corrected):

  sensors:
    open_windows:
      entity_id:
        - sensor.temperature_out
        - sensor.temperature_in
      value_template: >-
        {%- set invalid = ['unknown', 'unavailable'] %}
        {%- 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 %}

EDIT
The lion’s share of this thread has been to devise a means of mitigating the root-cause of this issue which was answered early in the thread.

Question: One automation triggered at each restart, why?
Answer: Because the automation’s trigger relies on resources that disappear moments before restart.

The true ‘fix’ is that the state of the sensors shouldn’t go from known to unknown moments before restart. The orderly shutdown of integrations should not cause the entity states to effectively be lost.

Unfortunately, resolving that is an architectural matter (and beyond the average user’s control) so what’s left is to create templates (or whatever) to detect and mitigate the issue when it arises. Basically, it’s a workaround.

2 Likes

I have tested the last code and if that code works, the automation is still triggered… I can see in the History tab that if the sensor is on before restarting, it goes off when restarting and then on again. If sensor is off before restarting, it goes on when restarting and then off again…
Capture d’écran 2020-05-28 à 10.55.47
Capture d’écran 2020-05-28 à 10.55.05
I’m wondering if last state is restored at boot for binary_sensor based on templates.

I’ve also tried to turn off the automation when event stop is firing but it does not work.

Guess I need to less restart HA :man_shrugging:

Here is my last code:

- alias: Notification - Ouvrir les fenetres, fait bon dehors
  initial_state: true
  trigger:
    - platform: time_pattern
      minutes: "/10"
  condition:
    - condition: template
      value_template: >-
        {% set outside = states('sensor.temp_out') %}
        {% set inside = states('sensor.temp_in') %}
        {{ (outside|float + 0.7) <= inside|float }}
    - condition: state
      entity_id: binary_sensor.temp_out_falling
      state: 'on'
    - condition: template
      value_template: "{{ now().month >= 5 and now().month <= 9 }}"
    - condition: state
      entity_id: input_boolean.open_windows
      state: 'off'
    - condition: state
      entity_id: group.all_person
      state: 'home'
  action:
    - service: notify.all
      data:
        title: "Open"
        message: "Windows!"
        data:
          push:
            sound: default
    - service: input_boolean.turn_on
      data:
        entity_id: input_boolean.open_windows

I solved my issue by triggering my automation every 10 minutes and the temp check is done in conditions

The Solution was posted a long time ago but you chose not to use it. Checking every 10 minutes is an inefficient workaround.

1 Like

I have tested all the code that has been given here and I thank you all for your help but none worked. The code is fine and is completely logic but each time I restarted HA I got a notification because the automation was triggered (due to state being unknown for whatever reason). Even when using {{ states('binary_sensor.open_windows') }} when one of the entity’s state is not a number.
I have concluded that a trigger with a template or a trigger with a binary sensor based on a template was not a good idea here. Even if it is not efficient, it now work really fine: I do not have anymore a notification when HA is restarted and it work as expected