Don't understand, how delays and triggers work

Don’t understand, how delays work

Hello. Want to make an automation that contains a delay and after delay - a repeat loop (repeat until temp is below desired) with delays (delay - start a heater - delay - stop a heater). So sequence look like this

alias: heat_script
sequence:
  - delay:
      hours: 0
      minutes: 5
      seconds: 0
      milliseconds: 0
  - repeat:
      until:
        - condition: numeric_state
          below: input_number.t_otkrytiia_okna_mk
          entity_id: sensor.temperatura
      sequence:
        - type: turn_on
          device_id: 9baf3d00aaed113fb23489155460faba
          entity_id: switch.rozetka_3_switch
          domain: switch
        - delay:
            hours: 0
            minutes: 3
            seconds: 0
            milliseconds: 0
        - type: turn_off
          device_id: 9baf3d00aaed113fb23489155460faba
          entity_id: switch.rozetka_3_switch
          domain: switch
        - delay:
            hours: 0
            minutes: 3
            seconds: 0
            milliseconds: 0

But I can’t call it inside a Time Pattern trigger (2 minutes).
The idea is whole automation should start every 2 minutes, but if condition chooses my sequence, all automation should wait until sequence finished. In a real my automation triggered every 2 minutes and never wait a delay 5 minutes. So ot never entered a loop.
What I did? I moved all this sequence with delays in a separate script and called it from automation- no result. Automation starts every 2 minutes and didn’t waiting my script.
How can I avoid this? I want to start automation every 2 minutes, but, if choosed a delay-loop sequence, trigger must waiting for sequence finished and restart automation only after my loop ended. How can I do that?
Thanks.

Please see the sticky post which explains how to format code.

Can you explain what you want to achieve as opposed to how you want to make an automation work?

1 Like

I want to: 1) start the automation every 2 minutes (time pattern trigger) 2) if temp is above - do some actions 3) if temp below - do my “delay+loop action”. Also there are delays inside the loop. But overall time of all delays in a branch(3) are bigger then Time Pattern trigger, so automation restarts every 2 minutes and doesn’t wait when my delays ended. But I want that trigger “Time pattern”, which causes automation to run, must stop and wait until my branch(3) ended. But can’t do that. How can I make the Time Pattern trigger (or another trigger) to wait unti all actions ended?

You’re still explaining the mechanics of the automation you have in mind as opposed to what is the intended application.

2 Likes

Shortly, please, I don’t understand what you mean. How can I do that I want in a terms of Home Assistant? I want to enter a cycle, that pauses a trigger until cycle is equal a some condition. When cycle ended - a trigger must be activated again. I need exactly this algorithm, no need to explain how I use it.
Maybe is there some alternatives to trigger Time Pattern or even a trigger? How can I make a trigger to wait a delays inside my automations?

First, you should have a look at the mode of your automation, as an automation that could possibly run for plenty minutes (until the repeat condition is matched) will cause a re-entrance.

Please read Automation Modes to understand the modes.

Then, if you want your automation to start every 2 minutes, you’ll need a trigger that do that and it is the time pattern, like this:

  - minutes: '/2'
    platform: time_pattern

Your action sequence is the following and I believe this is what you want:

wait 5 minutes then repeat (<-- this should probably disappear and be replaced with the time pattern)

  • turn on switch
  • wait 3 minutes
  • turn off switch

until your temperature sensor is bellow (?) an input_number of yours.

When the condition is fulfilled, the automation will stop.

What I mean is you should describe what you want your application to do, not how you think the automation should be structured to do it.

Describe what you want to achieve. For example:

I want to turn on a switch when the temperature is above 21.

Don’t describe how you want the automation to work:

I want to make a loop that checks a switch and uses a script that’s called only when the temperature rises above bla bla bla …

Maybe there’s a better alternative to “exactly this algorithm”. If you explain what you want your application to achieve, as opposed to the algorithm you want, we can help you choose the best approach.

2 Likes

This is a code

alias: winter test
description: ""
trigger:
  - platform: time_pattern
    minutes: /2
