Light fader with curves: linear, exponential and smooth

First one : Error executing service: <ServiceCall light.turn_on (c:2166f72589bf1afb7cfbe02eb5695c4d): entity_id=['light.computer_room'], params=brightness=7, color_temp=326> I think just like my light doesn’t register brightness below 25, maybe your light does the same.

I see the error t_lag: 326.5 > that should be a whole number, will have to add a round somewhere.

Added round the round.

Updated to V2.0.1.

fade IN, initial lights on: Seems to be working. There was a general warning about performance issue. Maybe you already know this.

Using time.sleep can reduce the performance of Home Assistant
5:31:24 AM – (WARNING) Python Scripts

fade IN, initial lights off: The fade in process stops half way. No other errors except this one.

Curved fader: When light is off, and temperature_start is not defined, the min/max mireds average will be default, please define temperature_start.

fade OUT, initial lights on: Seems to be working.

fade OUT, initial lights off: The fade in process stops half way. Looks like it is the same with fade IN test. No other errors except this one.

Curved fader: When light is off, and temperature_start is not defined, the min/max mireds average will be default, please define temperature_start.

The fader seems to be working when the lights are on initial. When it is off, it fades to half way.

We are getting close! Thank you so much for testing.
Can you set debug_report to 2 and send me the output, it will be a long one.

Can you set debug_report to 2 and send me the output, it will be a long one.

I don’t see any new error logs after setting to (2). Weird thing though. After I set to error (2), it seems to worked. It was able to go from 0 - 255, but it did a quick fade in/out position which looked weird. When I test again, it did not work. Still goes up to half way mark. Still no other errors.

The server log only shows a few errors. The full log can be found ‘home-assistant.log’ in your config folder.

I’ve looked at the home-assistant.log file and I see the same warning that pertains to the script.

home-assistant.log:

2021-12-16 05:24:03 WARNING (SyncWorker_6) [homeassistant.components.python_script.smooth_fader.py] Curved fader: When light is off, and temperature_start is not defined, the min/max mireds average will be default, please define temperature_start.
2021-12-16 05:24:03 WARNING (SyncWorker_6) [homeassistant.components.python_script] Using time.sleep can reduce the performance of Home Assistant
2021-12-16 05:24:44 WARNING (MainThread) [homeassistant.helpers.entity] Update of sensor.eri_to_work_waze is taking over 10 seconds
2021-12-16 05:29:35 WARNING (SyncWorker_10) [homeassistant.components.waze_travel_time.sensor] Error on retrieving data: empty response
2021-12-16 05:29:44 WARNING (MainThread) [homeassistant.helpers.entity] Update of sensor.duc_to_work_waze is taking over 10 seconds
2021-12-16 05:29:44 WARNING (MainThread) [homeassistant.helpers.entity] Update of sensor.eri_to_work_waze is taking over 10 seconds

Hello! I’m really interested on this feature, do you ahve any update on this since then?

Im still working on it.
Because I’m using absolute values opposed to percentages, I have to implement it for every type of lamp differently. I’m testing if the fading is smooth enough when using the percentages. Check back in a few weeks.
Kinds regards,
Ingrid

1 Like

Great script!
Why does the script turn off the light completely when setting the brightness_end to 1? I have to set it to 2 to dim lowest light without turning lamp off.

Feature for future: Would it be possible to add a curve that is more like a sinus or similar, that is slow also at the end and not only in the beginning of the dimming?

Hi Thomas,

The turn off with endpoint 1 is a bug :beetle: thanks for the feedback, I will look into this.
The sinus would be a good addition, maybe the easiest way to address this is to consider it two opposite exp5 formula’s…

New version is coming this weekend.

Kind regards Ingrid

@studioIngrid Just to check, does the above caveat mean that when the light is off, one has to define both brightness_start and brightness_end for the fade to be able to take place?

For instance, if the light were to be off and if I were to call this script while omitting brightness_start but with brightness_end being set to (say) 20, would that do nothing?

PS Just as a very minor thing—it looks like there’s an eensy typo in that caveat (“brighness_start” and “brighness_end” rather than “brightness_start” and “brightness_end”).

Got this working yesterday but with a small caveat.

If I use fade in and the light is off, light will turn on at the latest level it was, then go back down to 0 as I want it. After that fade it works normally.

