Where is the appropriate place for this code - in the automation section?
The code shown here is a python script. The discussion above is about how to alter the script.
If you read the below link, it explains how to setup the python script integration. Once done, you can copy and paste the above code to a text file and save to a .py file in the appropriate location. In my case, I named the file light_fader.py, and saved it to config/python_scripts.
I will caution that it may or may not work well with your particular light entities. In my case, I am using Lutron Caseta, and this script works just fine. But no harm in trying either way.
Once setup, and having restarted HASS, you could call the python script as a service in an Automation or Script. Setting the parameters you want for
transition time, the lights should dim or brighten, depending on the current brightness percentage.
Here is an example. NOTE: the service name will come from your python script name, so look in Developer Tools >> Services for the appropriate "python_script … " name in your installation.
# Gradually fade from current, to these brightnesses - service: python_script.light_fader data: entity_id: light.kitchen_table end_level_pct: 12 transition: '00:15:00' - delay: '00:00:02' - service: python_script.light_fader data: entity_id: light.front_foyer_foyer_star end_level_pct: 28 transition: '00:15:00'
thank you so much - that is really helpful reply!
I got it working by adding this to my automation…thanks again!
#automation in my configuration.yaml - alias: "Media player playing" trigger: - platform: state entity_id: media_player.appletv from: 'paused' to: 'playing' for: seconds: 1 condition: - condition: state entity_id: sun.sun state: 'below_horizon' action: service: python_script.light_fader data: entity_id: light.basement_lights end_level_pct: 0 transition: '00:00:15'
I am now seeing an error in my home assistant log:
Any insight would be appreciated. Thanks!
core-ssh:/config# tail home-assistant.log exec(compiled.code, restricted_globals, local) File "light_fader.py", line 14, in <module> ZeroDivisionError: division by zero 2019-12-10 23:32:31 ERROR (SyncWorker_19) [homeassistant.components.python_script.light_fader.py] Error executing script: division by zero Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/components/python_script/__init__.py", line 195, in execute exec(compiled.code, restricted_globals, local) File "light_fader.py", line 14, in <module> ZeroDivisionError: division by zero
Wild guess: start pct = end pct? Would explain the “divide by zero”
I am fighting this error with a simple call service try. I use Milights, simple python scripts and all else works though.
Error executing script: 'int' object is not subscriptable Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/components/python_script/__init__.py", line 195, in execute exec(compiled.code, restricted_globals, local) File "light_fader.py", line 12, in <module> File "/usr/local/lib/python3.7/site-packages/RestrictedPython/Eval.py", line 35, in default_guarded_getitem return ob[index] TypeError: 'int' object is not subscriptable
Another day of trying, I dont really get it to work, but at least I have a new error :-X
Some more infos:
- Normal transitions work fine in HA
- I built an esp8266_milight_hub and integrated the bulbs via MQTT Autodiscovery. Transitions sthould also work regarding the documentation, but as “non-integer values are supported”, I wonder if this could be the cause of the error.
- As I also tried setting different (100-500) values at the hubs "Default transition period (milliseconds)" setting.
- Without further coding skills I would guess that the scripts wants an int but gets an float… or sth. like this.
I will try a manual MQTT setup later, but maybe you guys may have a clue whats wrong here.
Error executing script: 'NoneType' object has no attribute 'lower' Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/components/python_script/__init__.py", line 195, in execute exec(compiled.code, restricted_globals, local) File "light_fader.py", line 3, in <module> File "/usr/src/homeassistant/homeassistant/core.py", line 887, in get return self._states.get(entity_id.lower()) AttributeError: 'NoneType' object has no attribute 'lower'
Thank you to Frank and Finny for creating the python code. My house is about 90% Lutron Caseta, and this has been a huge help in creating easy to manage transitions.
@mrsnyds Thanks for sharing this awesome script, works great!
Glad you found it useful. Kudos to Finny Samuel for the original post that gave inspiration to the structure of the routine (Light Fader - Quick and Dirty ).
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.
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.
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
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?