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

    value_template: "{{ is_state('sensor.time', states('input_slider.random_hour')|int ~ ':' ~ states('input_slider.random_minute')|int) }}"

Hi,
is there a doc about the meaning of “~” in this config ?
I don’t really get what it means.

2nd question, just to clear something that could be obvious to everyone but me,
I guess that we can’t set the range/random inside the “at:” field ?

- alias: 'Turn off between midnight and midnight thirty'
  trigger:
    - platform: time
      at: '00:{{ range(0,30) | random | int }}:00'
  action:
    - service: light.turn_off
      data:
        entity_id: light.light_bulb

Hey @ViperRNMC,

I did use your example, but I cannot seem to get it to work. I keep getting an error, that Delay should not be in the Automation part. Is your example a script, or is it an automation??

I only use automation

alias: Turn on Living room Lights when sun gets dimmer
trigger:
  platform: numeric_state
  entity_id: sun.sun
  value_template: '{{ state.attributes.elevation }}'
  below: 0.1
action:
  service: scene.turn_on
  delay: '{{ ((range(0, 1) | random) | int) ~ ":" ~ ((range(5, 55) | random) | int)  ~ ":" ~  ((range(5, 55) | random) | int) }}'
  entity_id: 'scene.scn_liv_on1'

Gives me the following error:

Invalid config for [automation]: [delay] is an invalid option for [automation]. Check: automation->action->0->delay. (See ?, line ?). Please check the docs at https://home-assistant.io/components/automation/

So I am a little confused… (Yes, I do use the automation: !include_dir_list includes/automation/ option)

Can you put your light on it, @ViperRNMC?

Put the delay at the bottom or at the top, because the “service: scene.turn_on” should come right before the "entity_id: ‘scene.cn_liv_on1’

  action:
  - delay: '{{ ((range(0, 1) | random) | int) ~ ":" ~ ((range(5, 55) | random) | int)  ~ ":" ~  ((range(5, 55) | random) | int) }}'
  - service: light.turn_off
    entity_id: light.front_porch_landscape
1 Like

Just did that, and getting the same error.

Can I see what you have right now?

Delay and service should have a "- " in front of them, because they are list items of “action:”

@BendedArrow, I already fixed it :wink: I Misstyped yaml.

alias: Turn on Living room Lights when sun gets dimmer
trigger:
  - platform: numeric_state
    entity_id: sun.sun
    value_template: '{{ state.attributes.elevation }}'
    below: 0.1
action:
  - delay: '{{ ((range(0, 1) | random) | int) ~ ":" ~ ((range(5, 55) | random) | int)  ~ ":" ~  ((range(5, 55) | random) | int) }}'
  - service: scene.turn_on
    data_template: >-
      entity_id: 'scene.scn_liv_on{{ ( range(1, 4)|random) }}'
    # entity_id: 'scene.scn_liv_on1'
1 Like

This is what i recent used for an vacation automation

# vakantie
- alias: Scene vakantie
  trigger:
  - platform: state
    entity_id: binary_sensor.woonkamer_motion_dark
    to: 'on'
  - platform: time
    at: '22:00:00'
  condition:
  - condition: state
    entity_id: input_boolean.vakantie
    state: 'on'
  - condition: or
    conditions:
    - condition: and
      conditions:
      - condition: state
        entity_id: binary_sensor.woonkamer_motion_dark
        state: 'on'
      - condition: time
        before: '22:00:00'
    - condition: template
      value_template: >
        {{ trigger.platform == 'time' }}
  action:
  - delay: "{% if trigger.platform == 'state' %}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_{{ trigger.platform|replace('state', 'on')|replace('time', 'off') }}
    entity_id: group.zithoek
  - service: notify.user
    data_template:
      title: 'Home Assistant '
      message: >
        lichten {% if trigger.to_state.state == 'on' %}aan{% else %}uit{% endif %}

Whow! That looks nice too, @ViperRNMC!!!

Right now, I am automating my livingroom lights, so I don’t have to worry about too much automation for the holidays :slight_smile:

No thanks :grinning:

ROFLMAO, @ViperRNMC, and that is the beauty of systems like this. We can both use systems, that we think fit, and work with that. The beauty of open, isn’t it? :smiley: But I sure will use some of your code!

Hi!

Been trying to format your code, would you please check for me if this is it? especially the nested conditions are a bit of a challenge… :wink:

- alias: Scene vakantie
  trigger:
    - platform: state
      entity_id: binary_sensor.woonkamer_motion_dark
      to: 'on'
    -  platform: time
       at: '22:00:00'
  condition:
    - condition: state
      entity_id: input_boolean.vakantie
      state: 'on'
    - condition: or
      conditions:
        condition: and
        conditions:
          - condition: state
            entity_id: binary_sensor.woonkamer_motion_dark
            state: 'on'
          - condition: time
            before: '22:00:00'
          - condition: template
            value_template: >
              {{ trigger.platform == 'time' }}
  action:
    - delay: >
        {% if trigger.platform == 'state' %}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_{{ trigger.platform|replace('state', 'on')|replace('time', 'off') }}
        entity_id: group.zithoek
    -  service: notify.user
       data_template:
       title: 'Home Assistant'
       message: >
         lichten {% if trigger.to_state.state == 'on' %}aan{% else %}uit{% endif %}

if you post herre, please use the code </> button in the editor, to

changed my code, thx

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.