Step Light Brightness with Template in Automation

Hi Experts!

I am trying to step the brightness of a light source through an automation by using data_template. I cannot use transition: since this particular light source does not support it.

I do not get any change in brightness when running the below automation. What could I do wrong?

- id: '1570867369747'
  alias: Time
  trigger:
  - at: '23:15'
    platform: time
  condition:
  - condition: state
    entity_id: input_boolean.automation_sunrise
    state: 'on'
  action:
    - service: light.turn_on
      data_template:
        entity_id: light.hall_tak
        brightness: >
          {% if states.light.hall_tak.attributes.brightness < 50 %}
          {% set steps = (250 - states.light.hall_tak.attributes.brightness) // 20 %}
          {% for i in range(1, steps+1) %}
          {{ states.light.hall_tak.attributes.brightness + i*20 }}
          {% endfor %}
          {% endif %}

I get configuration valid! when running check config under server control.
It looks like this when running the code through template under development tools.

brightness: >
46
66
86
106
126
146
166
186
206
226
246

There is no else. So when states.light.hall_tak.attributes.brightness is below 50, no valid brightness is produced. Could this be your issue? Also, is the condition met?

Edit: silly me, it is much more obvious. Your template code produces a list of brightness values. But this does not mean the service call gets repeated. Your code should loop the action light.turn_on. I am unaware of a method to achieve that in yaml. It can be done in Python code / App Daemon I believe.

Thanks, that makes perfect sense. A list of brightness values does not repeat the service call itself.

But there must be some way to solve it within yaml? Can I call another automation from within the automation to make it happen?

I have no idea how to get python or AppDaemon involved.

I just searched the forum, AppDaemon seems the obvious route. But perhaps this link will inspire you, using a combination of script and automation. I like the idea :slight_smile:

1 Like

It was not obvious to me that your link solved my problem but maybe I have to read it a couple of times.

For inspiration; create an automation that repeatedly calls a script, each time passing higher brightness. Then stop the automation when the desired value is reached. It might be triggered by an input boolean, that you disable when the last brightness is reached.

I don’t have it in code, but perhaps this is something you can run with?

1 Like

I will give it a try but I am at the limit of my coding skills here.

I can help you tomorrow hopefully.

Very much appreciated, it is fun, but takes so much time to get it right in yaml.

you gotta have a service with a delay between each brightness level for this to work. Your best option is a python script or hardcode each brightness level as a ramp up in a single script. With your current automation, your light will just turn on to 246 without any progression.

1 Like

Here is someone who did this with a script: Light Fader by Transition Time

3 Likes

Thanks for the link but I think python is too advanced for me at this point. I need to better understand yaml before taking the python step.

Without knowing the details this should be possible to do in yaml, combining automation and script as you propsed above?

Is it too much to ask that you write an automation and script example for me? That I would be able to understand and modify myself. For example I dont get how to pass information between the automation and script. As pedro points out there needs to be a delay as well.

Fully understand if you dont have time for it.

Agree, delay is needed. Hardcoded wont work since I want to start at current brightness that will differ over time. Was hoping to find a solution without python.

I like a good challenge. Plus I also have an automation that could use a step by step brightness increase. Currently it’s just a giant script, every step written out without looping.

Can you explain in words what you are trying to achieve? If I’m interpreting correctly, around sunrise you want the brightness to increase to 250, in steps of 20, starting from the current brightness. But only if the current brightness is less than 50. Is that your intention? The trigger time of 23:15 does not seem to fit the profile :slight_smile:

It is prototype code so I understand it does not make sense :slight_smile:

What I want could be desceibed as; one hour before sundown I want a light to stepwise increase its brightness starting at the current brightness and stop at maximum brightness. The increase should be slow, it could take in total 60 seconds so that a person in the room hardly notices that it is increased.

It is kind if the same functionality as transition: provides but my light source does not support it.

It would be great if the transition time could be changed in the code to select something else than 60 seconds. It would also be great if the end brightness could be chosen in the code to be something else than maximum brightness.

Does it make sense?

Here is looping 2 scripts. It needs more work, but as a starting point I hope it works for you. Let me know what you think :slight_smile:

Scripts.yaml:

brightness_step_odd:
  alias: 'Increase brightness odd step'
  sequence:
    - service: light.turn_on
      data_template:
        entity_id: light.keukentafel
        brightness: >
          {% if brightness_step <= required_steps %}
            {{ state_attr('light.keukentafel', 'brightness') + 20 }}
          {% else %}
            {{ state_attr('light.keukentafel', 'brightness') }}
          {% endif %}
    - delay:
        seconds: 3
    - service_template: >
        {% if brightness_step|int <= required_steps|int %}
          script.brightness_step_even
        {% else %}
          script.dummy
        {% endif %}
      data_template:
        brightness_step: "{{brightness_step|int + 1}}"
        required_steps: "{{ required_steps }}"

brightness_step_even:
  alias: 'Increase brightness even step'
  sequence:
    - service: light.turn_on
      entity_id: light.keukentafel
      data_template:
        brightness: >
          {% if brightness_step|int <= required_steps|int %}
            {{ state_attr('light.keukentafel', 'brightness') + 20 }}
          {% else %}
            {{ state_attr('light.keukentafel', 'brightness') }}
          {% endif %}
    - delay:
        seconds: 3
    - service_template: >
        {% if brightness_step <= required_steps %}
          script.brightness_step_odd
        {% else %}
          script.dummy
        {% endif %}
      data_template:
        brightness_step: "{{brightness_step|int + 1}}"
        required_steps: "{{ required_steps }}"

dummy:
  alias: 'Dummy script'
  sequence:
    - delay:
        milliseconds: 1

Automations.yaml:

- alias: 'Brightness increase by steps'
  trigger:
    - platform: time
      at: '14:36'
  action:
    service: script.brightness_step_odd
    data_template:
      required_steps: "{{ ((255 - state_attr('light.keukentafel', 'brightness')) / 20) | round(0, 'floor') }}"
      brightness_step: 1
1 Like

Wow, cool, thanks. Will give a shot when the kids are sleeping!

Curious question, instead of having odd and even script can ONE script call itself until done or wont that work?

For my understanding why is the dummy script needed, cant the else clause just be empty?

A script can’t be started when it is running, so a script cannot call itself.

For the dummy script: you can try it out without if you like :wink: but a service template can’t be empty is what I was taught.

1 Like

Okay, those two things would have taken me a long time to figure out.

I tried the automation and scripts but get strange behavior, first brightness step is 15 and second is 5. After that the brightness does not change. I need to look into the details further.

For troubleshooting purposes is it possible to print values somewhere while the scripts is running so that I can see what is happening?

Is it possible to see if a script is running, to make sure it is not stuck in an endless loop or something?

I will spend more time on this tomorrow.

The States page is for monitoring , you can see the script status. No printing these values I’m afraid.

You can post your code if you like, we’ll be happy to take a look.