Home / Away Switch Boolean using automation

Hello All,

Thanks for the help so far. I am up and running, I have a few tasmotised Sonoffs in action and I have just built a multi sensor. And I have life 360 device tracking working.

Now its time to automate some of this. And I want to use the home away status as a condition for a lot of the automation. So if me or the family are home our PIRs will turn lights and stuff on, and if we’re not home I want the PIRs to let us know there are unauthorised movements in the house and possibly send camera images. (I need to walk before I can tun though!)

So I am going to set up a boolean switch where it will switch on when our life 360 app is in the home state. But there is also this newance of person now on here so I am a bit unsure which way to tackle it.

I currently have a location set up card for all the family. And I have set a map tab up too. (bit stalkerish but peace of mind when my 13 year old visites her friends or is at her clubs etc) Allo good.

I have set up a boolean for the wifes phone as per this in the configuration.yaml

input_boolean:
  kimhome:
    name: Kim is home
    initial: off
    icon: mdi:human-female

Then I have added it to my automations card

image

Then I have added the following to the automations.yaml

# Kim Boolean Home State
- alias: 'mark Kim home'
  condition:
    condition: state
    entity_id: input_boolean.kimhome
    state: 'off'
  trigger:
    - platform: state
      entity_id: device_tracker.life360_kimmie
      to: 'home'
  action:
     service: input_boolean.turn_on
     data:
       entity_id: input_boolean.kimhome

- alias: 'mark Kim away'
  condition:
    condition: state
    entity_id: input_boolean.kimhome
    state: 'on'
  trigger:
    - platform: state
      entity_id: device_tracker.life360_kimmie
      to: 'not_home'

  action:
     service: input_boolean.turn_off
     data:
       entity_id: input_boolean.kimhome

Now I have used the life360 entity, but I have also set up the person entity and linkied the life360 to this too, so they will both show the location status as home or not_home or other locations I have named in the configuration.yaml

But the boolean is not neing updated when the phone is in the home location.

Any ideas and or suggestions would be greatlyh appreciated.

Thanks in advance.

Rob

1 Like

Any suggestions would be appreciated. Even if there is some reading somewhere on how to use the person entity in automations. I took the Boolean example from the ha guide. But I may have broken something…

Thanks in advance.

Rob

I haven’t done a masive amount with device trackers, but it might be worth removing your conditions.

I have some lights working with this:

- alias: Im Home light me up
  trigger:
    platform: state
    entity_id: device_tracker.wills106_willsphone
    from: 'not_home'
    to: 'home'
  condition:
    condition: or  # 'when dark' condition: either after sunset or before sunrise
    conditions:
      - condition: sun
        after: sunset
#        after_offset: "-00:15:00"
      - condition: sun
        before: sunrise
  action:
    service: homeassistant.turn_on
    entity_id: group.HallLights

So you should be able to just swap entity_id: group.HallLights with entity_id: input_boolean.kimhome and change your device tracker name.
You can also get rid of my condition. Then just invert it for your other state.

Thanks Wills, I have tried but to no avail. does anyone have any idea how to set up the wi-fi home boolean as Rob describes in his video at 3:23 https://www.youtube.com/watch?v=SuoSXVqjyfc&t=185s To update a location based on whether your phone is logged onto wi-fi? If it not possible to do it by life360 tracker.

Thanks in advance.

Rob

Can we take a step back? What ultimately are you trying to do? I.e., why do you want an input_boolean to represent when a person is home or not when the device_tracker (or person) entity can already tell you that? If you do simply just want a “boolean”, then I’d suggest a Template Binary Sensor instead. Something like:

binary_sensor:
  - platform: template
    sensors:
      kimhome:
        friendly_name: Kim is home
        value_template: "{{ is_state('device_tracker.life360_kimmie', 'home') }}"

No need for an automation to keep it up to date.

2 Likes

Thanks for this idea.

I am a bit new to this so I am trying to replicate other setups I have seen etc.

