Scene with dimming increment

Hi!

I would like to create a manually triggered scene that increase dimming level by 10 % from current level. Found a piece of LUA and tried to puzzle something together. Does not work though… any thoughts?

Would be very grateful for your input, code looks as per below:

local function Dimma()

local dID = 186

local mindim = 0
local maxdim = 100

local switchOnOff = luup.variable_get(“urn:upnp-org:serviceId:SwitchPower1”, “Status”, device)
local lightLevel =luup.variable_get(“urn:upnp-org:serviceId:Dimming1”, “LoadLevelTarget”, device)

if (switchOnOff == “1”) then
lightLevel =lightLevel-10
luup.call_action(“urn:upnp-org:serviceId:Dimming1”, “SetLoadLevelTarget”, {newLoadlevelTarget = lightLevel}, device)
else
lightLevel =100
luup.call_action(“urn:upnp-org:serviceId:Dimming1”, “SetLoadLevelTarget”, {newLoadlevelTarget = lightLevel}, device)
end

return Dimma()

When you read lightlevel you need to convert to a number before the arith (at a quick glance)?

Hi, thanks for the fast response!

I have no idea what I am doing, are totally illiterate when it comes to LUA.

What I have tried to do is to puzzle something together. All I really want is to be able to create 2 scenes (manually triggered):

  1. To increase the dimming of a bulb, from current level incrementally by +10% (of course, each time the scene is run)
  2. To decrease the dimming of a bulb, from current level incrementally by -10% (of course, each time the scene is run)

Now I am hoping for forum support and more skilled people to help me out :slight_smile:

local lightLevel =tonumber(luup.variable_get("urn:upnp-org:serviceId:Dimming1", "LoadLevelTarget", device))

Try This :slight_smile:

What are you using “device” and dID for, perhaps you’r mixing them up? As they are stated the device seems to be a global variable and the dID is local. Which one is the light and the switch?

This could work

[code]-- I have not tested it but it seems ok. When switchOnoff is on the light goes down with 10% each time the scene runs
– When switchOnoff is off the light goes up with 10% each time the scene runs
– The code could be simplified with else statements but are stated as is for understanding

local dID = 186 – The Bulb Device
local dID2 = 123 – The Switch

local switchOnOff = luup.variable_get(“urn:upnp-org:serviceId:SwitchPower1”, “Status”, dID2)
local lightLevel =tonumber((luup.variable_get(“urn:upnp-org:serviceId:Dimming1”, “LoadLevelTarget”, dID)))

if (switchOnOff == “1”) and (lightlevel > 0) then
lightLevel =lightLevel-10
luup.call_action(“urn:upnp-org:serviceId:Dimming1”, “SetLoadLevelTarget”, {newLoadlevelTarget = lightLevel}, dID)
end
if (switchOnOff == “0”) and (lightlevel < 100) then
lightLevel =lightLevel+10
luup.call_action(“urn:upnp-org:serviceId:Dimming1”, “SetLoadLevelTarget”, {newLoadlevelTarget = lightLevel}, dID)
end [/code]

Hi!

Dont quite understand, in the code you posted, where do I put the deviceID and where do I put the increment +10% and -10%?

I want scene 1 to increase the current dim level by 10% and decrease by 10% respectively in scene 2.

:slight_smile:

I changed my post above :slight_smile:

This code has an error:

local lightLevel =tonumber(luup.variable_get(“urn:upnp-org:serviceId:Dimming1”, “LoadLevelTarget”, device))

The problem is that luup.variable_get has 2 return values, the value and the time stamp of when the variable changed. See [url=http://wiki.micasaverde.com/index.php/Luup_Lua_extensions#function:_variable_get]http://wiki.micasaverde.com/index.php/Luup_Lua_extensions#function:_variable_get[/url]. to_number() takes the input and the base. So the result of the above code is almost certainly not what you want. But if you add a second set of parentheses you’ll get the single return value and tonumber() will give what you want:

local lightLevel =tonumber((luup.variable_get("urn:upnp-org:serviceId:Dimming1", "LoadLevelTarget", device)))

You are right :slight_smile: Thanks, modified above

Yet again I am stunned by this amazing forum :slight_smile:

Re-thought: What if I create two separate and manually triggered scene, one for a) ->increasing by 10 % and b) ->decreasing by 10%. Forget also the part where current setting is >0 and <100.

So I end up with One scene for increasing 10% from current level, and One scene for decreasing 10% from current level. In other words, all I would have to do to increase the light dimming level, would be to run the scene 1 time (as an example)

In other words, if we exclude the Switch, what should the code look like?

BONUS question: if I would like to include more than one dimmer/bulb, what would that look like?

Thanks A LOT!!! ;D

Have you tried to do this without LUA code at all.

Dimmers have actions:
SetStepDelta with an arg of Some Number like 10 for 10%
StepUp
StepDown

These are available via the advanced tab in the Scene Editor or PLEG actions.

[quote=“RichardTSchaefer, post:10, topic:197171”]Have you tried to do this without LUA code at all.

Dimmers have actions:
SetStepDelta with an arg of Some Number like 10 for 10%
StepUp
StepDown

These are available via the advanced tab in the Scene Editor or PLEG actions.[/quote]

Can you elaborate on this, I’m curious as to how to change the Ramp Rate if there is no Device Parameter available. Ideally, I would like to be able to set a device to “ramp up” from 0% to x% over n period of time (secs) and also the opposite “Ramp Down”. Is this possible?

Hi Mr Schaefer!

I thought your idea was brilliant!

I created a manually triggered scene, selected the dimmer, changed under Advanced Tab as per your instructions.
However, I cant get it to work.

Please see attached pictures.

Would be WONDERFUL if you could identify what I have done wrong.

Best regards,
P-a

I have a dimmer module in my living room for a standing lamp. The LED bulb in it actually responds well all the way down to 2% and is dimmer than a night-light at that level, unlike a lot of other LED bulbs out there that seem to put out a lot of light at 1%. With that in mind… here are the codes I have in two different scenes, one for dim up and one for dim down. I step it where it goes first to 5% which is a usable light level to walk around the living room in really dim light. then it jumps bigger from there. the same thing on the dim down. I did it the way I did so that if someone had manually adjusted the dimmer through the app to some odd number it would still hit the 5% and not drop to something unusable. I also had a problem where things would get tweaky when Vera tried to subtract 20 from 15 and she would hang up for about 5 minutes…doing it this way avoided that. I never have had a problem with addition going over 100% but i did with subtraction going below 0.

Just replace the dID number with your device ID and this thing will work. If you need to change the values for things just make sure that the integrity of the if and elseif statements still works…you dont want any weird overlap.

Step Up Code

local dID =23 local level = tonumber(luup.variable_get("urn:upnp-org:serviceId:Dimming1","LoadLevelStatus",dID),10) if level < 5 then level = 5 elseif level >= 80 then level =100 else level = level + 20 end local args = {} args["newLoadlevelTarget"] = level luup.call_action("urn:upnp-org:serviceId:Dimming1", "SetLoadLevelTarget", args, dID)

Step Down Code

local dID =23 local level = tonumber(luup.variable_get("urn:upnp-org:serviceId:Dimming1","LoadLevelStatus",dID),10) if level >= 25 then level = level -20 elseif level <= 5 then level = 0 else level = 5 end local args = {} args["newLoadlevelTarget"] = level luup.call_action("urn:upnp-org:serviceId:Dimming1", "SetLoadLevelTarget", args, dID)

Mr Priest,

You MADE my DAY, massive amount of credit heading your way :wink:

I LOVE this forum, have a wonderful evening.

Best regards,
P-a

[quote=“P-a, post:14, topic:197171”]Mr Priest,

You MADE my DAY, massive amount of credit heading your way :wink:

I LOVE this forum, have a wonderful evening.

Best regards,
P-a[/quote]

Not a problem. I’m still kind of a code idiot, but I have figured out how to grab bits and pieces from other peoples code they have posted and slap them together till they work. I’d never try to write you a piece of code from scratch to solve a problem since I’d get it wrong for a while I’m sure…but if I have the code you want already tested and in my system I have no problem sharing.

I have a document with all sorts of bits of code in it I’ve used or copied from elsewhere. If I know for certain it works the text is green, if it is copied from the forum for reference but I’ve not tested it myself it is yellow, if it is a work in progress or broken it is in red.

P-a, Sorry for the late response but we were offline for several days due to Irma. Here is some code we use in our parrot rooms to slowly dim the lights from 30 minutes before sunset through 9pm (their bedtime). You can modify the code for any time interval you need, but the core requirements are there. In addition, we wanted to be able to lower or raise the light level in the midst of the code and have the dim ramp use the current level and continue on. One last note - I placed this code in a PLEG action so that it can recover from a Lua restart and continue. I originally had it as a scene but that was an issue because a lot of my coding work occurred while that particular scene was active, resulting in lights frozen at a particular level and no way to ‘restart’ the scene. Please feel free to strip out the SceneWatch code as this is a custom variable that I use to indicate the status of the code as it progresses and for troubleshooting. I left it in place so you could see how things worked.

luup.log("PL_Act: c_BirdRm_Lights_Dim")
luup.variable_set("urn:upnp-org:serviceId:SceneWatch1","Variable1"," ",50)
luup.variable_set("urn:upnp-org:serviceId:SceneWatch1","Variable2"," ",50)
luup.variable_set("urn:upnp-org:serviceId:SceneWatch1","Variable3"," ",50)
luup.variable_set("urn:upnp-org:serviceId:SceneWatch1","Variable4"," ",50)
luup.variable_set("urn:upnp-org:serviceId:SceneWatch1","Variable5"," ",50)
varDevice1 = 52
varDevice2 = 53
varMax = 70
varMin = 10
varDimStep = (varMax-varMin)/10
varDimLevel1 = varMax
varDimLevel2 = varMax

