How to create a Home Assistant Automation with zones and presence detection


Location tracking and zones is one of the most powerful features in home assistant and can open up the door of so many automations, in this blog post I’m going to share 5 useful automations.

To create an automation in home assistant with zones and presence detection you will need two things: A device tracker that can be your mobile phone or a dedicated device like a tile tracker and you will need to define zones, by default home assistant has your home defined already.

 

Zone

Your home zone is usually defined during setup, in case you want to change it go to Configuration and General.

Each zone has the following parameters:

  • Longitude
  • Latitude
  • Radius

They also have a name and an mdi icon that can be displayed when you view the zone in a map.

 

Additional zones can be configured in the user interface but also in the configuration.yaml file.

In my setup I created a separate zone file called zone.yaml and add this line in my configuration.yaml

zone: !include zones.yaml

Restart Home Assistant before making more changes.

 

This is an extract out of my zone.yaml file

- name: School
  icon: mdi:school
  latitude: !secret school_latitude
  longitude: !secret school_longitude
  radius: 130

  - name: !secret coffee_shop_name
  icon: mdi:coffee
  latitude: !secret coffee_shop_latitude
  longitude: !secret coffee_shop_longitude
  radius: 25

I’m using the secret file feature to conceal my sensitive information.

 

Person

To add a person in home assistant you navigate to Configuration –> Persons

Click the plus button, you can now link a person to a user if they have a login to home assistant. Add a device tracker to be linked to the person.

Now in your developer tool you will find the person.name entity.

Device Tracker

I device tracker in home assistant has several properties but mainly your geolocation that then is used to trigger events when the geolocation is within the radius of a zone.

I’m using Life 360, still early days to give a complete review but it is free to use. You can also use apple iCloud as an option if you have an IOS device, full details are on the home assistant page.

 

Automations

Front Door Porch Light

This automation will turn on my light in my front door porch giving me the necessary light to find my keys, when the sun is below horizon. I have a shelly 1 in the casing of the light that is turned on by the automation.

This automation is an example of how to use the home zone.

alias: 'Front Porch Lights turn on when someone is home.'
trigger: 
  platform: zone
  entity_id: device_tracker.life360_giordano_langella
#entity_id: person.natureboss
  zone: zone.home
  event: enter
action:
  service: light.turn_on
  entity_id: light.front_porch
condition:
  condition: state
  entity_id: sun.sun
  state: 'below_horizon'

 

Coming home from School

In this automation when someone leaves school and is heading back home our google mini will give out a message using text to speech.

alias: 'Coming Back Home from School'
trigger: 
  platform: zone
  entity_id: device_tracker.life360_giordano_langella
#entity_id: person.natureboss
  zone: zone.school
  event: leave
action:     
  service: tts.google_say
  data:
    entity_id: media_player.kitchen_speaker
    message: Giordano is coming back home

Enter Coffee Shop

This automation gives me a reminder of the current rules in terms of sanitising hands and wearing a face covering. A notification is triggered on your phone.

In order to use notification would would need to have the following in our configuration.yaml

default_config:

cloud: this is used if you are connected via Nabu Casa when not at home.

ios:  This is used on IOS devices to enable advanced notification, like actionable notifications.

Robot Vacuum Start / Stop

This automation is made up by two files:

The first automation trigger a service that started the robot vacuum as soon as I leave my home zone. In order to change zones you need a data connection when you are outside. If you go offline your zone will not change.

alias: 'Start Cleaning when house is empty'
trigger: 
  platform: zone
  entity_id: device_tracker.life360_giordano_langella
#entity_id: person.natureboss
  zone: zone.home
  event: leave

action:
-  service: vacuum.start
   data:
     entity_id: vacuum.robovac
-  service: notify.mobile_app_giordano_s_iphone_7
   data:
     message: "Robovac Started Cleaning"

 

The second automation reverses the first, it triggers a service to send the robot back to charging base when I return back home.

alias: 'Stop Cleaning when house is not empty anymore'
trigger: 
  platform: zone
  entity_id: device_tracker.life360_giordano_langella
#entity_id: person.natureboss
  zone: zone.home
  event: enter

action:
-  service: vacuum.return_to_base
   data:
     entity_id: vacuum.robovac
-  service: notify.mobile_app_giordano_s_iphone_7
   data:
     message: "Robovac Stopped Cleaning and is returning to base"

 

Pick something up from the store

This automation is the most complex, but could be the most valuable.

When my partner enters a specific zone like a supermarket or a shopping centre that has been previously defined in the zone.yaml file. A notification will be sent to the other half / family members. They have the opportunity to send back another notification telling them to pick up something from the store.

First you will need to add a push category in your configuration.yaml file

ios:
  push:
    categories:
      - name: Message
        identifier: 'message'
        actions:
          - identifier: 'MESSAGE'
            title: 'TEXT'
            activationMode: 'background'
            behavior: 'textInput'
            textInputButtonTitle: 'Text Partner'
            textInputPlaceholder: 'Placeholder'

 

 

Then you can add these two automations, the first is to send the notification to the family member

alias: 'Partner is at the store, send a notification / msg'

trigger: 
  platform: zone
  entity_id: device_tracker.life360_giordano_langella
#entity_id: person.natureboss
  zone: zone.sainsbury
  event: enter
action:
  # sent a notification to giordano's iphone
  service: notify.mobile_app_giordano_s_iphone_7
  data:
    message: "Your Partner has just got to the store"
    data:
      push:
        category: message

 

The second is to send the reply back to the person that entered the zone.

alias: 'Send a Notification to partner'
trigger: 
  platform: event
  event_type: ios.notification_action_fired
  event_data:
    actionName: MESSAGE
action:
  service: notify.mobile_app_giordano_s_iphone_7
  data_template:
   message: '{{ trigger.event.data["textInput"] }}'

 

Gio

Gio loves rabbits, smart home tech, WWII, travelling to Thailand & my favourite pizza is margherita with parma ham!

Recent Posts