This works great for my GE / Jasco ZW3101 zwave dimmer module! I know very little about python scripting and this was my first adventure into adding a python script to HA. I just wanted to expand on finnysamuel’s and add a few tips for new folks:
For starters I needed to enable python scripts, in configuration.yaml I added this line:
python_script:
I am running hassbian, so I created a scripts folder in the HA directory:
mkdir /home/homeassistant/.homeassistant/python_scripts
I created a fade_in and fade_out script, I am not sure if these needed to be executable or not, I made them just in case.
pi@hassbian:/home/homeassistant/.homeassistant/python_scripts $ ls -al
-rwxr-xr-x 1 homeassistant homeassistant 979 Apr 29 10:06 fade_in_light.py
-rwxr-xr-x 1 homeassistant homeassistant 1003 Apr 29 09:49 fade_out_light.py
Here is what my two scripts looks like:
Fade in:
Note: the only change I really made to finnysamuel’s work was at the end, I noticed I was unable to get to 100% brightness without it.
entity_id = data.get('entity_id')
sleep_delay = int(data.get('delay_in_sec'))
start_level_pct = int(data.get('start_level_pct'))
end_level_pct = int(data.get('end_level_pct'))
step_pct = int(data.get('step_in_level_pct'))
start_level = int(255*start_level_pct/100)
end_level = int(255*end_level_pct/100)
step = int(255*step_pct/100)
new_level = start_level
while new_level < end_level :
states = hass.states.get(entity_id)
current_level = states.attributes.get('brightness') or 0
if (current_level > new_level) :
logger.info('Exiting fade in - light already on')
break;
else :
logger.info('Setting brightness of ' + str(entity_id) + ' from ' + str(current_level) + ' to ' + str(new_level))
data = { "entity_id" : entity_id, "brightness" : new_level }
hass.services.call('light', 'turn_on', data)
new_level = new_level + step
time.sleep(sleep_delay)
data = { "entity_id" : entity_id, "brightness" : end_level }
hass.services.call('light', 'turn_on', data)
Fade Out:
entity_id = data.get('entity_id')
sleep_delay = int(data.get('delay_in_sec'))
start_level_pct = int(data.get('start_level_pct'))
end_level_pct = int(data.get('end_level_pct'))
step_pct = int(data.get('step_in_level_pct'))
start_level = int(255*start_level_pct/100)
end_level = int(255*end_level_pct/100)
step = int(255*step_pct/100)
new_level = start_level
while new_level >= end_level :
states = hass.states.get(entity_id)
current_level = states.attributes.get('brightness') or 0
if (current_level < new_level) :
logger.info('Exiting fade out - light already dimmed')
break;
else :
logger.info('Setting brightness of ' + str(entity_id) + ' from ' + str(current_level) + ' to ' + str(new_level))
data = { "entity_id" : entity_id, "brightness" : new_level }
hass.services.call('light', 'turn_on', data)
new_level = new_level - step
time.sleep(sleep_delay)
data = { "entity_id" : entity_id, "brightness" : end_level }
hass.services.call('light', 'turn_on', data)
Here are my automatons:
- alias: Family Room Light 15min Fade to 100% starting an hour before sunset
trigger:
platform: sun
event: sunset
offset: "-01:00:00"
action:
- service: python_script.fade_in_light
data:
entity_id: light.level
delay_in_sec: 9
start_level_pct: 0
end_level_pct: 100
step_in_level_pct: 1
- alias: Family Room Light 15min Fade to 30%
trigger:
platform: time
at: '22:00:00'
action:
- service: python_script.fade_out_light
data:
entity_id: light.level
delay_in_sec: 12.85
start_level_pct: 100
end_level_pct: 30
step_in_level_pct: 1
I hope this helps some folks
EDIT: Thanks jmart518 for the fix on the fade out.