Different brightness through the day in Nursery

Baby doesn’t like much light in the evening, so I wan’t to dim light through the day, I came up with a good idea, but sadly it does not really work, and I hope it is just a missing character somewhere :slight_smile:
Can you take a look at the code and point out what is wrong? The automation is simple, trigger light with a Xiaomi Switch, easy toggle what triggers this automation and change brightness on the fly:

- alias: Gyerekszoba fényerő
  trigger:
    platform: state
    entity_id: light.nappali__rgbw_1
#    entity_id: light.gyerekszoba__rgbw
    from: 'off'
    to: 'on'
  action:
    service: light.turn_on
    entity_id: light.nappali__rgbw_1
#    entity_id: light.gyerekszoba__rgbw
    data_template:
      brightness_pct: >
        {% if   now().hour >6 and now().hour <7 %}10
        {% elif now().hour >7 and now().hour <8 %}30
        {% elif now().hour >8 and now().hour <10 %}50
        {% elif now().hour >10 and now().hour <16 %}100
        {% elif now().hour >16 and now().hour <17 %}50
        {% elif now().hour >17 and now().hour <18 %}40
        {% elif now().hour >18 and now().hour <19 %}30
        {% elif now().hour >19 and now().hour <20 %}20
        {% elif now().hour >20 and now().hour <21 %}10
        {% elif now().hour >21 and now().hour <22 %}5
        {% else %}1
        {% endif %}

Each hour group has different brightness percent and Else should be only 22->06.

This automation will only trigger when the light is turned on. But it seems you want it to run every hour while the light is on. Is that right? If so, then your automation should use a time based trigger (i.e., every hour), and have a condition that the light is on. Then in the action you can set the brightness based on the current time. Is that what you’re looking to do?

You can also use a variable for the time via the now call to cut down on system calls…

 brightness_pct: >-
            {%- set hour = now().hour | int -%}
            {%- if   hour >  5 and hour <  7                   -%}20
            {%- elif hour >  7 and hour < 20                   -%}80
            {%- elif hour > 20 and hour < 22                   -%}60
            {%- elif hour > 22 and hour < 24                   -%}40
            {%- else                                           -%}20  
            {%- endif -%}

Thank you, it works, for others use this working code from jwelters idea:

- alias: Gyerekszoba fényerő
  trigger:
    platform: state
    entity_id: light.gyerekszoba__rgbw
    from: 'off'
    to: 'on'
  action:
    service: light.turn_on
    entity_id: light.gyerekszoba__rgbw
    data_template:
      brightness_pct: >-
        {%- set hour = now().hour | int -%}
        {%- if   hour >=  6 and hour < 7      -%}10
        {%- elif hour >=  7 and hour < 8      -%}30
        {%- elif hour >=  8 and hour < 10     -%}50
        {%- elif hour >= 10 and hour < 16     -%}100
        {%- elif hour >= 16 and hour < 17     -%}50
        {%- elif hour >= 17 and hour < 18     -%}40
        {%- elif hour >= 18 and hour < 19     -%}30
        {%- elif hour >= 19 and hour < 20     -%}20
        {%- elif hour >= 20 and hour < 21     -%}10
        {%- elif hour >= 21 and hour < 22     -%}5
        {%- else                              -%}1  
        {%- endif -%}

Is it possible to put 2 data templates in 1 action? Like merge 2 automations to one without error?

This is the working one, but when I try to merge and put kelvin under the brightness_pct, I get errors like it does not end at the endif block and tries to move to the next row.

- alias: Gyerekszoba színhőmérséklet
  trigger:
    platform: state
    entity_id: light.gyerekszoba__rgbw
    from: 'off'
    to: 'on'
  action:
    service: light.turn_on
    entity_id: light.gyerekszoba__rgbw
    data_template:
      kelvin: >-
        {%- set   hour = now().hour | int   -%}
        {%- if    hour >=  6 and hour < 8   -%}3000
        {%- elif  hour >=  8 and hour < 18  -%}3500
        {%- elif  hour >= 18 and hour < 20  -%}2500
        {%- else                            -%}1700  
        {%- endif                           -%}



- alias: Gyerekszoba fényerő
  trigger:
    platform: state
    entity_id: light.gyerekszoba__rgbw
    from: 'off'
    to: 'on'
  action:
    service: light.turn_on
    entity_id: light.gyerekszoba__rgbw
    data_template:
      brightness_pct: >-
        {%- set   hour = now().hour | int   -%}
        {%- if    hour >=  6 and hour < 7   -%}10
        {%- elif  hour >=  7 and hour < 8   -%}30
        {%- elif  hour >=  8 and hour < 10  -%}50
        {%- elif  hour >= 10 and hour < 16  -%}100
        {%- elif  hour >= 16 and hour < 17  -%}50
        {%- elif  hour >= 17 and hour < 18  -%}40
        {%- elif  hour >= 18 and hour < 19  -%}30
        {%- elif  hour >= 19 and hour < 20  -%}20
        {%- elif  hour >= 20 and hour < 21  -%}10
        {%- elif  hour >= 21 and hour < 22  -%}5
        {%- else                            -%}1  
        {%- endif                           -%}

The error message is this (/n = enters in automations to seperate codes)
Invalid service data for light.turn_on: expected float for dictionary value @ data['brightness_pct']. Got '50 \n\n\n\n\n\n\n\n\n