Scripts - How to detect if they are running?

I have a few fundamental questions about scripts which I wonder if I should know the answer to :wink:

I have always called scripts using,

- service: script.my_script

Very often, I have also successfully used the principle of waiting for the script to start and then for it to finish before continuing:

- wait_template: "{{ is_state('script.my_script', 'on') }}"
- wait_template: "{{ is_state('script.my_script', 'off') }}"

Which has always worked well until today when using that same technique, I had one script wait for another to first be ‘on’, and then be ‘off’, and even though I know it ran (it was a TTS announcement and I heard the announcement) it never seems to have had its state set to ‘on’.

image

Furthermore, and possibly connected to my problem, what is the difference between a script that is ‘started’ and a script that is ‘turned on’? Is it something to do with one having a wait_template?

image
image

But most importantly, why does a script that is ‘started’ apparently never change its state?

Notice that with the script that is ‘turned on’ the state does change.

image


Looking at the information above,

the script ‘announcement_morning_greeting’ did not get past waiting for either,

  • the script ‘announcement_arsenal_game_reminder’ to be ‘on’ (which it definitely was because we heard the announcement) or
  • the script ‘announcement_arsenal_game_reminder’ to be ‘off’ (which it definitely is now as evidenced by the current state)

Wow, I never really noticed this. This could explain a lot! :smile:

So, after reviewing the code, it looks like whenever a script is started it always fires a script_started event, which would explain the “started” entry in the logbook. However, the script entity is not turned on unless it executes a delay, or a wait_template that actually waits. And given the way it’s implemented, even then there’s a chance it might not actually turn on. (What happens is a state update is scheduled, and if the update happens soon enough to see the script still running – i.e., either still waiting for the delay or wait_template – then the state of the entity in the state machine will get set to on. However, if the delay/wait_template completes too fast, the update will see the script is no longer running, and it won’t change to on. Not sure how likely this is, but it seems possible.)

3 Likes

Wow indeed…

Thanks for looking into this. It sounds like this must be a bug, surely?
It makes no sense (to me, without any other view from someone else to change my mind) to have scripts only sometimes change state.

It seems to me that the only way around this would be to have as the last action in every script, a wait_template that waits for itself to change state to ‘on’.

All a bit messy.

And a major PIA as I use this technique a lot in my code as I generally script everything into small reusable* modules.


*Kind of, with limitations…

Completely agree.

That’s actually a very good workaround. That would guarantee the state will change in the state machine. Nice!

Also completely agree.

Do you know if this ever got fixed changed?
It’s might be a long shot but unfortunately I wouldn’t know where to begin to look for the answer.

I only ask because I know a lot of stuff gets fixed and updated without it ever being referenced anywhere publicly.