The idea behind the boolean, was that I can manually put people at home if I need to, also I am trying to work towards node red as it is a bit more at my level (not a code guru by a long shot) and a lot of the reading and youtube videos use the input booleans as triggers for automations.

I have after a bit of experimenting and reading sussed out the code to make this work.

Keep up the great work on the component too as it awesome. Liking the fact it will be an official one too as well.

Hello,

What was the code?

The code here is for the input boolean

input_boolean:
  kimhome:
    name: Kim is home
    initial: off
    icon: mdi:human-female

I am using the life360 component as per Phils Github Repo. All working great.

Let me know if you have an specific questions I might be able to help now. :slight_smile:

  robhome:
    name: Rob is home
    initial: off
    icon: mdi:human-male

I setup a manual update of the home_state by an actionable notification.

If have setup a people count (a person has one or more device tracker allocated):

sensor:
  - platform: template
    people_count:
        entity_id:
          - person.01
          - person.02
          - person.03
        friendly_name: "People at home"
        value_template: >-
          {{ states.person | selectattr('state', 'eq', 'home') | list | count | int }}

And when the last person leaves the home there is actionable notification if you want to update the home_state. Done in Node Red:

[{"id":"67ab537c.647fd4","type":"function","z":"f3da5939.ded228","name":"Derives notificatation_target (iphone_name)","func":"var m_out=[]; //array for message objects\n\n    var newmsg={payload:{service:'ios_' + msg.topic.substring(7) + 's_iphone',entity_id:'none',occupancy:msg.payload}};\n    m_out.push(newmsg);\nreturn[m_out];","outputs":1,"noerr":0,"x":790,"y":500,"wires":[["3a272705.6951c8"]]},{"id":"95f4568c.e8f688","type":"api-current-state","z":"f3da5939.ded228","name":"Noone at home anymore?","server":"1f3f4153.46120f","outputs":2,"halt_if":"0","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"sensor.people_count","state_type":"str","state_location":"","override_payload":"none","entity_location":"","override_data":"none","x":450,"y":500,"wires":[[],["67ab537c.647fd4"]]},{"id":"3a272705.6951c8","type":"api-call-service","z":"f3da5939.ded228","name":"Actionable notification to iPhone","server":"1f3f4153.46120f","service_domain":"notify","service":"","data":"{\"message\":\"You are the last one who left home. Do you want to update the home_state?\",\"data\":{\"subtitle\":\"[LONG PRESS] for actions\",\"push\":{\"category\":\"lefthome\"}}}","mergecontext":"","output_location":"payload","output_location_type":"msg","x":1150,"y":500,"wires":[[]]},{"id":"cdf1f359.8a303","type":"server-state-changed","z":"f3da5939.ded228","name":"Someone leaves the house","server":"1f3f4153.46120f","entityidfilter":"person","entityidfiltertype":"substring","outputinitially":false,"state_type":"str","haltifstate":"home","halt_if_type":"","halt_if_compare":"is_not","outputs":2,"output_only_on_state_change":true,"x":190,"y":500,"wires":[["95f4568c.e8f688"],[]]},{"id":"1f3f4153.46120f","type":"server","z":"","name":"HA@Virtualbox","legacy":false,"hassio":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true}]

And here is the part for the actionable notification.

ios:
  push:
    categories:
      - name: Leaving home
        identifier: 'lefthome'
        actions:
          - identifier: 'LEAVING_HOME'
            title: 'Update home_state'
            activationMode: 'background'
            authenticationRequired: true
            destructive: true
            behavior: 'default'
          - identifier: 'IGNORE'
            title: 'Ignore'
            activationMode: 'background'
            authenticationRequired: true
            destructive: false
            behavior: 'default'

You then need to capture the event:

automation:
  - alias: Update home state
    trigger:
      platform: event
      event_type: ios.notification_action_fired
      event_data:
        actionName: LEAVING_HOME
    action:
      [...]

thanks trying to get my head around all of this

Follow up question, How would I throw an OR into that? Im new at this. I want to be HOME if my GPS says I am home OR my wifi is connected.