Holiday / Vacation mode

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!

Quick note for others using mirekmal’s original code. This may be obvious to most but as a newbee, it has taken me a short while to spot! As well as setting up the family and light groups you also need to set up the entity_id: input_text.light_to_switch. I used the helpers page in the UI to add a text helper “light_to_switch” I also had to mess with the quotes a bit in the data_template lines to get the Automation menu in the UI to read the automation file after adding the code but once that syntax was sorted it works great! Thanks again

This works great! Thanks for sharing, both of you.

Hello mirekmal,

I’m not sure I’ve done it right.
Should I replace ‘entity_id’ with something else?
I got this error: Entity ID unknown is an invalid entity ID for dictionary value @ data[‘entity_id’]

Thanks a lot

It should look exactly like in the template. entity_id in this case is what is extracted from the group.
I tried to simulate some errors by changing things in template and configuration, but never got one as yours. What do you get if you put just

{{ state_attr('group.simulation_lights','entity_id') | random }}

into template editor?

Hello mirekmal, I got:

TypeError: object of type ‘NoneType’ has no len()

Thanks