Execute script from another script?

I have a script and want it to be executed from another script. HA runs all services but never execute the service with script. (script.tvzone_turn_off). is it intentionally not allowed to avoid recursive calls? or I do something wrong?

sleep_all:
  alias: Sleep all equipment
  sequence:
    - service: switch.turn_off            
      data:
        entity_id: switch.rf3_switch_asus
    - service: script.tvzone_turn_off            
    - service: switch.turn_off            
      data:
        entity_id: switch.sonoffdual

A script can call another script.

However, if the other script is busy, the call to it will be ignored (i.e. it won’t wait until the script is no longer busy, it just skips it).

A script that attempts to call itself (recursive) will not work because it is effectively still busy and so the call will fail.

Hmm… “Busy” ? mine should not be busy. But how do I check it?

If a script uses a delay or wait_template then it has the potential to become busy. While it is waiting for the delay to expire, or the conditions of the wait_template to be met, it is considered to be busy and this script cannot be called at that time.

  • Scripts that contain delay or wait_template can be turned off. In the Lovelace UI, they are rendered with a toggle button.
  • Scripts that do not contain a delay or wait_template are rendered with an EXECUTE button.

If your tvzone_off script does not use a delay or wait_template then the issue you are experiencing may be due to something else.

Sorry Teras, I was under the impression that it DID run the already running script but it just skipped all the delays and went straight to the payload.
(i may be wrong, just trying for clarification)

If there are concerns about calling an already running script you ‘could’ either check if the script is running before hand or set a bit at the begining and reset it at the end and use that in auotmations/scripts to prevent doubling up

I may learn something here :smiley:

Edit : Sorry, cross post (and yes I learnt something !)

Well… the child script does use the delay but the script itself is not running…
but ok, I will insert one more step to turn_off the script before calling it.

tvzone_turn_off:
  alias: Tunr off TV zone
  sequence:
    - service: media_player.turn_off
      data:
        entity_id: media_player.amazon_tv_stick
    - delay:
        seconds: 5
    - service: switch.turn_off
      data:
        entity_id: switch.rfkey4

That stirs the cobwebs in my mind. It may behave that way. I’m not certain because I rarely use scripts.

@kol
Based on what Mutt said, when the tvzone_turn_off script is called by your script, is any part of it executed? Or is all of it ignored?

the “sleep_all” alwys performs successfully the steps #1 (rf3_switch_asus) and #3 (sonoffdual) but ignores the #2 (script.tvzone_turn_off). I cannot imagine the “tvzone_turn_off” is executed by something else during its call…

I know that automations behave that way and until VERY recently they were handled by the same python code. I know they have now split it, but I’m not sure of the current divergence.
Dunno, I’m speculating based on whispers :man_shrugging:

No there should be no issue doing so. How odd this is not happening. running scripts from within scripts or automations (which is effectively identical) forms the base of HA automation, so that should be debugged if it is not working for you.

You could start by eliminating all data fields here, which are unnecessary. They might not cause trouble but less characters is less code to debug :wink:

sleep_all:
  alias: Sleep all equipment
  sequence:
    - service: switch.turn_off            
      entity_id: switch.rf3_switch_asus
    - service: script.tvzone_turn_off            
    - service: switch.turn_off
      entity_id: switch.sonoffdual

and

tvzone_turn_off:
  alias: Tunr off TV zone
  sequence:
    - service: media_player.turn_off
      entity_id: media_player.amazon_tv_stick
    - delay:
        seconds: 5
    - service: switch.turn_off
      entity_id: switch.rfkey4

beware for typo’s (as you have in the alias)…

1 Like

I found all my scripts in the Lovelace UI Scene editor… executable and editable but it does not show the ‘entity_id’ there. I tried to create a simple scene, saved it and then it reformated my “scripts.yaml” file: removed all comments, blank lines, etc… On another side, the good news is the ‘sleep_all’ script works now !?!

aren’t you now mixing scripts and scenes, and confusing this topic? confusing me for sure… :wink:

Ooops. you are right. these two items next to each other. I have mixed them… Sorry. But anyway my script_in_script works fine. I have removed “data:” as you suggested, the code looks much better now, and the Web editor “cleaned” the “script.yaml” as well. All works! Thanks!