Help with scene scripting for light control

I am very new to lua programming and although I have done some scripting in the past but i am a bit rusty. I would appreciate some help with the following:
- What I’m trying to accomplish: I have a z-wave switch controlling my stairs light. I also have a motion sensor by the stairs. I want the light to turn on at 20% brightness when the sensor detects motion at night.

  • What I’ve accomplished: I have been able to set the sensor to detect motion and turn on the lights at the correct brightness. I have been able to set the script to check the Day Night plugin to see if it is day or night and only run at night
  • What I am not being able to do: If the light is already on at full brightness, the light dims down to 20% if the motion sensor detects motion. I need to abort the scene if the light is already turned on.
  • This is my current script: Can you please help me with the syntax, what am I doing wrong?

local dID = 32 -- Device ID of your DayTime plugin local tID = 31 -- Device ID of target device local allow = false -- true runs scene during daytime, false runs it at night local status = luup.variable_get("urn:rts-services-com:serviceId:DayTime","Status",dID) local lighton = luup.variable_get("urn:schemas-upnp-org:device:DimmableLight:1","Status",tID) if lighton == "1" then return (false) else return ((status == "1") == allow

I will just say it before dozens of others do…

Read the PLEG basics guide by rexbeckett and then delete your code and use PLEG to do this. Pay special attention to the sequence expressions section, which will give you examples of how to create the logic of “if the light is already on when motion is detected, don’t turn the light down to 20%”

[quote=“grrgold, post:1, topic:186939”]I am very new to lua programming and although I have done some scripting in the past but i am a bit rusty. I would appreciate some help with the following:
- What I’m trying to accomplish: I have a z-wave switch controlling my stairs light. I also have a motion sensor by the stairs. I want the light to turn on at 20% brightness when the sensor detects motion at night.

  • What I’ve accomplished: I have been able to set the sensor to detect motion and turn on the lights at the correct brightness. I have been able to set the script to check the Day Night plugin to see if it is day or night and only run at night
  • What I am not being able to do: If the light is already on at full brightness, the light dims down to 20% if the motion sensor detects motion. I need to abort the scene if the light is already turned on.
  • This is my current script: Can you please help me with the syntax, what am I doing wrong?

local dID = 32 -- Device ID of your DayTime plugin local tID = 31 -- Device ID of target device local allow = false -- true runs scene during daytime, false runs it at night local status = luup.variable_get("urn:rts-services-com:serviceId:DayTime","Status",dID) local lighton = luup.variable_get("urn:schemas-upnp-org:device:DimmableLight:1","Status",tID) if lighton == "1" then return (false) else return ((status == "1") == allow[/quote]

You have used a device-type instead of a service ID - and you are missing an end statement. Try this:

local dID = 32 -- Device ID of your DayTime plugin local tID = 31 -- Device ID of target device local allow = false -- true runs scene during daytime, false runs it at night local status = luup.variable_get("urn:rts-services-com:serviceId:DayTime","Status",dID) local lighton = luup.variable_get("urn:upnp-org:serviceId:SwitchPower1","Status",tID) if lighton == "1" then return (false) else return ((status == "1") == allow end

[quote=“ctguess, post:2, topic:186939”]I will just say it before dozens of others do…

Read the PLEG basics guide by rexbeckett and then delete your code and use PLEG to do this. Pay special attention to the sequence expressions section, which will give you examples of how to create the logic of “if the light is already on when motion is detected, don’t turn the light down to 20%”[/quote]

thank you, where can i find that?

[quote=“RexBeckett, post:3, topic:186939”][quote=“grrgold, post:1, topic:186939”]I am very new to lua programming and although I have done some scripting in the past but i am a bit rusty. I would appreciate some help with the following:
- What I’m trying to accomplish: I have a z-wave switch controlling my stairs light. I also have a motion sensor by the stairs. I want the light to turn on at 20% brightness when the sensor detects motion at night.

  • What I’ve accomplished: I have been able to set the sensor to detect motion and turn on the lights at the correct brightness. I have been able to set the script to check the Day Night plugin to see if it is day or night and only run at night
  • What I am not being able to do: If the light is already on at full brightness, the light dims down to 20% if the motion sensor detects motion. I need to abort the scene if the light is already turned on.
  • This is my current script: Can you please help me with the syntax, what am I doing wrong?

local dID = 32 -- Device ID of your DayTime plugin local tID = 31 -- Device ID of target device local allow = false -- true runs scene during daytime, false runs it at night local status = luup.variable_get("urn:rts-services-com:serviceId:DayTime","Status",dID) local lighton = luup.variable_get("urn:schemas-upnp-org:device:DimmableLight:1","Status",tID) if lighton == "1" then return (false) else return ((status == "1") == allow[/quote]

You have used a device-type instead of a service ID - and you are missing an end statement. Try this:

local dID = 32 -- Device ID of your DayTime plugin local tID = 31 -- Device ID of target device local allow = false -- true runs scene during daytime, false runs it at night local status = luup.variable_get("urn:rts-services-com:serviceId:DayTime","Status",dID) local lighton = luup.variable_get("urn:upnp-org:serviceId:SwitchPower1","Status",tID) if lighton == "1" then return (false) else return ((status == "1") == allow end[/quote]

thanks, I will give it a try

@grrgold - The document can be found here: [url=http://forum.micasaverde.com/index.php/topic,21603.0.html]http://forum.micasaverde.com/index.php/topic,21603.0.html[/url]

Nothing wrong with Lua though and you don’t have to worry about plugin issues with version changes.

Sent from my iPad using Tapatalk

[quote=“Grwebster, post:7, topic:186939”]Nothing wrong with Lua though and you don’t have to worry about plugin issues with version changes.

Sent from my iPad using Tapatalk[/quote]

Agreed. Anyone with programming expierence might prefer Lua, I know I do. Nothing at all against PLEG though.

I’ve spliced in some code from one of my scenes. This uses LUUP to control the light rather than the UI object, so you’d clear those settings out. This scene would be triggered by the motion sensor.

local dID = 32           -- Device ID of your DayTime plugin (1=day, right?)
local tID = 31           -- Device ID of target device
local bID =33            --Device ID of the lamp
-- I comment out the allow and move it into the if/then 
--local allow = false       -- true runs scene during daytime, false runs it at night
local status = luup.variable_get("urn:rts-services-com:serviceId:DayTime","Status",dID)
local lighton = luup.variable_get("urn:upnp-org:serviceId:SwitchPower1","Status",tID)

--finds the light's current brightness
local lightLevel =luup.variable_get("urn:upnp-org:serviceId:Dimming1", "LoadLevelTarget", bID)

if  status == "0" and (lighton == "0" or (lighton == "1" and lightLevel < 20 ) )
then 
    luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget",{ newTargetValue="1" },bID)
    luup.call_action("urn:upnp-org:serviceId:Dimming1", "SetLoadLevelTarget", {newLoadlevelTarget = "20"}, bID)
end