Home/Away switch

Hello,
I see a lot of examples for triggering an “Away Automation” based on presence.

How can I create a simple on/off switch so it also overrides the other triggers?

I.e if my_android is away trigger away mode, but if I left my phone at work press a button and set home mode

Thanks,
M.

1 Like

You can create an input_boolean or input_select:

3 Likes

I’d personally not try and change the away/home status but have that status update an input_boolean that you use for your automations.
Being an input_boolean you can override it…

2 Likes

@lolouk44 could you elaborate on that?

Maybe like this. (Straight from my HA)

In the configuration.yaml

input_boolean:
  ejhome:
    name: ej home
    initial: off

Then in my automation

- alias: 'mark ej home'
  condition:
    condition: state
    entity_id: input_boolean.ejhome
    state: 'off'
  trigger:
    - platform: state
      entity_id: device_tracker.eric_a8967564d072
      to: 'home'
    - platform: state
      entity_id: device_tracker.eric_phone
      to: 'home'
  action:
     service: input_boolean.turn_on
     data:
       entity_id: input_boolean.ejhome

- alias: 'mark ej away'
  condition:
    condition: state
    entity_id: input_boolean.ejhome
    state: 'on'
  trigger:
    - platform: state
      entity_id: device_tracker.eric_a8967564d072
      to: 'not_home'
    - platform: state
      entity_id: device_tracker.eric_phone
      to: 'not_home'
  action:
     service: input_boolean.turn_off
     data:
       entity_id: input_boolean.ejhome

Then I get this in my frontend
image

1 Like

Thanks @ericleejoe.
I’d like to create something for the general house state, not for a single person.

Eventually, someone who is not me or my wife might be in the house, right?
I.e babysitter with the kids.
So I’d like the option to override the my_phone_is_away && my_wife_is_away states.

The way I’ve done this is with an input select, and it’s automated based on what is going on at the house.

For example, before I (or last person at home) leave home, a “Leaving Home” script is activated via Alexa or my tablet which changes the state from “Home” to “Away”, turns on my security camera via a WeMo switch, turns off any lights that are on, including TV etc.

When I arrive home, another automation (via the iOS app) changes the state from “Away” to “Home” and switches the security camera switch off. If someone else arrives before me, this is detected via NMAP and performs the same changes.

Since I live on my own, but occasionally have my partner or brother stay with me, I’ve also implemented a guest switch, so once on, certain automations are performed as if that was me.

Some examples:

Occupancy Input Select:

occupancy:
  name: Occupancy
  options:
    - Home
    - Away
  initial: Home

Leaving Home Script:

leaving:
  alias: 'Leaving Home'
  sequence: 
    - service: homeassistant.turn_on
      entity_id: 
        - switch.camera
        - script.tvoff
        - script.bedroomlights_off
        - automation.guest_notification
    - service: light.turn_off
      data:
        entity_id: light.study
        transition: '3'
    - service: input_select.select_option
      data:
        entity_id: input_select.occupancy
        option: "Away"

Guest Input Boolean:

guest_mode:
  name: 'Guest Mode'
  initial: off
  icon: mdi:account-plus

Automation for Input Boolean:

- alias: Guest Mode ON
  initial_state: 'on'
  hide_entity: true
  trigger:
    platform: state
    entity_id: input_boolean.guest_mode
    to: 'on'
  action:
    service: homeassistant.turn_on
    entity_id: group.guestcontrols

- alias: Guest Mode OFF
  initial_state: 'on'
  hide_entity: true
  trigger:
    platform: state
    entity_id: input_boolean.guest_mode
    to: 'off'
  action:
    service: homeassistant.turn_off
    entity_id: group.guestcontrols

Automation to return HA to “Home” based on me only:

- alias: Webcam Off
  initial_state: 'on'
  trigger:
    platform: state
    entity_id: device_tracker.iphone
    to: 'home'
  condition:
    condition: state
    entity_id: switch.camera
    state: 'on'
  action:
    - service: homeassistant.turn_off
      entity_id: 
        - switch.camera
        - automation.guest_notification
    - service: input_select.select_option
      data:
        entity_id: input_select.occupancy
        option: "Home"

Automation to return HA to “Home” based on Guest’s status:

- alias: Webcam Off Guest
  initial_state: 'off'
  trigger:
    platform: state
    entity_id: group.guests
    from: 'not_home'
    to: 'home'
  condition:
    condition: and
    conditions:
      - condition: state
        entity_id: input_boolean.guest_mode
        state: 'on'
      - condition: state
        entity_id: switch.camera
        state: 'on'
  action:
    - service: homeassistant.turn_off
      entity_id: switch.camera
    - service: input_select.select_option
      data:
        entity_id: input_select.occupancy
        option: "Home"

Like I said, guests are tracked via their MAC address using NMAP and part of a group.

