Light Fader by Transition Time

could you change this script to use it for volume fading on a media player? It is a feature i have been looking for for a long time. Tried some things but did not get it working. I was thinking about swapping all the light services and attributes for music_player services and attributes.

I expect that you could, and the concept would be the same, but the values that you set in each iteration would be specific to whatever scale fits the device you are controlling (i.e. 1 - 255 works for lights, but it will be something else for your TV or receiver.)

If I wanted to create a sensor that would state when the given entity is transitioning (so I can change the display of a button when it’s in progress). How would I go about doing that?

I’m assuming you know how to make the button change, based on some sensor value, so you just need some ideas for the sensor. You can use the template binary sensor , which will change from on to off based on the state of other entities. The only entity I can think of for this situation, is one you create, like an input boolean. (Might be something simpler, but nothing occurs at the moment).

So the idea I have is to base the template sensor on the state of the input boolean. When you start the transition, call the input_boolean.turn_on service to set the boolean to true/on, which in turn sets the sensor to on. That gives you an indication that the transition has begun.

Assuming that you want the display of the button to change back something else when the transition is done, I think you would have to create an automation that kicks in when one of the entities reaches the end state. For example if the transition ends at a certain brightness, have that state value be a trigger to run the input_boolean.turn_off service and reset the input boolean, thereby changing the sensor, and your button.

Of course, if you can base your button display on the state of the input boolean, then the sensor is redundant … but you did ask about a sensor.

Hi All
I’ve noticed that I am no longer able to run this light fader python script (after a recent home assistant update?). Has anyone had similar issues recently?

Previously, it would work like a charm.
Thanks!

It still works for me, although the first time I ran it the response was delayed.

I haven’t had time to debug yet, but I did just post a way to achieve pretty much the same effect, entirely in YAML. Won’t work so well for quick transitions, less then 15 seconds, but seems to work fine for slower transitions. YAML Light Fader for light entities that don’t support “transition”

Hi, can you make it so it calculates how much steps_pct is has to do instead of the default 1.
Like maybe have it calculate based on transition time and remaining pct? Like if I want to have it go from 1 to 100 in 15sec that it will calculate that is has to take steps of 6.67 now I have to have a minimum of 100seconds transition for 1-100 to work. If I do less than 100seconds for a 1-100 transition it will not go to 100pct but stop before it reaches 100.

Or am I missing something?

Hi. The script is just an example I have shared so others can do with it what they will. You could certainly make changes along the lines of what you suggested, although you might have to do some rounding on those percent changes.

Ah okay, thank you for the reply.

I have never worked with python before but it didn’t look that hard to understand. I’ve added a data get into the step_pct and added the option to rgb_color , color_temp to be able to change the colors/white.

Works like a charm, thanks for the code!

There you go! … and you’re welcome.

first of all: thanks for your script!

I have a lutron system in my house as well. I tried your automation and your python script, but the brightness never changes to more then 5% when i use it with a lutron light. it works with hue lights though, any ideas on what the issue could be?

Possibly because the Lutron integration doesn’t like receiving a stream of commands?

All that this python_script does (and the other version you tried in the other thread), is send a stream of light.turn_on commands containing increasing values of brightness. The number of commands, the time between each command and the amount of each brightness increase is calculated by the script.

You can manually simulate it using Developer Tools > Services. Call the light.turn_on service for a Lutron light using a brightness value of 125. Immediately call the same service with a value of 130. Increment and repeat as fast as you can (on the order of 1-5 seconds between service calls). That’s what the script does. By doing this manually, you will quickly discover if the Lutron integration supports this kind of operation.

If you want something non-blocking here’s something I made using pyscript and it’s async sleep:

1 Like

Thanks for this script… it’s a great idea that fills a great need. I am struggling to get this to work, and am finding that it sets the starting brightness correctly, but stays there. I am new to Python but don’t see any flaws in the code. I’ve tried this with several devices, particularly Z-wave dimmer switches. My goal is to be able to (say) fade a light from 20% to 100% over about an hour, and vice versa. So far my testing has been shorter durations (e.g. 30-60 seconds) if that makes a difference.

Hey @tal, can you spell out you’re scripts usage for those of us new to py scripts in HA?

I’ve setup everything for the service and put you’re script in the dir. https://www.home-assistant.io/integrations/python_script/

But when I ran it out of the box I was getting errors about “service not defined” due to the first line @service.

Taking that out… nothing would run… assuming since the scrips is a single function never fired… I then fired it by adding the following to the bottom…

entity_id = data.get('entity_id')
ending_brightness = data.get('ending_brightness')
minutes = data.get('minutes')
fade_light(entity_id, ending_brightness, minutes)

But now its erroring out on “state” not defined in line 3

What am I missing here?

There were some changes to the apis in the latest pyscript. I’ve updated the gist above. Also you should make sure you’re using pyscript, not the python scripting module built in.

1 Like

The pyscript extension available in HACS is different than the standad python_script feature provided by Home Assistant in the standard version of the software.

Pyscript is installed via hacs
Pyscript is not python_script!

Pyscript, in my opinion, is much easier to use.

WIthin integration pyscript is no longer available?
image

You first have to install HACS