Your issue is because you have quotes inside quotes. You cannot encapsolate single quotes inside single quotes. You need to use double quotes for the entire template and single quotes inside the template. Also, you are missing the colon after the word delay. Remember, yaml is space and punctuation driven, without that, yaml doesn’t know how to parse properly.
alias: TEST
trigger:
platform: state
entity_id: input_boolean.trigger
to: 'on'
action:
- delay: "00:{{ '{:02}'.format(range(1,10) | random | int) }}:00"
- service: homeassistant.toggle
data_template:
entity_id: '{{ states.group.away_lights.attributes.entity_id | random }}'
So I made some changes after some days. It’s pretty annoying that lights is turn off or on when i come home because the random delay. So i did a script with the conditions that it’s only execute when the alarmpanel is disarmed after that a random lamp is turned off or on.
But one question. Is it possible to send brightness level in the action section in the scripts. when the lamp is turned of and then turned on is comes back on 100 %.
alias: Belysning - inte hemma
trigger:
platform: time
minutes: '/45'
seconds: 00
condition:
condition: and
conditions:
- condition: state
entity_id: alarm_control_panel.ha_alarm
state: 'armed_away'
- condition: sun
after: sunset
action:
- delay: "00:{{ '{:02}'.format(range(1,30) | random | int) }}:00"
- service: script.turn_on
entity_id: script.away
The second one is to ensure the light is turned on after the delay only if the alarm is still armed. During that delay if you disarm the light would still come on without that 2nd one.
Thanks a heap. Didn’t realise (after 2+ yrs of using HA), that you can have a condition after an action! Awesome. P.S If you can edit your code above, the time statement is no longer valid. It now needs to be:
I thought I’s share what I have been using for a few months. I called it “Occupancy Simulator”.
occupancy_simulator.yaml (package)
input_boolean:
enable_occupancy_simulator:
name: Enable Occupancy Simulator
automation:
- alias: Occupancy Simulator-Doorbell
initial_state: 'on'
trigger:
- platform: event
event_type: xiaomi_aqara.click
event_data:
entity_id: binary_sensor.switch_158d00018b3ede
click_type: single
condition:
- condition: state
entity_id: input_boolean.enable_occupancy_simulator
state: 'on'
- condition: state
entity_id: sun.sun
state: 'below_horizon'
- condition: template
value_template: "{{ is_state('device_tracker.davess7lan','not_home') and is_state('device_tracker.rowess7lan','not_home') }}"
action:
- delay: '00:00:{{ range(10,40) | random | int }}'
- service: homeassistant.turn_on
data:
entity_id: light.bedroom_lamp
brightness: 200
- delay: '00:0{{ range(9) | random | int }}:{{ range(10,58) | random | int }}'
- service: homeassistant.turn_off
data:
entity_id: light.bedroom_lamp
- alias: Occupancy Simulator-Random Timed Lights (Sunset)
initial_state: 'on'
trigger:
- platform: sun
event: sunset
offset: '00:15:00'
condition:
- condition: state
entity_id: input_boolean.enable_occupancy_simulator
state: 'on'
- condition: template
value_template: "{{ is_state('device_tracker.davess7lan','not_home') and is_state('device_tracker.rowess7lan','not_home') }}"
action:
- delay: '00:{{ range(10,40) | random | int }}:00'
- service: homeassistant.turn_on
data:
entity_id: light.kitchen_light_level
brightness: 255
- delay: '00:0{{ range(9) | random | int }}:{{ range(10,58) | random | int }}'
- service: homeassistant.turn_on
data:
entity_id: light.bedroom_lamp
brightness: 255
- delay: '00:0{{ range(9) | random | int }}:{{ range(10,58) | random | int }}'
- service: homeassistant.turn_off
data:
entity_id: light.kitchen_light_level
- delay: '00:0{{ range(9) | random | int }}:{{ range(10,58) | random | int }}'
- service: homeassistant.turn_off
data:
entity_id: light.bedroom_lamp
- delay: '00:0{{ range(9) | random | int }}:{{ range(10,58) | random | int }}'
- service: homeassistant.turn_on
data:
entity_id: light.kitchen_light_level
brightness: 255
- delay: '00:0{{ range(9) | random | int }}:{{ range(10,58) | random | int }}'
- service: homeassistant.turn_off
data:
entity_id: light.kitchen_light_level
- alias: Occupancy Simulator-Random Timed Lights (Midnight)
initial_state: 'on'
trigger:
- platform: time
at: '00:00:00'
condition:
- condition: state
entity_id: input_boolean.enable_occupancy_simulator
state: 'on'
- condition: template
value_template: "{{ is_state('device_tracker.davess7lan','not_home') and is_state('device_tracker.rowess7lan','not_home') }}"
action:
- delay: '0{{ range(2) | random | int }}:{{ range(10,55) | random | int }}:00'
- service: homeassistant.turn_on
data:
entity_id: light.bedroom_lamp
brightness: 255
- delay: '00:0{{ range(9) | random | int }}:{{ range(10,58) | random | int }}'
- service: homeassistant.turn_off
data:
entity_id: light.bedroom_lamp
- delay: '00:0{{ range(9) | random | int }}:{{ range(10,58) | random | int }}'
- service: homeassistant.turn_on
data:
entity_id: light.bedroom_lamp
brightness: 255
- delay: '00:0{{ range(9) | random | int }}:{{ range(10,58) | random | int }}'
- service: homeassistant.turn_off
data:
entity_id: light.bedroom_lamp
I have am input_boolean in the GUI to turn it on/off and it only switches lights if my girlfriend and I are both not_home. If someone presses the doorbell, it starts a light switching sequence, likewise at a random time after sunset and again after midnight.
I’m using a Xiaomi wireless button (round type) for the doorbell button and the Xiaomi Gateway to produce the chime sound. As well as making the chime sound, automations pause any playing media and send notifications to phone’s with a camera snapshot