Home automation, holidays special - Presence simulation


If you have read my 7-posts serie about home automation, you know I’m using Home Assistant on Raspberry Pi 3 and I have a fairly complicated setup, made simple by the level of integration that Home Assistant provides.

If you haven’t read the previous posts, this is a quick recap (with links):

In this post I’ll write about presence simulation routines, that can come up quite handy during holidays like Christmas, Easter, or any time you’re traveling and leaving your flat unattended.

At the end of this post we’ll be able to:

  • Make Home Assistant aware of our holidays through simple Google Calendar entries
  • Avoid waste by turning off the heating while we’re away
  • Simulate presence by turning on and off the lights at strategic times

Google Calendar

GCal

To make Home Assistant aware of my (and my girlfriend’s) movements, I’m using the Google Calendar integration. It’s very easy to setup and natural to maintain up-to-date. In fact, I was already using a Google Calendar to keep track of my holidays before.

Although it’s off-topic, it’s fair to mention that I’m also using the Google Calendar integration to let Home Assistant know when it’s a bank holiday where I live, so that some of the functionalities (e.g. the morning routine - I’ll write about it in a future post) are disabled.

As a matter of fact, the integration opens a world of possibilities (think about specific automations that are triggered by sport matches, live concerts, etc.).

The instructions to setup the integration are available on the component page. I’ll copy the more important bits here for convenience:

Generate a Client ID and Client Secret on Google Developers Console.

  • Follow the wizard for creating a new project.
  • When it gets to the point of asking “Which API are you using?” just click cancel.
  • Click on the tab “OAuth consent screen”.
  • Set “Product name shown to users” to anything you want, for instance “Home-Assistant”.
  • Save this page. You don’t have to fill out anything else there.
  • Click “Create credentials” -> OAuth client ID.
  • Set the Application type to “Other” and give this credential set a name, then click “Create”.
  • Save the client ID and secret as you will need to put these in your configuration.yaml file.

Once this is done, just use the values obtained in the last step in the configuration.yaml file:

google:
  client_id: *Value_created_from_steps_above*
  client_secret: *Value_created_from_steps_above*

Restart Home Assistant. It will show you a card to complete the Google authentication, and it will give you a URL and a code to enter. This will grant your Home Assistant service access to all the Google Calendars that the account you authenticate with can read. This is a Read-Only view of these calendars.

You are now able to edit the file google_calendars.yaml that Home Assistant autogenerates to keep track of all your calendars. Set track to true only for the ones that are going to be of any interest for your automations. There’s no need to track all your personal calendars.

For the purpose of this post, we can create a new calendar called “Holidays” and track that.

Mine looks like this:

[...]
- cal_id: REDACTED@group.calendar.google.com
  entities:
  - [...]
  - device_id: holidays
    name: Holidays
    track: true
    search: "#holiday"
[...]

The device_id is the name that I’ll be able to use in Home Assistant to refer to this calendar. search is the string that will be used to match events to be of interest for this device_id.

From now on, calendar.holidays will track all the events contained in that specific Google Calendar that contain the string “#holiday”.

Please note that I’m using “#holiday” because I use this calendar for other home-related events, too. You don’t need a search string if you setup a calendar just to track holidays.

Google calendar integration

Now that we have a way for Home Assistant to know when we’re on holidays, let’s write the routines that will be used during these periods.

Heating

The first thing that came to my mind when setting up holiday routines was that I wanted to turn off the heating just before I left and until few hours before I arrived back home.

Save on the heating bill

I already have schedules for my heatings (these are not managed through Home Assistant but rather directly through the Homematic hub), so I just needed a way to temporarily override those. Luckily the heaters can be set to “Manual”, so I can override the temperature to a cold one (e.g. 15 degrees) while I don’t want them to turn on, and set them back to “Auto” before I come back from holidays so that they do whatever they were supposed to do in the first place.

For this simple behavior I had to write the following automation. It’s using the calendar event as trigger and just checking the month of the year (between September and March) to execute the action. I don’t care about spring and summer since the heatings are always off anyways. Your mileage may vary, but you can adapt the automation according to your habits.

