Unpredictable behaviour with nested code

suppose we have this pseudo code

  • action: script/toggle/wathever with complex code or nested
  • delay: xxx
  • do something else

i noticed, because strange behavior, that the first line starts executing but immediately delay is executed… i suppose it is executed asyncronously

what happens now, delay stops the execution of first line too, when the delay expire “do something else” starts while first line have to complete yet

so, you don’t know what is running and you can have the wrong behaviour

bug?! if no, any suggestion to solve the problem or run syncronously a block of code?!

thanks

No.

There are two ways to call a script and they have different behaviours. One waits for the script to complete before executing the next action. The other method does not. See: Scripts - Home Assistant

already used the “wait” possibilities but in some situations it waits forever

ok, have to rewrite something

thanks

You definitely need to re-write that script. You should not be waiting in scripts or automations for longer than a minute or so.

i retrived an example of a block of code used for my tests


    - repeat:
        count: 2
        sequence:

          - service: persistent_notification.create

            data:
      
              title: 'begin'

              message: "{{states('switch.myswitch')}}   {{now()}}"

          - action: switch.toggle

            target:

              entity_id: switch.myswitch

          - delay: 1

          - wait_for_trigger:
              - trigger: state
                entity_id: switch.myswitch
                to:

          - delay: 30

          - service: persistent_notification.create

            data:
      
              title: 'end'

              message: "{{states('switch.myswitch')}}   {{now()}}"

myswitch has nested code

it waits forever… if i remove the delay: 1 line it goes for the first cycle with state unchanged and the wait forever

what i’m missing?! is wait_for_trigger the problem?!

edit: the switch requires few seconds to change state…

You need to add a wait timeout to this in case it does not happen.

As far as I know, synchronous mode is the default mode for almost everything a user might touch in HA (actions, scripts, automations, etc.). Asynchronous doesn’t mean parallel execution, but “yielding”. Whenever you use the -delay or -wait_for_* action, that’s yielding. So the whole picture is synchronous execution, jumping from one fragment to another, managed by some internal scheduler.

That’s why I think, the async behaviour you see is probably caused by “yielding” code contained in your switch.

Most actions have to be completed before moving to the next. With some exceptions, e.g. explicitly using parallel mode or the second script call method shown in the link I shared above.

Yeah I somehow forgot about the huge exception with script.turn_on! :pray:

found the problem, i hope so…

i.e. we use turn_on / turn_off of a switch and we start clicking to see the results…

when you put a delay (or more delay) somewhere in the code turn_on and turn_off continue to run, even if you change the state of the switch, and things messes up fast; the result is unpredictable

You are going to have to be a lot clearer in your description of the problem than that.

Do you mean that the switch takes time to updates its state in home assistant?