I’m looking for an automation that will turn on lights between certain times while I’m away on vacation. Heck, might even turn on the sonos and play some music. I have no problems figuring out how to turn on lights between certian times, but was looking for something a little more “fancy”. I’d like to be able to turn on 2 (or maybe selectable # from input_number) random lights for a period of time, maybe after 60 minutes, turn on 2 different random lights.
I used to have something similiar with Smartthings, I’d select the lights I wanted, how many to turn on at a time, and if I wanted to have them change throughout the evening, etc.
Take a look at @anon43302295, his GitHub has a pretty neat implementation, so neat in fact that I ‘borrowed’ it and made it better (suited for my purposes )
Thanks for the pointer to the repo. Lots of good automations, scripts, ideas. I cannot seem to find one deailing with vacation lighting, or just random lighting. Can you point me to the yaml? Appreicate the help, thanks
Looks amazing, and easy to understand, thanks for the great comments. I have only one dumb question. What to you use to trigger the script that starts this process. Is it turning on “Holiday Mode”?
Yeah, I have an input_boolean called holiday mode - so you can switch it on manually and I think there’s an automation that automatically turns holiday mode on if nobody has been home for 48 hours in one of the other packages too.
Quick question, in your comments you mention something wrong with now() and kitchen? Is it the logic itself, or maybe you’ve changed some light names, etc. BTW, love the script, already got it mostly customized for me, but wanted to mostly make sure the now() was not something i had to fix,which I will if needed.
You need to swap out the now()'s in some places (maybe one, maybe all, haven’t looked yet) with templates that compare with sensor.time
I think my reference to kitchen is because I swapped out my kitchen light from a single Zwave unit to a group of hue bulbs, so I need to correct the entity ids in that file.
Yeah, you can use whatever you want to establish when it’s dark. Since I commented out that script I’ve developed an input_boolean that tells the system when it’s dark from various readings around the house, which I then use as triggers and conditions, so I need to swap that in to my version too.
##############################################
# #
# This package contains the scripts I use to #
# simulate the house being occupied when the #
# system is in Extended Away mode #
# #
##############################################
# Automation I use to set the house to extended away
- alias: Mark house as extended away
trigger:
- platform: state
entity_id: binary_sensor.people_home
to: 'off'
for:
hours: 24
action:
- service: input_select.select_option
entity_id: input_select.house_mode
data:
option: 'Extended Away'
input_selects:
house_mode:
name: Mode
options:
- Home
- Away
- Night
- Extended Away
# This is used to set a random bedtime for our phantom house guest
input_datetime:
mockupancy_bedtime:
has_date: false
has_time: true
initial: '23:00'
binary sensor:
- platform: template
sensors:
dark_outside:
value_template: "{{ is_state('sun.sun', 'below_horizon') }}"
friendly_name: "Dark Outside"
# The main mockupancy script...
mockupancy:
sequence:
# Wait until it is after 6pm and dark to start the routine
- wait_template: "{{ states.sensor.time.state >= '18:00' and is_state('binary_sensor.dark_outside' , 'on') }}"
# Check that it Extended Away mode is still on
- condition: state
entity_id: input_select.house_mode
state: 'Extended Away'
# Pick a random bedtime between 10pm and midnight.
- service: input_datetime.set_time
data_template:
entity_id: input_datetime.mockupancy_bedtime
time: "{{ (range(22, 23)|random|int) }}:{{ (range(0, 59)|random|int) }}:00"
# Start the routine, switch on the 'usual' lights
- service: homeassistant.turn_on
entity_id:
- scene.tv_lighting
# Start scripts to randomise occasional lights
- service: script.random_master_bedroom
- service: script.random_kitchen
# Start scripts to randomise kids' bedtime simulation
- service: script.random_guest_room
- service: script.random_ashleys_room
# 1 hour before bedtime, change lighting as if TV was turned off
- wait_template: "{{ states('sensor.time') == (((state_attr('input_datetime.mockupancy_bedtime' , 'timestamp')) - (60 * 60))|timestamp_custom('%H:%M', false)) }}"
- service: homeassistant.turn_on
entity_id:
- scene.casual_lighting
# 20 minutes before bedtime, turn on kitchen lights only
- wait_template: "{{ states('sensor.time') == (((state_attr('input_datetime.mockupancy_bedtime' , 'timestamp')) - (20 * 60))|timestamp_custom('%H:%M', false)) }}"
- service: homeassistant.turn_on
entity_id:
- scene.morning_lighting
# Stop kitchen/master bedroom randomisation
- service: homeassistant.turn_off
entity_id:
- script.random_master_bedroom
- script.random_master_bedroom_loop
- script.random_kitchen
- script.random_kitchen_loop
# At 'bedtime', turn on Master Bedroom for random time between 2 and 15 minutes
# turn off kitchen, wait 20-30 seconds, turn off living room wait 30-59 seconds,
# turn on bedroom.
- wait_template: "{{ states('sensor.time') == (state_attr('input_datetime.mockupancy_bedtime' , 'timestamp')|int|timestamp_custom('%H:%M', False)) }}"
- service: homeassistant.turn_on
entity_id: switch.stairwell_light
- delay: "00:00:{{ (range(15, 59)|random|int) }}"
- service: homeassistant.turn_off
entity_id: group.livingroom
- delay: "00:00:{{ (range(20, 30)|random|int) }}"
- service: homeassistant.turn_off
entity_id: group.kitchen
- delay: "00:00{{ (range(20, 30)|random|int) }}"
- service: homeassistant.turn_on
entity_id: light.bedroom_ceiling_light
data:
brightness: 80
- delay: "00:{{ (range(2, 15)|random|int) }}:00"
- service: homeassistant.turn_off
entity_id: switch.stairwell_light
- delay: "00:{{ (range(15, 59)|random|int) }}:00"
- service: homeassistant.turn_off
entity_id: light.bedroom_ceiling_light
# # After a random time between 15 minutes and an hour, turn off the lights
- delay: "00:{{ (range(15, 30)|random|int) }}:00"
- service: input_boolean.turn_on
entity_id: input_boolean.goodnight_switch
# If light before 7am, switch off lights, stop and loop, if not run a further script first
# - wait_template: "{{ now().hour == 7 }}"
- wait_template: "{{ states.sensor.time.state == '07:00' }}"
- service_template: >
{% if states('binary_sensor.dark_outside' , 'on') %} script.morning_mockupancy
{% else %} script.mockupancy_loop {% endif %}
# A further script for if it is still dark in the morning
morning_mockupancy:
sequence:
# Check that it Extended Away mode is still on
- condition: state
entity_id: input_select.house_mode
state: 'Extended Away'
# Wait a random nmber of minutes between 1 and 30 and switch on the morning lights
- delay: "00:{{ (range(1, 30)|random|int) }}:00"
- service: homeassistant.turn_on
entity_id: scene.morning_lighting
# Wait until it is light, switch off lights and loop
- wait_template: "{{ is_state('binary_sensor.dark_outside' , 'off') }}"
- service: script.mockupancy_loop
# Randomising beditmes for the upstairs rooms
random_guest_room:
sequence:
# Wait until after 8pm, and create a random delay up to 30 minutes and check if it is dark
# - wait_template: "{{ now().hour >= 20 }}"
- wait_template: "{{ states.sensor.time.state >= '20:00' }}"
- delay: "00:{{ (range(1, 30)|random|int) }}:00"
- condition: state
entity_id: binary_sensor.dark_outside
state: 'on'
# Switch light on for between 10 and 30 minutes
- service: homeassistant.turn_on
entity_id: light.guest_bedroom_lamp
- delay: "00:{{ (range(10, 30)|random|int) }}:00"
- service: homeassistant.turn_off
entity_id: light.guest_bedroom_lamp
random_ashleys_room:
sequence:
# Wait until after 9pm, and create a random delay up to 30 minutes and check if it is dark
# - wait_template: "{{ now().hour >= 21 }}"
- wait_template: "{{ states.sensor.time.state >= '21:00' }}"
- delay: "00:{{ (range(1, 30)|random|int) }}:00"
- condition: state
entity_id: binary_sensor.dark_outside
state: 'on'
# Switch light on for between 10 and 30 minutes
- service: homeassistant.turn_on
entity_id: light.ashleys_room_lamp
- delay: "00:{{ (range(10, 30)|random|int) }}:00"
- service: homeassistant.turn_off
entity_id: light.ashleys_room_lamp
# Randomising rooms we're in and out off
random_master_bedroom:
sequence:
# Check that it Extended Away mode is still on
- condition: state
entity_id: input_select.house_mode
state: 'Extended Away'
# Wait a random nmber of minutes between 15 and 90 and switch on
- delay: "00:{{ (range(15, 90)|random|int) }}:00"
- service: homeassistant.turn_on
entity_id: light.bedroom_ceiling_light
data:
brightness: 100
# Wait for between 3 minutes and 9 minutes and switch off
- delay: "00:{{ (range(3, 9)|random|int) }}:00"
- service: homeassistant.turn_off
entity_id: light.bedroom_ceiling_light
# Loop the script for next time.
- service: script.random_master_bedroom_loop
random_kitchen:
sequence:
# Check that it Extended Away mode is still on
- condition: state
entity_id: input_select.house_mode
state: 'Extended Away'
# Wait a random nmber of minutes between 15 and 90 and switch on
- delay: "00:{{ (range(15, 90)|random|int) }}:00"
- service: homeassistant.turn_on
entity_id: light.kitchen_island_light
data:
brightness: 100
# Wait for between 3 minutes and 9 minutes and switch off
- delay: "00:{{ (range(3, 9)|random|int) }}:00"
- service: homeassistant.turn_off
entity_id: light.kitchen_island_light
# Loop the script for next time.
- service: script.random_kitchen_loop
# Scripts that loop the ones above that need looping
mockupancy_loop:
sequence:
# Switch off the lights, and make sure the calling scripts have stopped
- service: homeassistant.turn_off
entity_id:
- group.all_lights
- script.mockupancy
- delay: 00:05:00
# Check that it Extended Away mode is still on and restart the script for a new day
- condition: state
entity_id: input_select.house_mode
state: 'Extended Away'
- service: script.mockupancy
random_master_bedroom_loop:
sequence:
# Stop the calling script
- service: homeassistant.turn_off
entity_id: script.random_master_bedroom
- delay: 00:05:00
# Check that Extended Away mode is on and restart the script
- condition: state
entity_id: input_select.house_mode
state: 'Extended Away'
- service: script.random_master_bedroom
random_kitchen_loop:
sequence:
# Stop the calling script
- service: homeassistant.turn_off
entity_id: script.random_kitchen
- delay: 00:05:00
# Check that Extended Away mode is on and restart the script
- condition: state
entity_id: input_select.house_mode
state: 'Extended Away'
- service: script.random_kitchen
The very first template in the mockupancy script needs now() swapping out, the entity id in your template sensor doesn’t need to be there as it is contained within the value_template already, and the value template needs double quotes at either end.
But other than that on a quick glance you’re looking pretty good
The main problem with this kind of script is how long it takes to test if its working properly, hence how mine has ended up commented out and stuck on the back burner. Hope it works well for you
Awesome thanks for quick review. I updated the changes above. You know I went back and forth on the value template and the Quotes. When I put in in the template test tab, If it had Quotes, the result was “true” or “false” without quotes it returned true or false. Was not sure if the quotes would be an issue.
Yeah, basically whatever you put in the template editor will start with the brackets, then for uniformity any quotes that you need inside those brackets use single quotes, for example
Then when you’re happy with your template and you put it in to your configuration.yaml (or linked one) add double quotes to either end if it is a one liner, so the above one would be
forgive me digging up an old topic, but I guess there’s no sense of making a new one - first of all - @anon43302295, great script! I’m working on a similar one [leaving home next week and thought it would be cool to simulate occupancy ;)] and figured out most of the things, but I have a question about looping.
when you’re running scripts: random_master_bedroom, random_kitchen, random_guest_room, random_ashleys_room from within mockupancy script, do they run parallel to mockupancy in the same time and mockupancy can do other stuff then, or they just wait for each others’ turn and mockupancy is stopped until other scripts are done?
They run in parallel. So the kids lights go on and then off at their (random) set time, the kitchen and bathroom randomly pop on and off throughout and they all wind down towards ‘bedtime’.