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!
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
that is excellent, thanks Phill!
This is exactly what I was trying to do.
You’re a 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
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.
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.
Thank you for pointing me in the right direction.
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.
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!
well… let me take full advantage of your help Phil
my problem is with bell of my intercom, I’ve done this python script which works correctly when someone push at intercom… trough HA something weird happens with my binary sensor so I don’t always receive notifications with Telegram (which works correctly with other automations) so my idea was to use my python script to start the automation with telegram automatically.
Can you check if the script will be correct like this?
Original Python script:
#!/usr/bin/env python
from time import sleep
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
button1=13
GPIO.setup(button1,GPIO.IN,pull_up_down=GPIO.PUD_UP)
while(1):
if GPIO.input(button1)==0:
print "Intercom ring"
sleep(.80)
New python script should be updated replacing the “print command”, correct? Is it necessary an “entity_id”? Below the new script:
#!/usr/bin/env python
from time import sleep
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
button1=13
GPIO.setup(button1,GPIO.IN,pull_up_down=GPIO.PUD_UP)
while(1):
if GPIO.input(button1)==0:
hass.services.call("automation", "trigger", {
"entity_id": "automation.bussata",
"skip_condition": True,
})
sleep(.80)
Phil sorry I’m really new… is “entity_id” due? Because I don’t know what entity I have to involve… and quotation marks on the automation name must be written?
Because the goal is to receive the notification and in the automation the actions is as follow:
action:
service: notify.notifichetelegram
data:
message: Hanno Citofonato
Can I call directly the service: notify.notifichetelegram? Which would be easier? smile:
Welcome to the HA community!
First, you should probably start a new topic to request help specific to your unique scenario.
Second, the python_script
integration only allows a very small subset of things that can typically be done with Python. From the docs:
The scripts are run in a sandboxed environment.
So nearly everything you did in your script can’t be done in this kind of script.
If ultimately you’re having an issue with the Raspberry Pi GPIO Binary Sensor, then you should ask for help with that, rather than going off in this potentially unnecessary, complicated direction. I have no experience with using RPi GPIO, so I really can’t help with that. And even if the answer doesn’t lie there, I suspect there may be a much easier solution with automations, template sensors, etc. than writing some custom Python code, inside or outside of HA.
Good luck!