HomeAssistant keeps restarting maybe because of an automation loop?

Hi there,

tried to make an ESPhome internal setup to keep the temperature and humidity at the same time within a given range and had no success.

Now I tried to create a looped automation within HA and outside ESPhome …

alias: Filament klimatisieren (PLA)
trigger:
  - platform: state
    entity_id: input_boolean.filament_vorwaermen
    to: "on"
condition:
  - condition: and
    conditions:
      - condition: or
        conditions:
          - condition: state
            entity_id: input_boolean.beide_filadryer_vorwaermen
            state: "on"
          - condition: state
            entity_id: input_select.filadryerwahl
            state: Filadryer 2
      - condition: or
        conditions:
          - condition: state
            entity_id: input_select.filamentwahl
            state: "PLA (weiß/1.75) : Form Futura"
          - condition: state
            entity_id: input_select.filamentwahl
            state: "PLA (schwarz/1.75) : Form Futura"
          - condition: state
            entity_id: input_select.filamentwahl
            state: "PLA (silber/1.75) : Prusament"
action:
  - repeat:
      while:
        - condition: state
          entity_id: input_boolean.filament_vorwaermen
          state: "on"
      sequence:
        - if:
            - condition: or
              conditions:
                - condition: numeric_state
                  entity_id: sensor.keller_filadryer2_temperatur
                  below: 45
                - condition: numeric_state
                  entity_id: sensor.keller_filadryer2_luftfeuchtigkeit
                  above: 30
          then:
            - service: switch.turn_on
              entity_id: switch.keller_filadryer2_heizung
            - wait_for_trigger:
                - platform: numeric_state
                  entity_id: sensor.keller_filadryer2_temperatur
                  above: 45
                - platform: numeric_state
                  entity_id: sensor.keller_filadryer2_luftfeuchtigkeit
                  below: 30
            - service: switch.turn_off
              entity_id: switch.keller_filadryer2_heizung
mode: single

… that seems to work…

Bildschirmfoto 2023-08-02 um 19.37.51

… but seems also to crash HA:

23-08-02 18:46:34 ERROR (MainThread) [supervisor.homeassistant.api] Error on call http://172.30.32.1:8123/api/config: 
23-08-02 18:48:46 WARNING (MainThread) [supervisor.homeassistant.websocket] Connection is closed
23-08-02 18:48:46 ERROR (MainThread) [supervisor.homeassistant.api] Error on call http://172.30.32.1:8123/api/config: [Errno 104] Connection reset by peer
23-08-02 18:48:51 WARNING (MainThread) [supervisor.misc.tasks] Watchdog miss API response from Home Assistant

So far, however, this is only a guess, because this error and associated restart only occur when automation is running, but …

  • … how can I prove a definite connection in the logs?
  • … where is the error in the code of the automation?

Any help would be highly appreciated! :slight_smile:

Thank you all!

Kay

You need to think differently. Rather than staying in a loop which will consume resources until your system crashes, trigger on the numeric states, and also use them as conditions (this is equivalent to having AND triggers), if these pass, turn on the device in your action then finish.

Don’t wait in the actions for the states to change, write another automation with the wanted states in numeric triggers and conditions then use the action to turn off your device.

Hi Tom!

Thank you! :kissing_heart:

Great idea to let two automations play the ball to each other to keep the desired temp and humidity values within desired range.

I’ll give it a try this night :slight_smile:

But - don’t you think - concerning the initial post for an „ESPhome-solution“ - such behavior shouldn’t be coded within the ESP? Any ideas or experiences?? :roll_eyes:

And further more: I would be interested what I made wrong as this - I must confess - is my first looped automation and I thought of it it’s a rather simple one - isn’t it?

Thanks again!

Kay

EDIT: Just got an answer to the ESP integrated control several minutes ago: wouldn’t it be more elegant to set this on the device itself? But nevertheless: I still would be interested in why a “simple” (?) automations loop crashed the whole system…

Yes Drew’s answer in your other topic would be a better way to control this.

I told you why your loop was a bad idea. Home Assistant is not designed to operate in continuous action loops. It will consume all your memory and crash. It is designed to be state driven.

Okay, I understand: state => reaction => new state => reaction … Otherwise the automations data stays in memory repeatedly and leads to the crash…

Aaand I also understand that - because of this - state driven automations finish after each step and therefore don’t provoke such reactions :slight_smile:

But I am interested in the idea of loop functions in automations as they do exist: what would be the usecase for the loops. What would you say is the “limit”? Are they i.ex. time-limited?

Kay

In the past I have used loops to make sure actions happen over intermittent network links. I repeat the action with a delay so the action gets retried every 5 seconds. However I also keep count of the number of loops (attempts) and give up after 12 loops (60 seconds).

The same sort of thing can be used for critical actions like pool filling pumps, where you want to make sure it turns off. Just in case the first attempt does not work for whatever reason, do it again a few more times until either the pump says it is off or you exceed the maximum number of tries you set (and send an urgent notification instead if that happens).

In all cases you should have an extra condition that breaks the loop after a reasonable amount of time if the required action does not happen.

Ok, great - thank you! :slight_smile: