Set random time for random automatic turning off lights (mimic someone is home)

Moving from Domoticz to HA, I wanted to recreate my ‘switch on lights and tv at randomish hours’ setup, for when I’m on vacation.

Found this topic, but it didn’t work for me - mostly since things have gotten new names since the OP posted the solution.

The following works for me, and I base everything off of a boolean to state if I’m home or not:

Added the following items to configuration.yaml, this defines the necessary input variables to use lateron:

input_number:
  tv_end_random_hour:
    name: TV End Hour during vacation
    min: 0
    max: 23
  tv_end_random_min:
    name: TV End Minute during vacation
    min: 0
    max: 59
  tv_start_random_hour:
    name: TV Start Hour during vacation
    min: 0
    max: 23
  tv_start_random_min:
    name: TV Start Minute during vacation
    min: 0
    max: 59

Added the following code to automations.yaml:

- alias: Set random TV start time on vacation
  trigger:
  - platform: time
    at: '18:30:00'
  condition:
  - condition: state
    entity_id: input_boolean.vacation_mode
    state: 'on'      
  action:
  - service: input_number.set_value
    data_template:
      entity_id: input_number.tv_start_random_hour
      value: '20'
  - service: input_number.set_value
    data_template:
      entity_id: input_number.tv_start_random_min
      value: '{{ (range(30, 40)|random) }}'

- alias: Set random TV end time on vacation
  trigger:
  - platform: time
    at: '18:30:00'
  condition:
  - condition: state
    entity_id: input_boolean.vacation_mode
    state: 'on'      
  action:
  - service: input_number.set_value
    data_template:
      entity_id: input_number.tv_end_random_hour
      value: '22'
  - service: input_number.set_value
    data_template:
      entity_id: input_number.tv_end_random_min
      value: '{{ (range(00, 05)|random) }}'

- alias: TV on during vacation during vacation
  trigger:
    platform: template
    value_template: "{{ is_state('sensor.time', states('input_number.tv_start_random_hour')|int ~ ':' ~ states('input_number.tv_start_random_min')|int) }}"
  condition:
  - condition: state
    entity_id: input_boolean.vacation_mode
    state: 'on'
  action:
  - data:
      entity_id: switch.media_cabinet
    service: switch.turn_on
  - delay: 00:01:00
  - data:
      entity_id: media_player.tv
    service: media_player.turn_on

- alias: TV off during vacation during vacation
  trigger:
    platform: template
    value_template: "{{ is_state('sensor.time', states('input_number.tv_end_random_hour')|int ~ ':' ~ states('input_number.tv_end_random_min')|int) }}"
  condition:
  - condition: state
    entity_id: input_boolean.vacation_mode
    state: 'on'
  action:
  - data:
      entity_id: media_player.tv
    service: media_player.turn_off
  - delay: 00:01:00
  - data:
      entity_id: switch.media_cabinet
    service: switch.turn_off
1 Like

is this really working? I checked my logs and noticed an error that a delay in format HH:MM or HH:MM:SS is expected. If you take eg range(5,55) and pipe in to random (why pipe it to int afterwards?) than the first 5 numbers in the range will give only a single number (5,6,7,8,9) and that should fail the automation right?

This must have been an older thread/quote. All of my new delays make sure that double digits are returned.

-Carlo

Yes, this is what I do but is there a more elegant way to get number between say, 5 and 10 with a leading zero than this (which is a second out)

- delay: "00:0{{ (range(4, 9)|random|int) }}:59"

I don’t think so. I don’t think there is a way to apply a filter to specify the leading zero. I think this is the best bet right now and works pretty reliably.

You can do this (using Python format strings):

{{ "00:%02d:00" % (range(5, 10) | random) }}

Which returns: 00:05:00 to 00:10:00. The magic is %02d which means print an integer with two digits, prepending with 0.

7 Likes

Just some ways to use the yaml capabilities:

  action:
    - delay:
        seconds: "{{ range(0, 120)|random|int }}"
  action:
    - delay:
        minutes: "{{ range(2, 36)|random|int }}"

Or even:

  action:
    - delay: "{{ (range(0, 120) | random) | multiply(60) | timestamp_custom('%H:%M:%S',False) }}"
2 Likes

Hello,
I try to get my lamp to turn off at a random moment between 23:00 and 00:00 hour.
This is wat i put in the automation.yaml

