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

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

@Noblewolf
Sorry for my English, is not my language.

How could I read your script? What time in minutes and how often and for how long is a light on, for example?

@B29 this section below controls the time. It will cause the changes to be random. The change could happen ever 5 minutes and 5 seconds to 1 hour 55 minutes and 55 seconds.

Then at the end of the script (in this case 00:00:00, or midnight)
This causes the lights to turn off within 1 hour. Some amount of time between 1 minute to 59 minutes.

1 Like

@Noblewolf thank you.