How to turn On/Off Raspberry Pi GPIO when not_home (Away Mode)

How to setup configuration.yaml to turn Off Raspberry Pi GPIO16 when I am “not_home”, and turn on when I am “home”? I want to turn off the RPI GPIO16 pin when I am not home, and turn GPIO16 on when I come back home. I already setup a switch that GPIO16 can be toggled from HA, wonder how to add not_home/home ability. Thank you!

Switch setup

switch:

  • platform: rpi_gpio
    ports:
    16: GPIO16

Just got this example from HA example page and looks like it can turn lights off in Away mode

Turn off lights when everybody leaves the house

  • alias: ‘Rule 2 - Away Mode’
    trigger:
    platform: state
    entity_id: group.all_devices
    to: ‘not_home’
    action:
    service: light.turn_off
    entity_id: group.all_lights

You will need something to track. Most likely your phone. There are many methods and most of them do not work well. Though combining many trackers with a Bayesian sensor seems to work well for some.

Trackers I’ve tried:

iOS: does not update locations reliably or often enough.
Nmap: worked well for months then stopped being reliable, possibly due to an iOS update.
Home Kit: worked well for a week or two then home / away started being unreliable / not triggering
Owntracks: GPS tracking only, not reliable would wander all over the place at night. However coupled with a bunch of iBeacons scatttered around the house it has been extreemly reliable at determining if I am home or away.

Also please edit your post and format your code as per the blue banner at the top of the page.

also be aware that ‘group.all_devices’ will include all devices discovered by home assistant. You would either need to limit the discovery of devices to only those you want to use for this automation or narrow your trigger down to the appropriate devices.

I was thinking that HA app on the smart phone will ask for permission for “location services” and HA can then detect the away/home based on the smart phone response. Does this feature work?

What type of smart phone?

I did not find the iOS app reliable.

Hi Tom,

I am using iPhoneX. I am using Amazon cloud CAM with home away feature enabled recording Only if I am not in the house, so far it has been working. Upon the first installation, I enabled “location service”, I guess the CAM use the decision based on my iPhone location service hint? Can HA app get the same location service hint from the iPhone?

Install the iOS Home Assistant app and enable location tracking in the settings and Home Assistant discovery should detect it.

Thanks Tom!

Going back to the original question, would you give me an example for the “configuration.YAML” how should I code my GPIO16 switch with the ios away feature, to turn it off when no body is home?

I plan to use GPIO to turn off my furnace when nobody is home. If detection is delayed by 10min to 15min, it is still acceptable.

These two automatons should do what you want:

automation:
  - id: heater_off_when_away
    alias: 'Heater Off When Away'
    trigger:
      platform: state
      entity_id: device_tracker.[your tracker ID] # e.g.  device_tracker.aarons_iphone
      to: 'not_home'
      for:              #  <----- This may help prevent false triggering but is not required
        minutes: 5
    condition:
      condition: state   #  <----- Only if the switch is on
      entity_id: switch.gpio16
      state: 'on'
    action:
      service: switch.turn_off    #  <----- Turn it off
      entity_id: switch.gpio16

  - id: heater_on_when_home
    alias: 'Heater On When Home'
    trigger:
      platform: state
      entity_id: device_tracker.[your tracker ID]
      to: 'home'
    condition:
      condition: state   #  <----- Only if the switch is off
      entity_id: switch.gpio16
      state: 'off'
    action:
      service: switch.turn_on  #  <----- Turn it on
      entity_id: switch.gpio16

Note the lower case for the gpio switch entity ID. HA will convert the switch configuration listed in your fist post to this. It is important. HA is case sensitive.

1 Like

Thanks a lot for your time Tom! Will give it a try.