Step Light Brightness with Template in Automation

Tags: #<Tag:0x00007f73938f2858>

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.

What’s so advanced about it? Copy the example to your system and use it. It’s far better than diddling with delays and scripts to create a clumsy version of looping.

If you need help using the suggested python_script, let us know and we’ll walk you through it.

Are you sure your light supports brightness (0-255) or does it perhaps want a brightness_pct (0-100)?

Your example works great after some minor modification. Unfortunately I have found that my light sources need 10 seconds between commands which makes a small step ramp-up of brightness very time consuming, especially if multiple lights are included.

Another question; if I want to do this for say five lights in one automation, do I need to write two scrips per light according to your example or are there better ways not to repeat code over and over? Send the identity ID to the script through template?

Yes, you can pass variables to a python_script. The light fader script accepts parameters.

Were you responding to me or to 123? Depends on what route you chose I guess :slight_smile:
I’m curious if you got it working to your satisfaction.

1 Like