User Tools

Site Tools


tutorials:slow_fade

Slow Fade

Many devices support the Ramp Rate parameter that you can include with any Dim/Brightenor 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:

tell xUnit “name of the unit” to slowFade( goalLevel, stepAmount, speed)

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:

tell xUnit “Bedroom Lamp” to slowFade( 50, 5, 30)

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:

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
tutorials/slow_fade.txt · Last modified: 2023/02/13 14:52 by 127.0.0.1