Holiday / Vacation mode

Hi folks,

So I’m looking to do up a holiday mode for when myself and herself are on holiday.

I’ve looked at a few posts about it, but it still seems beyond my limited programming ability. I have worked out what I need to have happen, just not how.

In essence,
I’m trying to work out how to get lights to come on at a random time, within a time range. Eg. 2230, +/- 15 minutes.

Have I missed something totally obvious? Or is this something not as straightforward as I hope it is?

Create some scenes, automations, and/or, scripts to accomplish this. Automations are triggered by time and look at a condition if you’re on holiday (think input boolean or device tracker) then the action would call a scene, or script (which then could call scenes with a transition), or flat out just set the light or switch itself.

Generally (do you know how many times I’ve said that ? I should really look for alternatives)
I see someone else is responding but …
If at 22:15 you generate a random number (look it up) of 0 to 30)
You could then delay that time, switch what you need
Generate another random number
Then switch off

Here is complete code I use for exactly the same purpose. You will need to adjust eventually your start (now 30 minutes before sunset), end (randomly after 22:30) and frequency (15 minutes) preference. Also some other setup required:

  • group.family - group of people for whom if not at home automation will trigger
  • group.simulation_lights - group containing all lights that will be used by automation (not all at home)

Basically automation triggers every 15 minutes + random delay (up to 30 minutes) and changes the state of randomly selected light in group to opposite (so either turn on or off).
It is also supplemented by additional automation that at time 22:30+random (30 minutes) switches all lights off.

#############################################################################
## Turn on/off indoor lights randomly
#############################################################################
- id: 'Automatic light toggle'
  alias: Random Away Lights
  initial_state: True
  hide_entity: False

  trigger:
    - platform: time_pattern
      minutes: '/15'

  condition:
    - condition: state
      entity_id: group.family
      state: 'not_home'
    - 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"

    # Selecting entity for toggling
    - service: input_text.set_value
      data_template:
        entity_id: input_text.light_to_switch
        value: "{{ state_attr('group.simulation_lights','entity_id') | random }}"

    # Toggling entity state
    - service: homeassistant.toggle
      data_template:
        entity_id: "{{states('input_text.light_to_switch')}}"

################################################################
## Stopping presence simulation randomly 22:30
################################################################
- id: 'Turning off all toggled lights'
  alias: Turn off all lights
  initial_state: True
  hide_entity: False

  trigger:
    - platform: time
      at: '22:30:00'

  condition:
    - condition: state
      entity_id: group.family
      state: 'not_home'

  action:
    - delay: '00:{{ range(15,59) | random | int }}:00'
    - service: homeassistant.turn_off
      entity_id: group.simulation_lights

Please note additional step of setting the input_text.light_to_switch to random value - you might move this step directly to last step, but I’m using it to record automation states elsewhere, so need to take a note of it…

7 Likes

Have a look at Node-Red with Presence Faker. Can’t be simpeler than that

1 Like

This is looking useful!

So just to clarify. The trigger platform will be time_pattern.
(Can you explain the minutes: ‘/15’ part? I don’t think I’ve used the time_pattern trigger before.)
Your condition section is straight forward, so I’ve no problems there.
The action part.

Because it’s basically going to follow my gf’s habits (I work nights and am away more often than home), as she’s fairly constant in her routine. So I don’t need to trigger a random light, rather I’ll trigger a specific light. So can the action part be slimmed down to;

 action:
  - delay: "00:{{ '{:02}'.format(range(0,30) | random | int) }}:00"
  - data:
      entity_id: light.bedroom
    service: light.turn_on

@Sidioussam '/15 meant just that action will be triggered evry 15 minutes from start., so you can change it to " to have seconds granularity or adjust number of minutes.
Regardin action; it would be rather:

  action:
    service: light.turn_on
    entity_id: light.bedroom

I believe no need for hyppen withing action section if not using template, but explicitly referring to specific entity.
Eventually I’d consider using light_toggle to switch the light on/off, otherwise it will switch on only once and stay like that…

Wait, so where does the random delay go so?

My thought was as follows.

Trigger:
A specific time.

Condition:
Only when Holiday mode is toggled on

Action
Generate a delay, then turn light on

Have I got it wrong?

You are right! The sequence is:

  • trigger (every 15 minutes) starts action
  • first step in action is to introduce random delay, up to 30 minutes
  • after delay is finished turn on thge light.
    I’m not sure how you want to control your ‘vacation mode’, by setting any switch to be flipped manually? I want to have it fully autometed, so I’m using this group.family condition - if there is nobody home automation is always triggered and I do not need to remember to switch it on.
    Watchout! I just realized I should comment on one drawback of this solution. Since hassio can run only one instance of specific script or automation, if next instance is triggered it cancels delay step and finishes remaining step in original one instantly. so it might shorten the delay. In reality I solved this by having 2 copies of this automation defined and having delays set to never overlap. So they start every 15 minutes alternatively (so every 30 minutes each of instances) and have upo to 30 minutes delay. I forgot to change this in the code I pasted, so in realty having this automation to run every 15 minutes with random delay of 30 does not make sense, it will always be truncated at 15 minutes max.

Sorry for the delay in getting back to you. A family weekend got in my way :stuck_out_tongue:
I think I’ve got it now.
My plan is that I toggle a switch in HA and that the automation will check the status of that switch at a certain time. If the switch is on, the automation fires, which will start a sequence. The random delay is so that it doesn’t start at the same time every day, and the sequence will have random delays built into it. Eg. Bathroom light stays on for a given time, plus minus five minutes, or maybe the bedroom light stays on an extra ten or fifteen minutes etc etc. So I don’t need to worry about activating a random light or switch as you have done it. My GF’s habits are fairly predictable in their sequence, the main difference is the duration.