condition: []
action:
  - choose:
      - conditions:
          - condition: numeric_state
            above: input_number.t_otkrytiia_okna_mk
            entity_id: sensor.temperatura
        sequence:
          - type: turn_off
            device_id: 9baf3d00aaed113fb23489155460faba
            entity_id: switch.rozetka_3_switch
            domain: switch
          - type: turn_on
            device_id: ac3f119a7ddde952e4ed0bd5cba0b5d6
            entity_id: switch.provetrivanie
            domain: switch
      - conditions:
          - condition: numeric_state
            below: input_number.t_zakrytiia_okna_mk
            entity_id: sensor.temperatura
        sequence:
          - type: turn_off
            device_id: ac3f119a7ddde952e4ed0bd5cba0b5d6
            entity_id: switch.provetrivanie
            domain: switch
          - device_id: 2dbe7e38309f805e9f4c9dfcf171a342
            domain: cover
            entity_id: cover.drivent_2e8
            type: set_position
            position: 10
          - delay:
              hours: 0
              minutes: 5
              seconds: 0
              milliseconds: 0
          - repeat:
              until:
                - condition: numeric_state
                  below: input_number.t_otkrytiia_okna_mk
                  entity_id: sensor.temperatura
              sequence:
                - type: turn_on
                  device_id: 9baf3d00aaed113fb23489155460faba
                  entity_id: switch.rozetka_3_switch
                  domain: switch
                - delay:
                    hours: 0
                    minutes: 3
                    seconds: 0
                    milliseconds: 0
                - type: turn_off
                  device_id: 9baf3d00aaed113fb23489155460faba
                  entity_id: switch.rozetka_3_switch
                  domain: switch
                - delay:
                    hours: 0
                    minutes: 3
                    seconds: 0
                    milliseconds: 0
    default: []
mode: restart

But it never ends first 5min-delay, because of /2 Time Pattern. Sorry, I don’t quite understand exactly which automation mode (restart or queued) to set.

In single, you’ll get a warning but your automation should continue (but the new one will not start)
In restart, the 5 minutes delay will stop and the new automation will take over but will obviously be interrupted by the next run 2 minutes later
In queued, the current one will finish and the next /2 time pattern will start right after it, but you’ll run over a full queue very fast as it is 10 runs in the queue by default (could be changed but I don’t think that it is the right approach)
In parallel, it is almost the same as the queue but you’ll have conflict when one run will off a switch while the other is trying to on it.

Basically, the 5 minutes wait is something you should avoid at any price because of the /2 time pattern, either you do a time pattern that is longer and include the 5 minutes (/7 as an exemple).

Or, you change how it works. What I get from the code is that you want to on/off a switch until a preferred temperature is reached but you’re playing with more than one entity to achieve your goal.

Can’t you change the approach and do either a template trigger including all your conditions at once or multiple entities, including the temperature?

I agree with Taras that you probably shouldn’t try to make this automation to work as it is but more rethink what you want to achieve.

It could be as easy as

trigger:
  - platform: numeric_state
    entity_id: sensor.sensor.temperatura
    for:
      hours: 0
      minutes: 3
      seconds: 0
    above: '{{ states.input_number.t_otkrytiia_okna_mk.state }}'
    id: too_hot
  - platform: numeric_state
    entity_id: sensor.sensor.temperatura
    for:
      hours: 0
      minutes: 3
      seconds: 0
    below: '{{ states.input_number.t_otkrytiia_okna_mk.state }}'
    id: too_cold
  - platform: time_pattern
    minutes: /2
    id: regular_check
  action:
  - choose:
    - conditions:
      - condition: trigger
        id: too_hot
      sequence:
        - type: turn_off
            device_id: 9baf3d00aaed113fb23489155460faba
            entity_id: switch.rozetka_3_switch
            domain: switch
      - condition: trigger
        id: too_cold
      sequence:
        - type: turn_on
            device_id: 9baf3d00aaed113fb23489155460faba
            entity_id: switch.rozetka_3_switch
            domain: switch
    - conditions:
      - condition: trigger
        id: regular_check
      sequence: []