- alias: 'Turn off the heating when going on holiday'
  trigger: 
    platform: state
    entity_id: calendar.holiday
    from: 'off'
    to: 'on'
  condition:
    condition: or
    conditions:
      - condition: template
        value_template: '{{ now().month < 3 }}'
      - condition: template
        value_template: '{{ now().month > 9 }}'
  action:
    # Repeat this snippet for each room
    - service: climate.set_temperature
      data:
        entity_id: climate.living_room
        temperature: 15
    - service: climate.set_operation_mode
      data:
        entity_id: climate.living_room
        operation_mode: 'manual'
  [...]

- alias: 'Turn on the heating when coming back from holiday'
  trigger:
    platform: state
    entity_id: calendar.holiday
    from: 'on'
    to: 'off'
  condition:
    condition: or
    conditions:
      - condition: template
        value_template: '{{ now().month < 3 }}'
      - condition: template
        value_template: '{{ now().month > 9 }}'
  action:
    service: scene.turn_on
    entity_id: scene.heating_auto_mode

The scene mentioned above is really simple as you can imagine, and reads like this:


- name: Heating auto mode
  entities:
    climate.living_room: 
      operation_mode: 'auto'
    climate.bathroom: 
      operation_mode: 'auto'
    climate.bedroom: 
      operation_mode: 'auto'
    climate.dining_room: 
      operation_mode: 'auto'
    climate.kitchen: 
      operation_mode: 'auto'

I could probably use a scene for the “Heating off” mode too, although if I remember correctly it’s not possible to set both operation_mode and temperature without calling separate services, and that might explain why I didn’t use a scene.

Lights

The second thing that came to my mind when setting up holiday routines was presence simulation. This means, among other things, turning on and off the lights to make other people (e.g. possible burglars) think that there are people in the flat. Theoretically it might include other things like turning on and off the TV or the music, but since I live in a condo this is not necessary since noise levels cannot be easily associated to a specific flat from the outside.

In order to have a realistic simulation, I decided to turn on the living room light shortly after sunset and until about bedtime, when I’d turn on instead lights in the bedroom for a couple of minutes. This slightly resembles my routine, but again your mileage may vary and you should rather adapt the automation to your habits.

There are 4 automations in total, and they read like this:

- alias: 'Turn on living room light after sunset when on holidays'
  trigger:
    platform: sun
    event: sunset
    # This is a trade-off between too early during summer and too late during winter
    offset: "+01:15:00"
  condition:
    - condition: state
      entity_id: calendar.holiday
      state: 'on'
    - condition: time
       # This is a safe guard for the really long days in summer
      before: '21:45:00' 
  action:
    service: light.turn_on
    entity_id: light.living_room

- alias: 'Turn off living room light at night when on holidays'
  trigger: 
    platform: time
    at: '22:10:00'
  condition:
    condition: state
    entity_id: calendar.holiday
    state: 'on'
  action:
    service: light.turn_off
    entity_id: light.living_room

- alias: 'Turn on bedroom light before bedtime when on holidays'
  trigger:
    platform: time
    at: '21:30:00'
  condition:
    - condition: state
      entity_id: calendar.holiday
      state: 'on'
    - condition: state
      entity_id: sun.sun
      # Again, safe guard for the long days in summer
      state: 'below_horizon'
  action:
    service: light.turn_on
    entity_id: light.bedroom

- alias: Turn on bedlights at 22.05 when on holidays
  trigger:
    platform: time
    at: '22:05:00'
  condition:
    condition: state
    entity_id: calendar.holiday
    state: 'on'
  action:
    service: scene.turn_on
    entity_id: scene.bedroom_before_nighttime

- alias: 'Turn off all lights at 22.50 when on holidays'
  trigger:
    platform: time
    at: '22:50:00'
  condition:
    condition: state
    entity_id: calendar.holiday
    state: 'on'
  action:
    service: homeassistant.turn_off
    entity_id: group.all_lights

The scene “bedroom before nighttime” is the following:

- name: Bedroom before nighttime
  entities:
    light.bed_table_1:
      state: on
      brightness: 50
    light.bed_table_2:
      state: on
      brightness: 50

The lights automations above operate all-year long (unlike the heatings automations), so I had to make some trade-offs to account for the huge differences in sunlight duration between summer and winter.

I hope you enjoyed this post about home automation used for presence simulation and you learned how you can be more relaxed when leaving your flat unattended for longer periods. If you’d like to read about any other specific system or feature, please let me know through the comments and I’ll try to write some more posts!

Have fun tinkering with your smart home automation system!