first LUUP code... If on, then Turn Off

I have a GE remote scene controller, and as I understand it, the ‘Off’ buttons won’t actually turn the scenes off if programmed through Vera. So, I was hoping to come up with a LUUP code that basically works like this: when the scene is triggered by the remote, it will check if the lights are on. If they’re off, it will turn them on. But if they’re already on, it will turn them off.

So I came up with this code for the ‘if on’ then ‘turn off’ trigger:

local lul_lghtstat = luup.variable_get(“urn:schemas-upnp-org:device:DimmableLight:1”,“LoadLevelStatus”, 2)
if (lul_lghtstat) > 0) then
luup.call_action(“urn:schemas-upnp-org:device:DimmableLight:1”, “SetTarget”, {newTargetValue = “0”}, 2)
end

Does this code/logic make sense?

I think so. But I am not sure your syntax is correct. I think “DimmableLight:1” Should be “DimmableLight1”. I am no luup expert, but think that is the correct syntax for the unpnp device.

I know this works with the handheld remote. I am not sure whether you are talking about a handheld or a wall mount.

Once you have your scene programmed in the Vera, re-include the scene controller. Vera then programs the remote to directly control any lights in the scene. In this way, the off side of the scene button works.

The serviceId is not correct, per this:
http://wiki.micasaverde.com/index.php/Luup_Variables#Dimmable_Light

the samples presented here might help:
http://wiki.micasaverde.com/index.php/Luup_Scenes_Events#Lighting_and_Switch_Actions

[quote=“wscannell, post:3, topic:170472”]I know this works with the handheld remote. I am not sure whether you are talking about a handheld or a wall mount.

Once you have your scene programmed in the Vera, re-include the scene controller. Vera then programs the remote to directly control any lights in the scene. In this way, the off side of the scene button works.[/quote]

yeah, i’m using the battery remote. I tried setting up the scene and having the button on the remote be the trigger, but it would only turn on the scene (a scene controlling only lights)… it wouldn’t turn them off when pressing the ‘off’ side of the button.

thanks everyone for the advice on the code. i’ll try cleaning up my syntax.

will this luup code only run if the condition is met? otherwise, the scene will operate as normal? (ie, the scene is set to turn on lights, but if the lights are already on, then it will turn off lights).

[quote=“stylekyle, post:6, topic:170472”]thanks everyone for the advice on the code. i’ll try cleaning up my syntax.

will this luup code only run if the condition is met? otherwise, the scene will operate as normal? (ie, the scene is set to turn on lights, but if the lights are already on, then it will turn off lights).[/quote]

This http://wiki.micasaverde.com/index.php/Luup_Scenes_Events explains the hierarchy of priority when using luup code. Bottom line is with luup code in a scene it only executes if your code returns true.

so, in order for this to work, I would need an ‘else’ statement? basically:

if light off, then turn on, else, turn light off.

something like this for an if/then/else statement?

local lul_lghtstat = luup.variable_get("urn:schemas-upnp-org:device:DimmableLight1","LoadLevelStatus", 2) if( lul_lghtstat = 0 ) then( luup.call_action("urn:schemas-upnp-org:device:DimmableLight1", "SetTarget", {newTargetValue = "100"}, 2 ) else( luup.call_action("urn:schemas-upnp-org:device:DimmableLight1", "SetTarget", {newTargetValue = "0"}, 2 ) end

i’m still getting a code error when i test it, so i’m assuming something is still wrong with my syntax. do ‘spaces’, or ‘line breaks’ matter, or does LUA ignore them?

Looks like a few different problems:

a) correct the serviceId’s, as mentioned above
b) remove the parenthesis from around the [tt]call_action[/tt] blocks…

It may be simper for you to look at this example for the overall syntax:
http://wiki.micasaverde.com/index.php/Luup_Variables#Weather_Plugin

it doesn’t have the “[tt]return true[/tt]” part, but the syntax bits are correct.

cool, i modified the weather one, and the code works when tested using the LUA test code, it comes back ‘Message sent successful’. However, When i created a scene, and put the code in the LUUP section, and Run the scene, nothing happens. It doesn’t matter if the light is on or off first. Nothing happens. Here is my code…

local lul_lghtstat = luup.variable_get("urn:schemas-upnp-org:serviceId:DimmableLight1", "LoadLevelStatus", 5) if (lul_lghtstat == 0) then luup.call_action("urn:schemas-upnp-org:serviceId:DimmableLight1", "SetLoadLevelTarget", {newLoadLevelTarget = "100"}, 5) else luup.call_action("urn:schemas-upnp-org:serviceId:DimmableLight1", "SetLoadLevelTarget", {newLoadLevelTarget = "0"}, 5) end

seems like this should work…? is there a difference between LoadLevelTarget vs Target Value?

Check the serviceId string… character-for-character :wink:

If it doesn’t exactly match the string from the spec, or the examples provided for DimmableLight1, then it won’t work. When in doubt, hover your most over the Label for any Variable in the Advanced tab of the Device’s Dialog. The Bubble help shows the exact string to use.

There is no ‘serviceid’ string… there is a devicetype. I’m using UI5 on vera lite, and fields under the advanced tab are:

device_type: urn:schemas-upnp-org:device:DimmableLight:1

and

id: 5

It’s weird though, because, under the ‘settings’ tab, ID is 4.

@stylekyle,
I’ve added some examples to the wiki page about variables to hopefully clarify the situation:
http://wiki.micasaverde.com/index.php/Luup_Variables#Examples_2

There are now examples for both SwitchPower1 (Light Switches) and Dimmable1 (Dimmers), I’ve created example code snippets that demonstrate how to work with these Variables (get values, request changes). Presumably we can continue to augment these examples for the other variables that people commonly work with.

I have no preference as to where these examples go, or what format their in, but putting them here at the point where the variables are defined seems to make sense.

This should clarify, since many users can simple cut/paste these, and change the relevant device#.

I’ve also added some related, clarifying comments to another thread:
http://forum.micasaverde.com/index.php/topic,9198.msg62654.html#msg62654

as folks seem to get confused by the devices and services stuff…

cool. this helps. Only thing i’m still confused on, is where do I find the serviceid? It’s not in the advanced tab.

Hover your mouse over the Label on the left of the Variable your interested in, in the Advanced Tab of the Device. A bubble hint will appear with the service id

EDIT: Pictures attached, wording changed to include reference to “Advanced Tab of the Device

...where do I find the serviceid?

There’s another thread concerning this question which might help you:
http://forum.micasaverde.com/index.php/topic,9432.0.html

not sure where this is that you’re talking about. I hover over the labels on the advance tab and nothing happens. Perhaps its because I’m using UI5.

I’ve attached images to the above to show it under UI4 and UI5, as well as amending the wording to make it clearer where these can be seen within the UI.

oh, i get it. thanks for the picture. i didn’t realize the list in the advance tab was broken up. i was at the top and nothing happens up there. I’ve found the variables section midway down, and sure enough, the service ID shows up when i hover over one of the variables.

looks like there are different service id’s for different variables. so, for ‘LoadLevelTarget’, the serviceid is ‘urn:upnp-org:serviceID:Dimming1’. i’m assuming that is what i’ll use in my code for changing the light level.