Combine LUUP code in scenes

Once again looking for some assistance. :slight_smile:

I have some scenes in my VeraLite that monitor my alarm panel to determine if it is armed or not. Like many others do. I also installed Heliotrope to monitor the sun.

Currently under my “Disarm” scene I have the sytem turning on interior lights and it also uses LUUP code to Flash my porch light. I also have the same thing for “Arm” scene but it turns the interior lights off.

I also have a scene Combination Switch that is monitoring my Heliotrop, at which there are scenes running for my outside lights.

Current LUUP code:

[code]local array_lights = {19}
local original_status={}
local counter = 3
local delay = 2

function set_switch(device,value)
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”,“SetTarget”,{ newTargetValue=value },device)
end

function tree_on()
for i, device in ipairs(array_lights) do
set_switch(device,“1”)
end
luup.call_delay( ‘tree_off’, delay )
end

function tree_off()
counter = counter-1

if counter > 0 then
	for i, device in ipairs(array_lights) do
			set_switch(device,"0")
	end
	luup.call_delay( 'tree_on', delay )

else

–Set to original status
for i, device in ipairs(array_lights) do
set_switch(device,original_status[i])
end

end

end

–Save Original status
for i, device in ipairs(array_lights) do
original_status[i] = luup.variable_get(“urn:upnp-org:serviceId:SwitchPower1”,“Status”, device)
end

tree_off()[/code]

What I’m wanting to do on the “Disarm” scene is to check the status of the Combination Switch that is currently monitoring my Heliotrope status and if it’s off do not turn the interior lights on and if it’s on of course turn the interior lights on.

I have some bits of luup code pulled from this forum so I have never tried to combine them before.

Thanks

youataknow,

The way to get a Combination Switch’s current state is:

local device = 123 -- change this local status = luup.variable_get("urn:futzle-com:serviceId:CombinationSwitch1", "Status", device)

The status variable will contain the string “0” or “1”, which you can test in an if-statement.

To combine this code with your existing Luup code, just append it to the end, outside of any if- or function-blocks.

Futzle,

Thanks for the luup code also I wasn’t sure if just appending another if/and/then under the existing code would work or if I would have to wrap into the existing code. I’ll work on that tonight and reply back.

Futzle,

Ok I’ve been doing some more reading and tried to piece this together, but of corse it’s not working. So no laughing at my frankenstein. :slight_smile:

[code]local device = 38
local status = luup.variable_get(“urn:futzle-com:serviceId:CombinationSwitch1”, “Status”, device)
local FrontRoom = 20
local KidRoom1 = 29
local KidRoom2 = 28

function Lights_On()
if (status == “0”) then
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”,“SetTarget”,1,FrontRoom)
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”,“SetTarget”,1,KidRoom1)
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”,“SetTarget”,1,KidRoom2)
end
end[/code]

I was looking add what the outside light was doing to specify it and set a delay. Then I was also looking at another forum that is similar to this that you posted on back in July 2011. So please school me on more on this and soon I will not need to ask such simple questions.

Thanks

[quote=“youataknow, post:4, topic:172884”]luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget",1,FrontRoom) [/quote]

That third parameter (1) looks muy dodgy. The third parameter to a luup.call_action is supposed to be a { list }, like in your earlier examples in this thread:

luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget",{ newTargetValue=value },device)

The rest looks about right.

{ newTargetValue=value }
vs.
1

So that part I wasn’t sure about either. If I put in {newTargetValue=value} how does it know to to set the switch value to 1? Above was code pulled from this forum and I can see why they left it as a variable because of the timer and then to set it back to what it was. In this case I want them turn on so would it be:

{newTargetValue=1}

Thanks

Also I was just watching my logs with different ways of that being set and honestly after it does the flashing of the porch light it appears the luup code stops and never tries to check the status of the combination switch to turn on the inside lights. So I think there is something else wrong. ???

Sorry bro,
You seem to be much more experience than me. I just begun this week :), and getting used to Luup. I’m really good programmer in Java and .NET, but Luup lack of a debugger, and I miss it. I’m still wondering how to debug my code. Saw something about download the log file, but dont know how to get the password for root.
But it is a really simple question for someone like you.

  1. How I get the device # in Veralite
  2. What is wrong with this simple code, I just want to set 20% the light just if it is turned off when a sensor detect movement. THis code is in the Scene Luup.
    My guess is the device number; I tried ID and the # wen see the settings.

if (luup.is_night()) then local familyDimm = luup.variable_get("urn:upnp-org:serviceId:Dimming1", "LoadLevelStatus", 2) if (tonumber(familyDimm) == 0) then return true end else return false end

I really appreciate your help.

Experiment,

I’ve done some Java and PHP programming as well. By no means am I an expert there either. Now give me VB scripting and I can hack away. :slight_smile:

I’m fairly new as well and I’m sure Futzle could answer with no problems but I want to see what I can do. :slight_smile:

  1. The device number for any device click on the settings wrench of your device, then go to advance tab, and then you will scroll down and see a device_file line and then below that is “id” which should be the ID of that device for your coding.
  2. As far as that code part I’ll leave that one to Futzle. :slight_smile: I’m trying to get mine to work as well.

For logging where I’m looking for simple stuff is http://deviceip/cgi-bin/cmh/log.sh?Device=LuaUPnP. I know you can ssh into the veralite and look in the var / log files to but I haven’t messed much with that.

Hope it helps

Thanks a lot! THe logs look great to troubleshoot

I was using both: ID and # but none worked for me. The light always set to 20% when movement is detected at night, no matter if it is on or off.
Thank you!!! I’m still learning but always willing to help people, becuase I like people help me.

[quote=“youataknow, post:6, topic:172884”]If I put in {newTargetValue=value} how does it know to to set the switch value to 1? 


{newTargetValue=1}

Yes, exactly that.

Your code doesn’t say what to return in this case:

[ul][li]Night time (luup.is_night() is true), and[/li]
[li]Lamp is at 0%.[/li][/ul]

Follow the logic and nesting of your if-else blocks and you’ll see it falls to the bottom without hitting any return statements.

If I recall correctly, these pieces of code have an implicit “return true” at the end of them, so that might explain why you are always seeing the light change.

Yes, I thought that’s what you wanted. Better post your whole code again and state clearly what kind of timing you are expecting.

Futzle,

No problem. Sorry I wasn’t clear in the first go around. Let’s see if I can do better. :slight_smile:

So to keep this simple let’s just focus on when my alarm panel disarms.

Currently when the alarm is disarmed a scene called SystemDisarm is triggered and does the following:
Step1: Under Devices Lights it turns on interior lights
Step2: It runs LUUP code (below) which flashes my porch light a few times.

[code]local array_lights = {19}
local original_status={}
local counter = 3
local delay = 2

function set_switch(device,value)
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”,“SetTarget”,{ newTargetValue=value },device)
end

function tree_on()
for i, device in ipairs(array_lights) do
set_switch(device,“1”)
end
luup.call_delay( ‘tree_off’, delay )
end

function tree_off()
counter = counter-1

if counter > 0 then
	for i, device in ipairs(array_lights) do
			set_switch(device,"0")
	end
	luup.call_delay( 'tree_on', delay )

else

–Set to original status
for i, device in ipairs(array_lights) do
set_switch(device,original_status[i])
end

end

end

–Save Original status
for i, device in ipairs(array_lights) do
original_status[i] = luup.variable_get(“urn:upnp-org:serviceId:SwitchPower1”,“Status”, device)
end

tree_off()
end[/code]

This works great but it turns my interior lights on during the day and night, which isn’t very convenient.

So what I’m wanting to do is disable the lights in the devices tab from turning on. Rebuild my LUUP code to not only flash the porch light like above but then check my CombinationSwitch (which tracks Heliotrope app) and only if it’s night time turn on the interior lights.

What I was doing was just appending my new code to the bottom of the existing code like:

[code]ocal array_lights = {19}
local original_status={}
local counter = 3
local delay = 2

function set_switch(device,value)
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”,“SetTarget”,{ newTargetValue=value },device)
end

function tree_on()
for i, device in ipairs(array_lights) do
set_switch(device,“1”)
end
luup.call_delay( ‘tree_off’, delay )
end

function tree_off()
counter = counter-1

if counter > 0 then
	for i, device in ipairs(array_lights) do
			set_switch(device,"0")
	end
	luup.call_delay( 'tree_on', delay )

else

–Set to original status
for i, device in ipairs(array_lights) do
set_switch(device,original_status[i])
end

end

end

–Save Original status
for i, device in ipairs(array_lights) do
original_status[i] = luup.variable_get(“urn:upnp-org:serviceId:SwitchPower1”,“Status”, device)
end

tree_off()

– Find Status of HelioTrope Combination Switch
local device = 38
local status = luup.variable_get(“urn:futzle-com:serviceId:CombinationSwitch1”, “Status”, device)
local FrontRoom = 20
local kidroom1 = 29
local kidroom2 = 28

function Lights_On()
if (status == “1”) then
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”,“SetTarget”,“newTargetValue=1”,FrontRoom)
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”,“SetTarget”,“newTargetValue=1”,kidroom1)
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”,“SetTarget”,“newTargetValue=1”,kidroom2)
end
end[/code]

Now what I’m thinking is that it would be more like:

[code]local combo_switch = {38}
ocal status = luup.variable_get(“urn:futzle-com:serviceId:CombinationSwitch1”, “Status”, combo_switch)
local front_room = {20}
local kid_room1 = {29}
local kid_room2 = {28}
local array_lights = {19}
local original_status={}
local counter = 3
local delay = 2