- id: '1586461625357'
  alias: Lamp hal uit
  description: ''
  trigger: []
  condition:
  - after: '23:00:00'
    condition: time
  action:
  - delay: '{{ (range(4, 6)|random|int) }}:00'
  - service: switch.turn_off
    entity_id: switch.sonoff_1000c979c1

It doesn’t work.
What am i doing wrong??

you a) have no trigger and b) do not pass data to your service call.
try this

- id: '1586461625357'
  alias: Lamp hal uit
  trigger:
    platform: time
    at: '23:00:00'
  action:
  - delay: '{{ (range(4, 6)|random|int) }}:00'
  - service: switch.turn_off
    data:
      entity_id: switch.sonoff_1000c979c1

This automation:

- id: '1584825287024'
  alias: Anwesenheitslicht aus (22:15 + 2..36 Minuten)
  description: ''
  trigger:
  - at: '22:15:00'
    platform: time
  condition: []
  action:
  - delay: '{{ "00:%02d:00" % (range(2, 36) | random) }}'
  - scene: scene.anwesenheitslicht_aus

should normally switch off at 22:15 + 2…36 minutes.
But it switches always at 22:15, there is no delay. What am I doing wrong?

it should…
try replacing delay with wait_template - I think it’s possible using sensor.time.

p.s where did you get this constriction? never seen anything like that and it works :wink:

"00:%02d:00" % blah

It was a few post before mine: https://community.home-assistant.io/t/set-random-time-for-random-automatic-turning-off-lights-mimic-someone-is-home/3524/42

1 Like

Okay thanks,
I am gonna try…

Now I tried:

  - delay: '00:{{ range(0,3) | random | int }}{{ range(0,9) | random | int }}:00'

but that is also not working. Very strange because this is the way that is documented in this thread. Strange, strange!

I will look what wait_template is.

I think you need to do some debugging.
I changed your script’s action and it shows me correct data.

action:
- service: persistent_notification.create
  data_template:
    message: '{{ "00:%02d:00" % (range(2, 36) | random) }}'

So it looks like the problem is in your delay (and I cannot reproduce it - everything works as expected)

By the way, according to the docs you can use this

- delay:
    minutes: '{{ range(2, 36) | random }}'
4 Likes

You are my hero! This is working fine, I don‘t know why!
I thought I used that before, but it didn‘t worked, but now it works.

Thank you!

1 Like

Good!
Well, as a homework you can now compare the working code with what does’t work and learn some more about HA if you have time/desire :wink:
Read the docs, it’ll save you from many troubles.

Sure I want to learn more about Home Assistant as it is really great. The time is the problem because two children want to play with their dad and don‘t want to share them with a computer. :slight_smile:

maybe one day…
going for a walk with my little one as well :wink:

I’ve been using this automation/script for a couple of years now and it has been working flawlessly. Love it!

Automation:

alias: "Security: Vacation Lights Evening Simulation"
description: >-
  Turn on script that simulates lights to look like the house is occupied.

  https://community.home-assistant.io/t/set-random-time-for-random-automatic-turning-off-lights-mimic-someone-is-home/3524/7
trigger:
  - platform: sun
    event: sunset
    offset: "-01:15"
  - platform: state
    entity_id:
      - alarm_control_panel.ha_alarm
    to: armed_away
condition:
  - condition: and
    conditions:
      - condition: state
        entity_id: alarm_control_panel.ha_alarm
        state: armed_away
      - condition: sun
        after: sunset
        after_offset: "-01:15"
action:
  - service: script.turn_on
    data: {}
    entity_id: script.vacation_lights_evening
mode: single

Script:

alias: Vacation Lights Evening
sequence:
  - repeat:
      until:
        - condition: or
          conditions:
            - condition: state
              entity_id: alarm_control_panel.ha_alarm
              state: armed_home
            - condition: time
              after: "21:30:00"
              before: "00:00:00"
      sequence:
        - service: scene.turn_on
          data_template:
            entity_id: scene.vacation_simulation_{{ (range(1, 4)|random) }}
        - delay: >-
            {{ ((range(0, 1) | random) | int) ~ ":" ~ ((range(5, 55) | random) |
            int)  ~ ":" ~  ((range(5, 55) | random) | int) }}
  - delay: 00:{{ (range(1, 59)|random|int) }}:00
  - service: light.turn_off
    data: {}
    entity_id: group.all_lights
mode: single
icon: mdi:home-analytics
1 Like