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
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?
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.
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
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.
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