Script: dynamic light brightness and delay according to the sytem time

Hello all,

I am trying to make the light brightness and delay dynamic according to the system time. So I want my living room lights to perform the following action:

At or after 08:00: full brightness with a delay of 30 minutes
At or after 23:00: brightness set to 20% with a delay of 5 minutes

It works partly, at 00:00 it will set the brightness to 20% with a delay of 5 minutes. Whatever value I use in the script (23, 22, 21 etc.), it always performs its action at 00:00. So I think the script performs its ELSE statement. What is wrong with my code?

I found this article and I tried to implement it in my code:

See my code below:

Automation:

script:

Could you please use preformatted text when posting your config. Its really hard to read otherwise…and no way of telling if your indentation is right.

~Cheers

@PhyberApex: I created two pictures, because I did not know how to use preformatted text :slight_smile:

This might be an alternative solution for you. Define a sensor like this

  light_level:
    friendly_name: 'Light level'
    value_template: >-
        {%- if now().hour >= 20 and now().hour < 23 %}
            Evening
        {%- if now().hour >= 23 and now().hour < 24 %}
            Nightfall
        {%  elif now().hour >= 0 and now().hour < 6 %}
            Night
        {%  elif now().hour >= 6 and now().hour < 8 %}
            Cockcrow
        {% else %}
            Day
        {%- endif %}

Than for the rules

  action:
    service: light.turn_on
    entity_id: light.fibaro_system_fgd211_universal_dimmer_500w_level_6_0
    data_template:
      brightness: >
        {%- if is_state("sensor.day_night", "Evening") %}
        255
        {%- if is_state("sensor.day_night", "Nightfall") %}
        120
        {%- elif is_state("sensor.day_night", "Night") %}
        60
        {%- else %}
        255
        {% endif %}
1 Like

Take at look at mine

https://github.com/CCOSTAN/Home-AssistantConfig/blob/master/automation/System/detect_and_adjust_lights.yaml

2 Likes

Thanks a lot guys for pointing me in the right direction! I will try those solutions and inform you about the results.

I liked the idea of creating a sensor, as described by @moskovskiy82. This solution works great! Thanks a lot for the help.
@CCOSTAN, thank you for sharing your solution as well.

This is a very useful piece of info @moskovskiy82 thanks! As a noob still coming to terms with templating, it is very helpful.

if I’d like my ‘nightfall’ to run, for example, between 11pm and 2am (i’m a night owl), how would i best have it cross the 24/0 threshold? would i require two arguments? one for 11 until 0 and one from 0 until 2? would they need to have different names?

Also, if i wanted to change colour based on time, and have low red lights in ‘night’, would that simply require a second data template for rgb colour? is it as simple as dropping a second data template in the action, or can you not have more than one data template per action?

tia