Simulating presence with Home Assistant
How to make an automation to randomly turn on lights while you're away.
When going away...Triggers when a boolean is switched. This is a how to guide for implementing similar in your setup.
I want to thank Mirek Malinowski (@mirekmal) on the Home Assistant forum for sharing their code in this post. I've taken their work to get me started, and largely their code is unchanged.
I'm writing this guide in August 2022, using Home Assistant version 2022.8.
Create the "holiday mode" toggle
I've configured my automation to only trigger if holiday mode is enabled. Equally I could have set it to trigger if all members of the house were away for an extended period, but that's sufficiently rare it made more sense to enable it manually. To achieve this I created an toggle helper called input_boolean.holidaymode
:
- Go to Settings then Devices & Services
- Click on the Helpers tab (in the web interace this will be at the top, on the Android app it's an icon at the bottom
- Click create helper
- Choose Toggle
- Type a name (e.g.,
HolidayMode
) and choose an icon (e.g.,mdi:beach
) and click Create - The list of helpers will now include
input_boolean.holidaymode
Create "light to switch" input text
As the automation stores the light it is going to change (switch on or off) in the "light to switch" variable, we need to create that.
- Go to Settings then Devices & Services
- Click on the Helpers tab (in the web interace this will be at the top, on the Android app it's an icon at the bottom
- Click create helper
- Choose Text
- For name, type
light_to_switch
and leave the other options as their defaults - Click Create
- The list of helpers will now include
input_text.light_to_switch
Create the light group
We need to create a group of lights that the automation will control. This gives us flexibility, as it may not be worth turning on every light (for example, a basement that has no windows wouldn't show any light externally so there's no point turning that light on). Once we've created the light group we'll be referencing it by name in the automation.
- Go to Settings then Devices & Services
- Click on the Helpers tab (in the web interace this will be at the top, on the Android app it's an icon at the bottom
- Click create helper
- Choose Group (a circle with three dots in it)
- When asked what type of group this is, choose Light group
- Give your group a name (e.g., "Presence simulation lights") and choose the members to include in the group
- Click Submit
- In the helpers list you'll see a group called
light.presence_simulation_lights
- copy this name exactly as we'll need it in the automation
Using relays or smart switches rather than smart bulbs / lights? See below!
Switches aren't lights
I'm using Shelly relays to make my normal light switches smart. This means that the physical light switch still works, important for people visiting or for when you're not holding a smart phone. Unfortuately this also means that you cannot add the light switches to a lighting group - they're switches not lights!
Fortunately, Home Assistant gives us a solution to this - the switch as X helper. The original switch is not affected, so any existing automations won't be negatively impacted.
- Go to Settings then Devices & Services
- Click on the Helpers tab (in the web interace this will be at the top, on the Android app it's an icon at the bottom
- Click create helper
- Choose Switch as X
- In the switch drop down box, find and click on the Shelly that you want to make into a light (e.g.,
switch.kitchen
) - Under New Type select Light
- Click Submit
- The list of helpers now includes
light.kitchen
with a type of Switch as X - Repeat for each of the switches you want to treat as a light
What's "Hide members"?
When creating the group there's an option called hide members at the bottom. Enabling this means that member entities (the individual lights) won't be shown in Home Assistant. We don't want this option, but it would be useful if you had six smart lightbulbs in a single light fitting, and wanted to treat them as a single light.
Create automation - random lights on
Once again, thanks to Mirek Malinowski (@mirekmal) on the Home Assistant forum for sharing their code in this post.
- Click Settings then Automations & scenes
- From the bottom right click the blue + Create automation button
- From the How do you want to create your new automation? pop up choose Start with an empty automation
- Once the graphical builder has loaded, at the top right click the three dots menu followed by Edit in YAML
- Copy and edit the code below to meet your requirements
- Once you're done editing, click the blue Save button in the bottom right - if all went well the save button will disappear and you won't see any errors (in red) at the top
alias: "Holiday mode: Presence simulation"
trigger:
- platform: time_pattern
minutes: /30
condition:
- condition: state
entity_id: input_boolean.holidaymode
state: "on"
- condition: sun
after: sunset
after_offset: "-00:30:00"
- condition: time
before: "22:00:00"
action:
- delay: 00:{{ '{:02}'.format(range(0,30) | random | int) }}:00
- service: input_text.set_value
data_template:
entity_id: input_text.light_to_switch
value: "{{ state_attr('light.presence_simulation_lights','entity_id') | random }}"
- service: homeassistant.toggle
data_template:
entity_id: "{{states('input_text.light_to_switch')}}"
initial_state: true
hide_entity: false
What does the automation do?
The automation's YAML above is fairly readable, even for someone who's not a developer. You can see the automation is split into sections that define the configuation.
alias
gives our automation a friendly name- The
trigger
section is where we define what makes this automation start. As we want to run this automation every day at certain times this is set to use thetime_pattern
platform withminutes: /30
which means every thirty minutes - Obviously we don't want the automation turning lights on all day, so the
condition
section allows us to be more granular
Astate
condition forinput_boolean.holiday_mode
means one of us must have told Home Assistant to be in holiday mode
There's no point in turning lights on during the day, so thesun
state condition ensures the automation only makes changes after sunset
Finally, atime
condition means the automation will only make changes before 22:00 - In the
action
section we declare what the automation will do. In this case, it will wait a random number of minutes (up to 30) before choosing a random light from our group. That light name will be stored in theinput_text.light_to_switch
variable and that light's state will then be toggled (turned on if it was off, and vice versa)
Create automation - lights off at random bedtime
Add a second automation, again editing it as YAML. This one triggers at a set time (23:00), waits a random amount of time between 15 and 59 minutes, and then turns off all the lights in the light.presence_simulation_lights
group.
alias: "Holiday mode: Turning off all toggled lights"
description: ""
trigger:
- platform: time
at: "23:00:00"
condition:
- condition: state
entity_id: input_boolean.holidaymode
state: "on"
action:
- delay: 00:{{ range(15,59) | random | int }}:00
- service: homeassistant.turn_off
data: {}
target:
entity_id: light.presence_simulation_lights
initial_state: true
hide_entity: false
mode: single
An icon on your dashboard
If you've followed this tutorial you'll want a button to easily enable holiday mode - if you don't then your automations won't enable any lights when you go away! This is as easy as editing a dashboard, adding a button card and setting the entity to input_boolean.holidaymode
. Then you'll have a button like the one at the top of this blog post to easily tell Home Assistant when you'd like to simulate presence.
What else could be added?
So far this automation only simulates presence using lights, but generally people don't sit at home in silence! If you have smart speakers (or a radio connected to a smart plug), you could create a similar automation to start playing music, the radio, or a podcast. Similarly, if you have a smart TV your could turn that on for a period too.
I'd like to add smart curtain openers / closers but those are quite pricey.
Alternative approaches
This automation isn't perfect, at there's not necessarily a logical sequencing of lights. For example, if I were moving around my house I might move from the living room to the hallway to the stairs to the bedroom. To do that I might turn on all the lights in those areas as I go. This automation doesn't provide a flow through the house, just random lights turn on at random times.
There are automations in Node-RED that review activity over the last few weeks and then play that back. While this may give a more natural flow through the house, I opted not to use it as a) I was in a rush before going away and b) because I'm not using Node-RED at the moment.
Conclusion
Obviously this isn't a replacement for securing your house properly when you're away, and you should invest in appropriate locks and alarm systems! This automation set is just an additional defence, and I take no responsibility if your house still gets broken into (or for the electricity used while you're away!).
Banner image: my Home Assistant dashboard's "holiday mode" button.