I’ll post up what I ended up with after I’ve cobbled it together :slight_smile:

Hey, I made an app to replay lights on/off from a week ago if all persons are away. Maybe this is something helpful for your use case.

Genius! Thanks ever so much - exactly what I was looking for.

Hi
Just came across this thread for setting up random lights. Can you please shed some light on what “values” to set up in “input_text.set_value” and “input_text.light_to_switch”?

For input_text.light_to_switch, do you list all the lights you want to be part of the process? e.g. light.kitchen, light.study etc?

Thank you for your help

I use input_text.light_to_switch to store the name of light entity that I want to toggle using simulation. So, the purpose of this block:

    # Selecting entity for toggling
    - service: input_text.set_value
      data_template:
        entity_id: input_text.light_to_switch
        value: "{{ state_attr('group.simulation_lights','entity_id') | random }}"

is to set input_text.light_to_switch to random value of entity from group.simulation_lights. I do this that way, since I’m using this specific random entity outside of this automation (for activities logging purpose) and this is the only way I found to preserve the name. So for purpose of this presence simulation automation only this block could be removed and data template for random entity selection could be inserted directly into light toggle section (not tested, I think it should look like):

    # Toggling entity state
    - service: homeassistant.toggle
      data_template:
        entity_id:  "{{ state_attr('group.simulation_lights','entity_id') | random }}"

Hi Mirek
Thanks for your explination. I will give it a go. I have now created 2 groups, one for upstairs and one for downstairs lights. I am trying to set up something so lights on both floors are on.

I will send an update later on
Regards

Nice helpful topic.
Into this right now too.

I’m gonna use it too like i “activate” the modus when i go on vacation…
Thing is, im thinking if its possible to stop the automation automatically after say 11:30 in the evening without turning it off…
I know here, u use 2 automations for it…

edit: maybe if you add this, would that work?

trigger:
  - platform: time_pattern
    minutes: '/15'
    for:
      hours: 2

I’m still working on this, but slowly as I have a lot of other stuff going on, but essentially I’ll have two automations, both of which will always be on. I can do this as I’m working on a pattern and not turning on random lights, it’s the timing that’s random. One automation works for the morning, and one for the evening/night.

Example:
(Random time late evening)Hall light turns on, (Random delay) bedroom light turns on, (Random delay) bathroom light turns on, (Random delay) bathroom light goes off, (Random delay) hall light goes off, (Random delay) Bedroom light goes off.

I have another, separate switch called “Guest Mode.” It’s primary purpose is to stop lights that are on timers switching off. It allows people to be in the house when I or my partner are not there. The vacation mode also uses this as a condition, so that if it’s active, the automations don’t fire, but as there are two automations (morning and evening) the next in the cycle will resume once Guest mode is switched off. It’s probably needlessly complicated, but once finished, I’ll be able to see if I can shrink it down anywhere.

Hope that makes sense.

Hi all! I tried to get @mirekmal code running but couldn’t get it to support multiple concurrent invocations, so tried to come up with a different solution.

(1) First create a light group and add add lights to include in the simulation

# Light group for presence simulation in configuration.yaml 
light:
  - platform: group
    name: simulation_lights
    entities:
      - light.lamp_A # Lamp friendly name A ..
      - light.lamp_B # Lamp friendly name B ..
      - light.lamp_C # Lamp friendly name C  ..

(2) Then create an automation which triggers every X minutes, support parallel invocations, selects a light at random from the previously defined group, and also randomizes the duration (in minutes) the light should be toggled on.

# Automation 
- id: '243214321423423423'
  alias: 'Random Away Lights'
  mode: parallel # Enable parallel executions 
  trigger:
    - platform: time_pattern
      minutes: "/15"
  condition:

#    - condition: sun
#      after: sunset
#      after_offset: '-00:30:00'
#    - condition: time
#      before: '22:00:00'

  action:
    service: script.light_duration
    data:
      # Pass randomly selected light from "group.simulation_lights" as script variable "light"
      light: "{{states.light.simulation_lights.attributes.entity_id | random}}"

      # random value between 5 & 30
      duration: "00:{{ '{:02}'.format(range(5,30) | random | int) }}:00"  

(3) And finally create a script “light_duration” which also support parallel execution

 # Script located in scripts.yaml
  light_duration:
    mode: parallel # Enable parallel executions 
    description: 'Turns on a light for a while, and then turns it off'
    fields:
      light:
        description: 'A specific light'
        example: 'light.bedroom'
      duration:
        description: 'How long the light should be on in minutes'
        example: '25'
    sequence:

# Write entry in logbook (for debugging) 
#      - service: logbook.log
#        data:
#          name: "Notice.."
#          message: >
#            Light {{ light }} will be turned on for {{ duration }}"

      - service: homeassistant.turn_on
        data:
          entity_id: "{{ light }}"
      - delay: "{{ duration }}"
      - service: homeassistant.turn_off
        data:
          entity_id: "{{ light }}"

I haven’t tried the solution much yet, but it seems to work :slight_smile:

Side note - Philips Hue provides similar functionality through “Hue Labs”, perhaps not as flexible, but some may find it convenient to get this behaviour in just a matter of clicks

EDIT: typo

Thanks for sharing this, just what I was looking for!