Running an automation within a script

well… let me take full advantage of your help Phil :sweat_smile:
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? :sweat_smile: smile: :rofl:

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!

so… all bad news then! Binary Sensor gives me a state and keep it… consider my intercom, if someone push it should be ON (or OFF if inverted) just for the time of the ring and it is not like this, it keeps staying and I cannot change it and likely it is not this the only problem. Do you think my script could be within the small subset of things? :blush: Thanks for all the replies Phil, really…

p.s.: where I can find the bases to understand how to call HA by external scripts? Thx

No. You can’t import modules, you can’t sleep (AFAIK), you can’t interact with GPIO (directly). But you can call a service! :wink: And there’s actually quite a bit more you can do, but it’s not really intended for this use case.

Again, if the binary sensor isn’t working as you expect, then you should ask for help with that. There are many people that have used GPIO sensors who would know how to handle this problem. I’m sure you can’t be the first wondering how to do this. And, unfortunately, it’s very unlikely that anyone with that knowledge will be looking at this topic, since it’s unrelated and there are already 20+ replies. Really, start a new topic. That’s what the forum is for. :slightly_smiling_face:

1 Like