varLupSS = luup.sunset()
varLuaSST = os.date("*t", varLupSS)
varSunsetTime = varLuaSST.hour * 60 + varLuaSST.min
varSSTHr = math.floor(varSunsetTime/60)
varSSTMin = varSunsetTime-(varSSTHr*60)
varSST = varSSTHr .. ":" .. varSSTMin
varSSoffset = 30

varStartTime = varSunsetTime - varSSoffset
varSTHr = math.floor(varStartTime/60)
varSTMin = varStartTime-(varSTHr*60)
varST = varSTHr .. ":" .. varSTMin

varEndTime = "21:00"
varETHr = tonumber( varEndTime:sub( varEndTime:find("%d+") ) )
varETMin = tonumber(varEndTime:sub(-2))
if varETHr and varETMin then
	varEndTime = varETHr * 60 + varETMin
else
	luup.log("ERROR: invalid end time")
	return false
end

varDelayPeriod = ((varEndTime - varStartTime)*6)
varCurTime = os.date("*t")
varCurrentTime = varCurTime.hour * 60 + varCurTime.min
varHumanTime = os.date("%m/%d/%Y %H:%M:%S")
	luup.variable_set("urn:upnp-org:serviceId:SceneWatch1","VariableName1","Sunset",50)
	luup.variable_set("urn:upnp-org:serviceId:SceneWatch1","Variable1",varSST,50)
	luup.variable_set("urn:upnp-org:serviceId:SceneWatch1","VariableName2","Start Time",50)
	luup.variable_set("urn:upnp-org:serviceId:SceneWatch1","Variable2",varST,50)
	luup.variable_set("urn:upnp-org:serviceId:SceneWatch1","VariableName3","Dim Delay",50)
	luup.variable_set("urn:upnp-org:serviceId:SceneWatch1","Variable3",varDelayPeriod/60,50)
	luup.variable_set("urn:upnp-org:serviceId:SceneWatch1","VariableName4","Dim Step",50)
	luup.variable_set("urn:upnp-org:serviceId:SceneWatch1","Variable4",varDimStep,50)
	luup.variable_set("urn:upnp-org:serviceId:SceneWatch1","VariableName5","First Step",50)
	luup.variable_set("urn:upnp-org:serviceId:SceneWatch1","Variable5",varHumanTime,50)

function DimDelay()
	varDimLevel1 = luup.variable_get("urn:upnp-org:serviceId:Dimming1","LoadLevelStatus",varDevice1)
	varDimLevel1 = varDimLevel1 - varDimStep
	luup.call_action("urn:upnp-org:serviceId:Dimming1","SetLoadLevelTarget",{ newLoadlevelTarget=varDimLevel1 },varDevice1)
	varDimLevel2 = luup.variable_get("urn:upnp-org:serviceId:Dimming1","LoadLevelStatus",varDevice2)
	varDimLevel2 = varDimLevel2 - varDimStep
	luup.call_action("urn:upnp-org:serviceId:Dimming1","SetLoadLevelTarget",{ newLoadlevelTarget=varDimLevel2 },varDevice2)
	varActionTime = os.date("%m/%d/%Y %H:%M:%S")
		luup.log("PL_Act: c_BirdRm_Lights_Dim")
		luup.variable_set("urn:upnp-org:serviceId:SceneWatch1","VariableName5","Last Action",50)
		luup.variable_set("urn:upnp-org:serviceId:SceneWatch1","Variable5",varActionTime,50)
	if (varDimLevel1 >= varMin) or (varDimLevel2 >= varMin) then
		luup.call_timer("DimDelay", 1 , varDelayPeriod,"","")
	end
end

-- if the current time is in the given interval, run the scene.
if varStartTime <= varCurrentTime and varCurrentTime <= varEndTime then
	if (varDimLevel1 > 0 or varDimLevel2 > 0)  then
		luup.call_timer("DimDelay",1,varDelayPeriod,"","")
		return true
	end
else
	return false
end

I had not actually tried the actions …
When I did I got not implemented in the log file … I do not know why Vera put these there and did not implement.

Howdy folks!

2nd issue: when I trigger the scene for increasing the level, in Vera interface, there is a delay before “scene play” button turns white again. This results in that I cannot push the button “fast” or several consecutive clicks. However, I have another scene that increases and decreases the volume of my sonos speakers?!

Both scenes are constructed more or less in the same manner.

Any remedy or suggestions, because this feature is useless with this problem?

Best regards!

Are you talking about initiating it from inside the Vera page? If so, everything has that pause for me there. If I run it from Authomation or Imperihome it runs as fast as I can tap it.

Hi,

In imperihome it does not run as fast as i can tap it…
I want your system behaviour. With one lamp in the scene
it is “acceptable”, IF i add 1 more lamp it sucks.

This also happen in my all lights off scene, sometimes
All lamps go out directly, but more often there is a delay
Of 30 sec on some lamps.

IF i turn lamps on/off manually in Vera, they react instantly.