Automation of turning lights on when GPIO « GP2 » is closed

Hi, I new to this raspberry pi. I have an alarm system Elk M1 that currently turns on the living room light when I enter the drive way by sending signal to a x10 wall switch.

I am converting to TP-Link hs200 wall switch and I want my Elk M1 to close GPIO pin #2 on the raspberry pi board that would in turn reacts and turn on the light sending signal to the hs200.

My hs200 is configured in the raspberry and it works with the home assistant.

Now I need to understand how I can automate the process of continuously read the status of GP2 and turn the lights if GP2 is detected as closed.

See Raspberry Pi GPIO Binary Sensor.

Is there an example on how to initially test a script and how to make it run for ever.

From the home assistant web page, I don’t know where to start.

You don’t need to do any of that. You just need to add to your configuration something like the following:

binary_sensor:
  - platform: rpi_gpio
    ports:
      2: elk_m1

and restart HA. HA will then start monitoring that pin for you and create an entity named binary_sensor.elk_m1 that indicates the state of the pin. Then you use that in the trigger of a normal HA automation that turns on the light.

I’ll admit, however, that although I did use a RPi for quite a while when I started using HA, I never tried to use the GPIO pins, so I’m basing this purely on the docs (and some light reading of the code.)

Thanks

I will try that and read the doc :slight_smile:

1 Like

I succeeded on modifying configuration.yaml on the second try. Now I see it in the home automation as binary_sensor.elk_m1.

I don’t know what to specify for Above and Below as well as for the other two. Is there a way to monitor the current value by command line in putty or in hassio commands or else where?

I located the status of the trigger in the image below. It is “on” when not shorted and “off” when shorted. Would that be 0 for “on” and 1 for “off”. What value do you think it is for Above and Below. Are both needed to be specified.


I tried different test with above and below using 0 and 1 without success. This url https://www.home-assistant.io/integrations/binary_sensor/ do specify it 0 and 1.

I can see in automations.yaml, the result of the automation created through the web interface.


I guess I could change the above to something else. I wonder is ‘0’ is not actually turning the value 0 to a string?

Tried editing the yaml file like this but still no result triggering this automation.

You don’t use a numeric_state trigger with a binary_sensor. You use a state trigger.

There’s a bug with HA using GPIO pins as input. I’m no expert, but from what I can tell, the rpi_gpio sensor does not poll the pins to see if they’re open or closed. It tries to detect the change from high to low (or low to high.)

For whatever reason, that process is very unreliable. There has been an open issue on this for two years now:

It looks like there may now be some hope a fix will get merged one of these days:

I have had no luck reading binary GPIO input. I’ve tried numerous hardware and software tweaks, and HA does catch some of the changes, but misses far too many to be of any use at this point.

I am still struggling to find the proper trigger command to catch the port change.

I can see that in developper tools section, the status of my sensor change to off on most try, changing between on to off. It just how to write the trigger that is blocking me. Still searching for examples…

  • id: ‘1574742374576’
    alias: ELK-M1-Driveway
    description: Trigger when entering driveway
    trigger:
    entity_id: binary_sensor.elk_m1
    platform: state
    to: 1
    condition: []
    action:
    • device_id: e72133adae5b4ffca7aabd4b5a45a7c5
      domain: switch
      entity_id: switch.salon
      type: turn_on

Please format the code properly so we can read it.

But I’m guessing you need to change

to: 1

to

to: 'on'

Can only edit the automations.yaml file and press on my board button or do I need to reboot hass.

With this configuration, I get no result.

Restart Home Assistant.

I got it to work. :grinning:

I needed to instructions:
from: ‘on’
to: ‘off’

Since my project is to open the lights when I drive in the driveway, it seems that port change detection is likely to happen most of the time. My ELK M1 alarm system will for sure close the port for a determined amount time. The automation seems to detect the change quite often even though I did see with some trials the it would get stuck on “off” until I press again my button. I only want to turn the lights so I can trigger the port a couple of times to make sure it goes back to “on” state. That won’t matter is the raspberry send a couple of turn on messages to my tp-link hs200 switches.

I didn’t restart the the Home Assistant, but instead reloaded the automations.

That’s the log of on/off trials. It corresponds correctly to most trials.

In the GUI, it looks like this.

That’s unlikely, but not impossible. In general a binary_sensor (by definition) only has two states: 'on' and 'off'. So if it is changing to: 'on', then it almostly certain was 'off' before, so specifying from: 'off' is generally redundant. Now having said that sometimes sensors, even binary ones, can be 'unknown' or 'unavailable', so…

Yes, that can work in limited scenarios. If you hand edit YAML files, get in the habit of restarting HA whenever you make changes, or someday you’ll find yourself chasing your tail. :confused: I’ve helped too many people that made this mistake to say otherwise.

Thanks for the advice