User Tools

Site Tools


tutorials:slow_fade

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

tutorials:slow_fade [2019/11/17 13:06]
James Sentman created
tutorials:slow_fade [2019/11/18 12:08] (current)
James Sentman finished article and added code
Line 1: Line 1:
 =====Slow Fade===== =====Slow Fade=====
-Many devices support the Ramp Rate parameter that you can include with any [[dictionary:​unitcontrol:​dim|Dim/​Brighten]]or [[dictionary:​unitcontrol:​turnon|Turn On]] command. If the protocol you are using does not support this there are several ways you can simulate something like it in a script. ​+Many devices support the Ramp Rate parameter that you can include with any [[dictionary:​unitcontrol:​dim|Dim/​Brighten]]or [[dictionary:​unitcontrol:​turnon|Turn On]] command. If the protocol you are using does not support this there are several ways you can simulate something like it in a script. ​This can be particularly useful in simulating a low dawn wakeup, or fading out lights at bedtime so that if you still walk through them on the way to bed they won’t be completely dark. 
 + 
 +This code adds an AppleScript handler to the Unit. You can control the goal level, either up or down, the step amount that each execution will raise or lower the brightness by and the time interval between adjustments. The command is used in a Tell block like this: 
 + 
 +<​code>​ 
 +tell xUnit “name of the unit” to slowFade( goalLevel, stepAmount, speed) 
 +</​code>​ 
 + 
 +This example would either dim or brighten the Bedroom Lamp until the value reached 50%. It would adjust it by 5% each time and execute every 30 seconds: 
 + 
 +<​code>​ 
 +tell xUnit “Bedroom Lamp” to slowFade( 50, 5, 30) 
 +</​code>​ 
 + 
 +One thing to keep in mind is that not all protocols can adjust the brightness instantly. If you send too many commands to a ZWave device too quickly you might find unexpected things happening if there were too many commands stacked up. I would suggest 5 seconds as a minimum interval for such things but you should experiment for what works with your system. 
 + 
 +To create the command paste this code into the On script of any Unit you wish to be able to slow fade: 
 + 
 +<​code>​ 
 + 
 +on slowFade(goalLevel,​ stepAmount, speed) 
 +  
 + -- get our current value as we need it in several places below 
 + set CurrentValue to (value of (thisUnit)) 
 +  
 + -- if we are already at that level then we do nothing 
 + if CurrentValue = goalLevel then 
 + return 
 + end if 
 +  
 + -- the stepAmount should always be a positive value when passed to this handler 
 + -- if the goal level is less than the current value then we need to subtract that amount 
 + if goalLevel < CurrentValue then 
 + 
 + -- we are dimming the device 
 + set newLevel to CurrentValue - stepAmount 
 +  
 + -- can't dim below 0 so just dim to zero and be done 
 + if newLevel ≤ 0 then 
 + dim (thisUnit) to 0 
 + return 
 + end if 
 +  
 + --if we have reached our goal level then we are also done 
 + if newLevel ≤ goalLevel then 
 + dim (thisUnit) to goalLevel 
 + return 
 + end if 
 +  
 + -- we still have some dimming to do 
 + dim (thisUnit) to newLevel 
 + --and we want to execute again in the "​speed"​ number of seconds 
 + -- the do later command takes milliseconds so multiply that by 1000 
 + -- and pass the same 3 parameters to us again using the with data parameter 
 + do later "​slowFade"​ in (speed * 1000) with data {goalLevel, stepAmount, speed} 
 +  
 + else 
 + -- we are brightening the device 
 + set newLevel to CurrentValue + stepAmount 
 +  
 + -- can't brighten above 100 so just set that value and be done 
 + if newLevel ≥ 100 then 
 + dim (thisUnit) to 100 
 + return 
 + end if 
 +  
 + -- if we have reached our goal level then we are also done 
 + if newLevel ≥ goalLevel then 
 + dim (thisUnit) to goalLevel 
 + return 
 + end if 
 +  
 + -- we still have some brightening to do 
 + dim (thisUnit) to newLevel 
 +  
 + -- we want to execute this again in the "​speed"​ number of seconds 
 + -- the do later command takes milliseconds so multiple that by 1000 
 + -- and pass the same 3 parameters to us again using the with data parameter 
 +  
 + do later "​slowFade"​ in (speed * 1000) with data {goalLevel, stepAmount, speed} 
 +  
 +  
 + end if 
 +  
 +  
 +end slowFade 
 + 
 +</​code>​ 
 + 
 + 
tutorials/slow_fade.1573995988.txt.gz · Last modified: 2019/11/17 13:06 by James Sentman