Running an automation within a script

Can I trigger an automation within a script?
I’ve set up the script below but the autmations are not triggering…

test_script:
  sequence:
    - service: homeassistant.turn_on
      entity_id: 
        - switch.sound_system
        - automation.alexa_play_bbc1
        - switch.physioroom_speakers
        - switch.bedroom_speakers
        - automation.tv_switch_off

…or do I need to put this in an automation which triggers from an input boolean or something like that?
From searching the forum I get the suggestions that a python script could resolve this but I’m reluctant to go down that rabbit hole.

An automation can be turned on (enabled) and turned off (disabled). That’s complete separate from an automation being triggered.

What’s contained in an automation’s trigger: section (and condition:) determines if the automation’s action will execute.

Your script is simply enabling two automations, not triggering them.

makes sense, how can I trigger/action an automation within a script?

I guess I can just duplicate the ‘actions’ I have in the automation as ‘actions’ in the script.
That would be the easiest, thank you for helping me work through this.

Sorted! :wink:

Since triggers are or’ed, you can add another trigger based on the state of say an input boolean, and have the script turn that boolean on.
However, if your conditions or actions depend on the trigger variable, this will get tricky.

… actually if there is a way of triggering an automation from a script that would be helpfull.
One of the automations has a condition which I need and I can’t include conditions in scripts.
I know I can trigger an input boolean which can then trigger an automation but it seems rather long winded when a simple “homeassistant.turn_on” will do the same?

I see you have these two automations:

automation.alexa_play_bbc1
automation.tv_switch_off

and you’re trying to make a script execute an automation. Let’s look at this another way:
How about taking the action portion of the automation and making it a script?

  • Now you can make an automation whose action section executes the script.
  • That script can even be executed by another script.

As previously stated, turning an automation on or off just enables or disables the trigger/condition processing. However, no matter whether it’s on or off, or what the triggers or conditions are, you can always cause the actions to run by using the automation.trigger service. E.g.:

test_script:
  sequence:
    - service: homeassistant.turn_on
      entity_id: 
        - switch.sound_system
        - switch.physioroom_speakers
        - switch.bedroom_speakers
    - service: automation.trigger
      entity_id:
        - automation.alexa_play_bbc1
        - automation.tv_switch_off
2 Likes

that is excellent, thanks Phill!
This is exactly what I was trying to do.
You’re a star! :star:

Be advised that an automation called by automation.trigger will ignore its trigger: and its condition: sections. In other words, only the automation’s action: section will be executed. If that’s what you’re after then all’s well.

Annoyingly, you are correct. The condition is ignored and the actions are triggered only. Urgh!
Looks like I’ll have to trigger a dummy switch (input boolean) which can then trigger the automation. Seems like quite a long winded way to trigger something that’s already been set up

What are you using to trigger the script?

An Alexa routine

You can simply move the conditions from the condition section to the action section…

- alias: blah
  trigger:
    blah_blah
  condition:
    - condition: whatever
      whatever_params
  action:
    - first_step

to:

- alias: blah
  trigger:
    blah_blah
  action:
    - condition: whatever
      whatever_params
    - first_step
1 Like

that does seem to work… who would have thought one can just mix up the order of things like that?!

One who read the documentation on scripts. :wink:

It’s not really “mixing up the order of things.” It’s selectively using the options available – in this case, using the automation’s condition section and/or a script condition step. (Note that the action section of an automation is really a script.)

yip it explains it nice and clear… conditions within scripts. Simple!
Done, tested and working just the way it’s supposed to. :+1:

Thank you for pointing me in the right direction.

1 Like

Hi Phil,
if I update the title of this forum with:
‘Runnin an automation within a “PYTHON” script’ could you be able to help me?
How can I call the automation.trigger within a pyhon script?
Hope you can help me.
Thank you in advance.

If you read the doc page on Python Scripts, and follow the links therein, pretty much you should find all the answers. The only thing that seems to be missing is the optional parameter to automation.trigger, which is skip_condition, but you can find info on that if you go to the SERVICES tab of the Developer Tools page (in the HA UI) and select the automation.trigger service.

But here’s a shortcut for you:

hass.services.call("automation", "trigger", {
    "entity_id": "automation.XXX",
    "skip_condition": False,
})

If you want it to skip (i.e., ignore) the automation’s condition section, then either set skip_condition to True, or just remove that line from the call.

1 Like

Ciao Phil, thank you so much for your prompt reply… I’m so new (I’m joking with HA in the last week) so today I’ve started giving a look to python scripts in order to front a binary sensor’s issue and I’m still trying to understand how to activate python scripts (at moment I’ve done it trought automations.yaml because I didn’t understand how to start them “calling the service” (inside configuration.yaml).
Thanks a lot, really appreciate an hand like yours. Cheers!