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

[NOTE 28-Oct-2016] This is now working again, though you need to use below updated code!
Many thanks to @dale3h who helped troubleshooting and coming with the new correct code!

So I wanted the lights to automatically turn off at night but at a random time. That way it’s not very obvious for unwanted guests to think after 2 or 3 nights: “It’s just automated and probably no one is at home, let’s go inside.”

Someone else gave me a suggestion of how to code this, and here is the final result:

Add the time sensor like this:

sensor:
  - platform: time_date
    display_options:
      - time

In automation add the following which at 21:00 will set a random hour between 22:00 and 23:00 and a random minute within that hour between half an hour and 45 minutes:

automation:
  - alias: set_random_time
    trigger:
      - platform: time
        after: '21:00:00'
    action:
      - service: input_slider.select_value
        data_template:
          entity_id: input_slider.random_hour
          # The endpoint is omitted, see: http://jinja.pocoo.org/docs/dev/templates/#range
          value: '{{ (range(22, 24)|random) }}'
      - service: input_slider.select_value
        data_template:
          entity_id: input_slider.random_minute
          value: '{{ (range(30, 46)|random) }}'

Then simply use that in your “lights turn off” automation:

  - alias: turn_lights_off
    trigger:
      - platform: template
        value_template: "{{ is_state('sensor.time', states('input_slider.random_hour')|int ~ ':' ~ states('input_slider.random_minute')|int) }}"
    action:
      - service: light.turn_off
        data: 
          entity_id: light.hue_color_lamp_1

That’s basically it :).
Of course you can fully customize it using your own hours and minutes or being dependant on motion sensors etc.

By the way I meanwhile also added conditions to the turning off lights, like if Kodi is playing, don’t turn off the lights.

25 Likes

Cool! Also see: Occupancy simulation

1 Like

Give this a shot and let me know if it works:

Enable sensor.time:

sensor:
  - platform: time_date
    display_options:
      - time

And the automations look like this:

automation:
  - alias: set_random_time
    trigger:
      - platform: time
        after: '21:00:00'
    action:
      - service: input_slider.select_value
        data_template:
          entity_id: input_slider.random_hour
          # The endpoint is omitted, see: http://jinja.pocoo.org/docs/dev/templates/#range
          value: '{{ (range(22, 24)|random) }}'
      - service: input_slider.select_value
        data_template:
          entity_id: input_slider.random_minute
          value: '{{ (range(30, 46)|random) }}'

  - alias: turn_lights_off
    trigger:
      - platform: template
        value_template: "{{ is_state('sensor.time', states('input_slider.random_hour')|int ~ ':' ~ states('input_slider.random_minute')|int) }}"
    action:
      - service: light.turn_off
        data: 
          entity_id: light.hue_color_lamp_1
4 Likes

I was attempting to do something just like this and found your post. I was able to accomplish the same thing by using a delay command. Here’s what I am using:

  - alias: 'turn_lights_off'
    trigger:
      platform: time
      after: '21:00:00'
    action:
      delay: '{{ (range(0, 2)|random|int) }}:{{ (range(1, 59)|random|int) }}:00'
      service: light.turn_on
      entity_id: light.lamp
11 Likes

Lol i use the same construcion, compact in one automation. with boolean switch.

- alias: 'Scene vakantie - uit'
  trigger:
  - platform: time
    after: '22:00:00'
  condition:
  - condition: state
    entity_id: input_boolean.vakantie
    state: 'on'
  action:
  - delay: '{{ ((range(0, 1) | random) | int) ~ ":" ~ ((range(5, 55) | random) | int)  ~ ":" ~  ((range(5, 55) | random) | int) }}'
  - service: scene.turn_off
    entity_id: scene.woonkamer_vakantie
1 Like

I LOVE this! Clever and EASY to implement!

I wish this worked with OFFSET the way it works with delay. Really clean!

- service: scene.turn_on
      data_template:
        entity_id: "scene.scn_x{{ (range(1, 4)|random) }}"

You can make this look better by selecting a random scene (name them scn_x1, scn_x2, etc.)

I’m guessing you could do the same with a script - sequence - delay too. It would make it look like the lights are going on and off in some order.

2 Likes

Sorry to reopen an old post, but I found this example to be really great:
Presence simulation

Sorry to revive an old post here again. I attempted to use the code for a random light turn off as given by fenster24 (Jan 5) and/or ViperRNMC (Jan 5) above. I entered these into the new automations.yaml file. I both attempted the exact same code as shown above, and also changed it up to match the format of the output that is given when creating an automation in the new Automations editor from the website interface (what I provide below). I keep getting an error that states “Invalid config for [automation]: [delay] is an invalid option for [automation]”. Any idea if I am just inputting this wrong, or if the new automations.yaml integration doesn’t allow a delay?

- action:
    entity_id: light.back_porch_light
    delay: '{{ (range(0, 2)|random|int) }}:{{ (range(1, 59)|random|int) }}:00'
    service: light.turn_off
  alias: 'Turn off Back Porch Random ~9 PM'
  trigger:
    platform: time
    after: '21:00:00'

Hope that my description isn’t too awful. Slowly starting to figure some of this stuff out…
Thanks.

The delay and switching the light off are two separate actions, should be something like…

- action:
  - delay: '{{ (range(0, 2)|random|int) }}:{{ (range(1, 59)|random|int) }}:00'
  - service: light.turn_off
    entity_id: light.back_porch_light
    

Ah! Yes, that passed the test with check_config! Will watch for it tonight to see if it works. Thank you so much mf_social for such a quick and helpful response!

- action:
  - delay: '{{ (range(0, 2)|random|int) }}:{{ (range(1, 59)|random|int) }}:00'
  - service: light.turn_off
    entity_id: light.back_porch_light
  alias: 'Turn off Back Porch Random ~9 PM'
  trigger:
    platform: time
    after: '21:00:00'
1 Like

You’re welcome, hope it works as expected :thumbsup:

It looks like it did. Thanks again.

1 Like

@dale3h

I can’t seem to get your code to work.

The random time input_sliders set there values but the state time test never shows true.

if i show the value of the time to be tested against if the random value is a single digit the format shows as ‘7:9’ instead of ‘07:09’.

Could that be the problem and is there any way to fix it if so?

Otherwise any other ideas?

Yes, that is likely the problem. I guess I must have missed this bug since I was working with double-digits.

To address that, you can use Jinja’s format filter, like so:

{{ '%02d'|format(states('input_slider.random_hour')|int) }}
{{ '%02d'|format(states('input_slider.random_minute')|int) }}
3 Likes

Thanks!

I’ll give that a try.

I was going in that direction but trying to figure out how to write the code by just jumping in somewhere in the middle without knowing the basics is pretty daunting.

What does “scene.turn_off” do? I thought scenes could only be activated.

You are right, I changed it to light.turn_off

If I’m correct, you can only turn on scenes, not?

yes, you could only turn scenes on, for turning off the lights you need to use the turn off light function.

This is what I use:

- alias: Scene vakantie
  trigger:
  - platform: sun
    event: sunset
    offset: '-00:45:00'
  - platform: time
    at: '22:00:00'
  condition:
  - condition: state
    entity_id: input_boolean.vakantie
    state: 'on'
  action:
  - delay: "{% if trigger.platform == 'sun' %}00:00:10{% else %}0{{ (range(0, 1)|random|int) }}:{{ (range(10, 59)|random|int) }}:{{ (range(10, 59)|random|int) }}{% endif %}"
  - service_template: >
    light.turn_{% if trigger.platform == 'sun' %}on{% else %}off{% endif %}
    entity_id: group.zithoek