But in the regular check, don’t wait for anything. Alert your phone, display something in an input_text, toggle an input_boolean to show the world that you’re in a check. Or remove that part.

:+1:

The moment I see an automation employing a Time Pattern Trigger, it’s often an indication that it can be improved.

The Time Pattern Trigger has useful applications but is often misused (usually due to a misunderstanding of how event-based systems work); Home Assistant is event-based thereby (nearly) eliminating the need to poll for status.

That’s why I have asked poddex to explain the intended goal of the application as opposed to seeking a means to fix an existing, sub-optimal, automation.

1 Like

Time pattern is easy to understand, but I didn’t expect that there would be such difficulties with it. I tried to avoid heavy-condition triggers, but it looks like I must re-write…uffff. Maybe I can find simple “While true” trigger.

There isn’t, the whole point is that triggers start the processing of the automation when they become true. If you want something like that you’ll need to investigate Node Red.

Did I understand correctly that there should be more than one automation if I have a lot of different conditions and cycles with delays? I.e. I can’t put everything in one automation in infinite cycle without Node Red?

The answer is … it depends.

You can use the parallel run mode to get them to run in parallel, but that may not give you what you want.

We’re all here to help you achieve your goal, the problem is we don’t know what it is because you haven’t described it.

All we see is a script that toggles a heater switch in a loop until the temperature decreases below a threshold value … and you want to add yet another loop to execute the script containing a loop. Why? Nobody can say for sure because we don’t know your goal.

For example, if the goal is to control a heater so that it maintains temperature within a desired range then the Generic Thermostat integration achieves that with little effort.

It’s not a simple temperature automation. I know, what is generic thermostat. I need exactly some minutes of heater ON and then switch it OFF and check the result after some time (and do some other actions). And do it again until temperature will exceed a desired value. And all of this - inside an infinite loop (repeat it again and again) with a condition responsible for a temperature that is lower than desired value. Start - check the temp - if lower then goto loop (exit when above), if above - goto to another actions. And all of this inside a cycle. Only Node Red can help?

You can do that in a HA automation. It’s not necessarily trivial but it’s certainly do-able.

There’s a few ways of doing it that don’t involve an infinite loop, something as simple as:

Automation One
Triggers:

  1. When the temperature falls below X
  2. When the heater has been off for X minutes

Conditions:

  1. Temperature is below X

Actions:

  1. Turn on heater
  2. Do stuff

Automation Two
Triggers:

  1. When heater has been on for Y minutes

Action:

  1. Turn off heater

etc

Great reply from @Tinkerer.
Did you look at my automation?

Do you understand that when it is too cold and you turn on the heater, the automation will start again when it is too hot for more than 3 minutes ?
Therefore, no need for any loop, while, loop of loops (yuck).

It will warm your room, after a while it will be too hot, it will fire the automation, that will turn off the heater, job’s done.

Maybe you want to improve that logic a bit, because I see that you are using more than just one switch but I’m pretty confident that you can start from my very basic code.

And @Tinkerer gave you a way to be sure to off what is needed after a defined time.

Put as much delay as you need to, but no loop. And pay attention that any delay might trigger different behaviours based on your automation mode.

Good. So you know it maintains a temperature within a desired temperature range, referred to as the ‘deadband’, using cold_tolerance and hot_tolerance.

I need exactly some minutes of heater ON and then switch it OFF

Generic Thermostat can be configured with a min_cycle_duration, meaning it turns on the heater for no less than the specified time.

and check the result after some time

Generic Thermostat check itself.

(and do some other actions).

What are the other actions? Are they done only when the temperature reaches the target temperature?

And do it again until temperature will exceed a desired value.

Generic Thermostat heats until the target temperature is reached.

And all of this - inside an infinite loop (repeat it again and again) with a condition responsible for a temperature that is lower than desired value.

Generic Thermostat effectively does this internally.


tl;dr

Everything you described can be achieved by Generic Thermostat. If you still believe it cannot, then which one of your requirements does it fail to meet?