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