function lights_on()
if (status ==“1”) then
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”,“SetTarget”,“newTargetValue=1”,front_room)
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”,“SetTarget”,“newTargetValue=1”,kid_room1)
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”,“SetTarget”,“newTargetValue=1”,kid_room2)

		function set_switch(device,value)
			luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget",{ newTargetValue=value },device)
		end


		function tree_on()
			for i, device in ipairs(array_lights) do
				set_switch(device,"1")
		end
		luup.call_delay( 'tree_off', delay )
		end

		function tree_off()
			counter = counter-1

		if counter > 0 then
			for i, device in ipairs(array_lights) do
			set_switch(device,"0")
		end
		luup.call_delay( 'tree_on', delay )

		 else

   --Set to original status
		for i, device in ipairs(array_lights) do
			set_switch(device,original_status[i])
		end

		end

else
function set_switch(device,value)
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”,“SetTarget”,{ newTargetValue=value },device)
end

		function tree_on()
			for i, device in ipairs(array_lights) do
				set_switch(device,"1")
		end
		luup.call_delay( 'tree_off', delay )
		end

		function tree_off()
			counter = counter-1

		if counter > 0 then
			for i, device in ipairs(array_lights) do
			set_switch(device,"0")
		end
		luup.call_delay( 'tree_on', delay )

		 else

   --Set to original status
		for i, device in ipairs(array_lights) do
			set_switch(device,original_status[i])
		end

		end

end
[/code]

Sorry if this makes it worse. But thanks for being patient.

Hi youataknow,

Thanks for the extra description. I think that you’re pretty close.

This bit at the end:

function Lights_On() if (status == "1") then -- (stuff) end end

you wrapped in a function block. Strip out the contents of the function and put them in naked.

if (status == "1") then -- (stuff) end

This way the statements will run every time the scene is executed (well, except for when status ~= “1”, because of your if-statement).

Also, replace these:

luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget","newTargetValue=1",FrontRoom)

with these:

luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget", { newTargetValue=1 } ,FrontRoom)

The third parameter has to be a Lua thing called a table, and tables are marked with braces, not quotes.

Unnecessary lesson on functions follows


When you wrap statements in [tt]function foo() 
 end[/tt] you are telling Lua to not run those statements right now, but to remember them and save them up for when you later say [tt]foo()[/tt] (that is when the statements are run). If you define a function with [tt]function foo() 
 end[/tt] but never call it with [tt]foo()[/tt] then the statements will never run.

That means that this would also be a solution:

[code]function Lights_On()
if (status == “1”) then
– (stuff)
end
end

Lights_On()[/code]

Your suggested code at the end of your last post is actually getting colder on the trail. Nesting function inside function is wrong*. But it does reveal which one of the many ways that new programmers misunderstand functions, so it was helpful to see it.

  • This rule is a lie, but it’s a useful one; when you know why it’s a lie, you will not need the rule any more.

Thanks. I’ll work on this tonight. Yeah. I’ve never had formal training, schooling, etc, in what I have done. My stuff (other than this) works. It is by no means pretty or even efficient. So learning the “rules” to do it properly is always helpful.

[quote=“futzle, post:15, topic:172884”]Unnecessary lesson on functions follows


When you wrap statements in [tt]function foo() 
 end[/tt] you are telling Lua to not run those statements right now, but to remember them and save them up for when you later say [tt]foo()[/tt] (that is when the statements are run). If you define a function with [tt]function foo() 
 end[/tt] but never call it with [tt]foo()[/tt] then the statements will never run.[/quote]

futzle, do you use some sort of IDE (like luaedit, LuaEdit - Free IDE/Debugger/Editor for Lua) for your code?

vi is the best IDE I know.

lol, should have known :wink:

Let the editor wars begin :slight_smile:
Real men (Sorry futzle) have emacs in their .login

I do use emacs (on windows) from their I can:
Edit files, it knows about different languages (including lua) and can format the files.
(In 20+ years I have not found a language it can’t handle 
 it knows: .java .js .cs .pl .c .cc .cs .json .xml 
 you name it it can handle it.)
It highlights the keywords for each language 
 does indentation properly (for each language!).
Can skip over blocks (where the definition of a block is language specific).

I run local shells (Windows cmd and lua) in the editor they look like files 
 but you type in a line and they get executed. The output is in the same editor window. Great for running windows commands or testing LUA syntax. It also run Remote Shells on Vera. Similar to local shells, but I execute commands remotely. It also allows opening up files remotely on Vera AND editing them. I Open up the log file and review it. Just hit the refresh key to get the latest updates 
 all while sitting on my windows desktop. Actually I have almost the exact experience when I sit on a linux desktop.

Emacs was created around the time the term “Artificial Intelligence” was created, I think the early 80’s. It’s not the kind of editor you learn in a day 
 but it is the kind of editor you will use for life!

I forgot 
 it also has a vi emulation mode!