Python controlling GPIO

switch
Tags: #<Tag:0x00007f1b95298260>

#1

Hi all. I am struggling to run a python script that will switch my gpio ports on for 1 second.
I get error on the first line of code import RPi.GPIO as GPIO
I am using this solution on my rPi3B and works fine. I wanted to integrate and automate.
So I installed hassio to run the code with ifttt and so on…
I can use following code in configuration:
switch:

  • platform: rpi_gpio
    ports:
    6: GarageON_OFF_remote
    10: Power1ON_remote
    27: Power1OFF_remote

BUT it has main limitation for me that the gpio will stay on.
Can anyone help me please?


#2

Hava look at the automation docs. You can quite easily turn a GPIO pin on for one second:

  - alias: 'Garage Remote'
    trigger:
   # you can trigger on whatever you want here.
    action:
      - service: switch.turn_on
        entity_id: switch.garageon_off_remote
      - delay: 00:00:01
      - service: switch.turn_off
        entity_id: switch.garageon_off_remote

Or if you just wan to toggle the GPIO from the front end web interface you can use a script:

garage_remote:
  sequence:
    - service: switch.turn_on
      entity_id: switch.garageon_off_remote
    - delay: 00:00:01
    - service: switch.turn_off
      entity_id: switch.garageon_off_remote
  

#3

Thank you. I tried second way and also simplified. but it is not working:
error>
Error loading /config/configuration.yaml: mapping values are not allowed here
in “/config/configuration.yaml”, line 77, column 8

switch:
- platform: rpi_gpio
  ports:
     6: Garage

group: !include groups.yaml
automation: !include automations.yaml
script: !include scripts.yaml
 Garage:
  sequence:
    - service: switch.turn_on
      entity_id: switch.Garage
    - delay: 00:00:01
    - service: switch.turn_off
      entity_id: switch.Garage

#4

Paste this bit in your scripts.yaml file, not the main configuration.yaml file (if you dont have one, make one):

garage:
  sequence:
    - service: switch.turn_on
      entity_id: switch.garage
    - delay: 00:00:01
    - service: switch.turn_off
      entity_id: switch.garage

Be very careful with the number of preceding spaces (not tabs!), also note the the use of lower case letters.

If you look at the developer tools menu (left hand side lower down on the main web page) and press the states button that looks like this: <> it will show you all your entity names and their states. Note how your switch id was converted to lower case.


#5

Tom thank you. It works. But not quite good.
On restart of system it started the script for no reason to me. (maybe the missing cleanup of gpio?)
2nd question can I somehow combine it in just a script or hide the switch from home screen?

something like this does not work:

garage:
- platform: rpi_gpio
    ports:
      6: garage
  sequence:
    - service: switch.turn_on
      entity_id: switch.garage
    - delay: 00:00:01
    - service: switch.turn_off
      entity_id: switch.garage

Thx


#6

Be careful which GPIO you use.

The 'Pull" columns of this chart shows the state of the GPIOs before they have been initialised (i.e. before HA starts). The GPIOs with pull-up resistors will be high until HA starts. The ones with pull-down resistors will be low until HA starts:

Yeah that won’t work to hide the switches. The GPIO switch definition has to go under the switch section of your configuration.yaml file (or in the switches.yaml file if you use switch: !include switches.yaml).

Use customize to hide the switches:

customize:
  switch.garage:
  hidden: true

#7

Thank you very much for all!