I tried commenting this part out and editing it like so:

 # GET CURRENT STATE
  states = hass.states.get (entity_id)
  #b_cur = b_initial = states.attributes.get ('brightness') or 0
  b_cur = b_initial = 0
  t_cur = t_initial = states.attributes.get ('color_temp') or 0

But the effect remains.

Great script otherwise!

For me the fade in tests starts with whatever I have set brightness_start to be which is one.

What I am noticing for the fade out tests is that the light physical will turn off completely at the end of the duration, but in HA the light shows still on at a brightness of 1.

update: I may just take it back of what I said about about the fade out issue. I was testing with duration of 1 minute and less. I tried with 3 minute duration and HA shows turn off now.

I created a services.yaml for this script to use the UI selectors.
It would be nice to make color_temp optional as some dimmable lamps don’t have this feature.

#<config>/python_scripts/services.yaml
smooth_fader:
  name: Smooth Fader
  fields:
    entity_id:
      #     entity_id: light.entity ; required
      description: The light that will be turned on.
      example: light.myDimmer
      required: true
      selector:
        entity:
          include_entities:
            - light.myDimmer
    duration:
      #     duration: '00:00:00' ; required, time under 10 seconds is not advised
      description: Duration Time, Time under 10 seconds is not advised.
      example: "00:00:10"
      default: "00:00:10"
      required: true
      selector:
        time:
    brightness_start:
      #     brightness_start: 0-255 ; default: current
      description: Start brightness.
      example: 0
      default: 0
      selector:
        number:
          min: 0
          max: 255
    brightness_end:
      #     brightness_end: 0-255 ; default: current
      description: End brightness.
      example: 255
      default: 255
      selector:
        number:
          min: 0
          max: 255
    brightness_curve:
      #     brightness_curve: 'linear' or 'exp2' or 'exp5' or 'smooth' ; default: exp5
      description: Light brightness curve algorithm
      example: exp5
      default: exp5
      selector:
        select:
          options:
            - linear
            - exp2
            - exp5
            - smooth
    temperature_start:
      #     temperature_start: 154-370 ; default: current
      description: Start temperature
      example: 154
      default: 154
      selector:
        number:
          min: 154
          max: 370
    temperature_end:
      #     temperature_end: 154-370 ; default: current
      description: End temperature
      example: 154
      default: 154
      selector:
        number:
          min: 154
          max: 370
    temperature_curve:
      #     temperature_curve: 'linear' or 'exp2' or 'exp5' or 'smooth' ; default: exp2
      description: Light temperature curve algorithm
      example: exp2
      default: exp2
      selector:
        select:
          options:
            - linear
            - exp2
            - exp5
            - smooth
1 Like

Hi MiniCooper246, could you explain to me how i can use your script? I am interested, Thank you!

nice script, thank you! Would it be a lot of work to add color to it? Say for a sunrise simulation?

The Python Script itself is not made by me, only the services.yaml.
A service.yaml is a declaration File to use the script more easily from the HomeAssistant Web UI.
(service.yaml Docs)

But you have to edit it slightly in this area. it requires a list of light entities you want to be able to control.

selector:
        entity:
          include_entities:
            - light.myDimmer

Color is supported by default (i think) but i could not test it, because my lights don’t have a color feature. So I had to edit the original Script slightly to not bug, because it can’t access the color temp of my lights.

1 Like

Hi Community !
First of all, thank you very much for this script. Im trying to find some complex solution veeeeeeery long time ago, and this is so far the most stable and “smooth” option.
Can you please support me with 2details in this script ?

  • I would like to achieve dimming/fading UP or DOWN for my lights from "current state-so current brightness, temperature, colour)
    However, if i try to run the script WITH defined END BRIGHTNESS, without temperature values, the script throws an error that temperature should be defined. How can i please avoid this, and control only the BRIGHTNESS part (of course with duration and curve) ? With mono lights/strips is not such a big deal (however im using different temperatures during the day) but with RBG is that even worse.

  • Sometimes would be great to stop the script from further execution, if light is turned off or brightness changed manually during script running. Is is please possible somehow ?

I can test nearly immediate any change, if needed.

Thank you very much !

1 Like

Hi all, I am currently updating the script. Working on these changes:

  • add rgb color support
  • breakout for animations duration > 10 sec
  • breakout service to stop all animations
  • wrap in custom integration to call async

Will post when update is ready.

— Ingrid

4 Likes