Hope this gives you something to work with!

8 Likes

Thanks @icaman004!
I think in my case (and at least for now) the Occupancy input_select is enough.
I just want it to work as simple as a house alarm, that the last leaving person would set as ON/AWAY (meaning, we are leaving the house) or OFF/HOME we just entered.

Having read your post, I found some similarities between our use cases. I came up with the idea to work with a bayesian_sensor that calculates the likelihood of the house being “alive” or “occupied” and turns on if a certain probability threshold has been met.

It could be that this approach is an overkill for what you’re looking for, nevertheless it brings some advantages compared to a device_tracker-only based approach:

  • you might want to trigger surveillance cameras on even if your phone is at home
  • what if you leave your registered phone at home while going for a walk? The sensor would not trigger in that case.
  • guests (who are not part of the device_tracker groups) would not be taken into account

Likewise I use multiple criteria which can determine whether the house is occupied or not. You can feel free to extend this list based on your personal habits…

Here’s my code in case you’re interested (I’ve actually created a package for it):

binary_sensor:
  - platform: bayesian
    name: 'house_not_occupied'
    prior: 0.4  # 40% chance that house is not occupied (12h out of 24h a day) + office days
    probability_threshold: 0.88
    observations:
     # We are not in the house -> very likely there's no motion recorded by motion and door/window sensors
      - entity_id: 'sensor.no_house_activity'
        prob_given_true: 0.8 # 80% chance that if we're out of the house there's no motion recorded in the house
        prob_given_false: 0.1 # 20% chance that if we're NOT out of the house there is no motion recorded in the house
        platform: 'state'
        to_state: 'True'
      - entity_id: 'group.family_devices'
        prob_given_true: 0.95  # 95% chance that if we're out of the house there's no family device connected
        prob_given_false: 0.2 # 20% chance that if we're NOT out of the house the devices are not connected
        platform: 'state'
        to_state: 'off'
     # We are not in the house -> very likely the NB is not connected to LAN
      - entity_id: 'device_tracker.lenovonblan'
        prob_given_true: 1.0 # 100% chance that if we're out of the house the NB is not connected to LAN
        prob_given_false: 0.6 # 60% chance that if we're NOT out of the house the NB is not connected to LAN
        platform: 'state'
        to_state: 'not_home'
     # We are not in the house -> very likely the Philips TV is not running
      - entity_id: 'device_tracker.philpstv'
        prob_given_true: 1.0 # 100% chance that if we're out of the house the TV is not running
        prob_given_false: 0.85 # 60% chance that if we're NOT out of the house the TV is not running
        platform: 'state'
        to_state: 'not_home'


sensor:
##### Helpers for bayesian sensor

  - platform: template
    sensors:
      no_house_activity:
        friendly_name: No activity in house for 1h+
        value_template: "{{ is_state('timer.no_motion', 'idle') }}"

timer:
  no_motion:
    name: 'House Idle Timer'
    icon: mdi:timer

automation:
  - alias: 'timer start in case of no house activity'
    trigger:
      - platform: state
        entity_id: binary_sensor.fibaro_system_fgms001_motion_sensor_sensor
        to: 'off'
      - platform: state
        entity_id: binary_sensor.fibaro_system_fgms001zw5_motion_sensor_sensor
        to: 'off'
      - platform: state
        entity_id: binary_sensor.fibaro_system_unknown_type0702_id1000_sensor
        to: 'off'
    action:
      - service: timer.start
        data:
          entity_id: timer.no_motion
          duration: 01:00:00
6 Likes

Thanks @mastermarkush, that’s definitely something I’ll consider for tje full automation.
But first I want the old-style button on the dashboard, where last person to leave sets it on AWAY, and the first to enter sets is on HOME.

You can put your phones in to a group and operate an input select based whether that group is away or home. (You will need nmap tracker for this). No phones in the group present will cause the group to show as ‘away’. Any of the phones present will cause the group to show as ‘home’

From my automations, here are the two that set the input select named ‘house_occupancy’ to ‘vacant’ or ‘occupied’. There are some built in delays to make sure iPhones just haven’t dropped off the network :wink:

Other automations that actually control things (e.g. turn off lights or turn down heat) are all triggered by changes in the status of the input select named ‘house_occupancy’.

- alias: Presence - no one home
  trigger:
    platform: state
    entity_id: group.phones_presence
    from: 'home'
    to: 'not_home'
    for:
      minutes: 15
  action: 
    service: input_select.select_option
    data:
      entity_id: input_select.house_occupancy
      option: 'vacant'
      
- alias: Presence - someone home
  trigger:
    platform: state
    entity_id: group.phones_presence
    from: 'not_home'
    to: 'home'
    for:
      minutes: 5
  action: 
    service: input_select.select_option
    data:
      entity_id: input_select.house_occupancy
      option: 'occupied'
2 Likes