Weird behavior after running new code I put together-is there an error in it?

Okay, so I had a small luup script to “arm” my Vera into away mode. I then recently added a conditional so the system will not arm if a door or windows is open, and flash the portch light 3 times. I’m now getting weird behavior, and I figure theres something wrong with the code causing the weird behavior in my other LUUP codes. FYI Node 4 is a speaker that makes beeping sounds, and device 27 is the porch light. The code seems to work, other than the weirdness with other scenes after implementing. Any help is appreciated much!! Here is the code I put together:

Device = "urn:micasaverde-com:serviceId:SecuritySensor1"

KidsSlider = luup.variable_get(Device,"Tripped",16)
LoungeSlider = luup.variable_get(Device,"Tripped",12)
MasterSlider = luup.variable_get(Device,"Tripped",14)
FrontDoor = luup.variable_get(Device,"Tripped",10)

function arm (empty) 
  luup.call_action('urn:micasaverde-com:serviceId:HomeAutomationGateway1', 'SetHouseMode', { Mode=2 }, 0)
end


if (KidsSlider == "0") and (LoungeSlider == "0") and (MasterSlider == "0") and (FrontDoor == "0") then
  luup.call_action('urn:micasaverde-com:serviceId:ZWaveNetwork1','SendData',{Node='4',Data='112 4 6 1 5'},1)
  luup.sleep(9000)
  luup.call_action('urn:micasaverde-com:serviceId:ZWaveNetwork1','SendData',{Node='4',Data='112 4 6 1 6'},1)
  luup.call_delay("arm", 20, "") 

else

  luup.call_action('urn:micasaverde-com:serviceId:ZWaveNetwork1','SendData',{Node='4',Data='112 4 6 1 4'},1)

  counter = 3
  original_status = luup.variable_get("urn:upnp-org:serviceId:SwitchPower1","status",27)

  while counter > 0 do
    counter = counter-1
    luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget",{ newTargetValue="1" },27)
    luup.sleep(1000)
    luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget",{ newTargetValue="0" },27)
    luup.sleep(1000)
  end

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

end

All your quotes are pretty did you copy off this forum? if you did then the code you copied was not formatted correctly. You will need to replace all quotes.

@Sorin this is an on going issue, yet another user has issues with quotes.

I’m not sure what button I’m supposed to hit to keep that from happening when I post–I have all the codes properly formatted in notepad over here…

To be clear, my issue is not with the quotes–that happened when I created this post.

I’ve re-formatted the code so that it’s readable in the forum. If you want to see how, then just go in to edit it again.

I also changed the code by moving the [intended] global function arm() out of the scope of the if statement.

That may help, but I have to say that your use of luup.sleep() is highly questionable, especially with a long delay like 9000 which may well make the whole Vera unstable. You should use luup.call_delay() for implementing any significant delay.

…also, a bit concerned about the final end statement.

Thanks so much for the assistance–the sleep code has been in place for 2 years–the conditional and light flash are what I added. That being said, I’m always open to improving the code, and will look into using delay instead. What’s the issue with the end statement?–if the conditional says open after running the code, it would certainly explain the weirdness I’m seeing…

Hi JTG
I think there is problem in your _get commands - as that returns TWO values, the variable and a timestamp. So the test ==“0” may fail. To make certain it works, I would declare the variables as local with a dummy TimeStamp. eg

local KidsSlider, tStamp = luup.variable_get(Device,“Tripped”,16)

Also the long Sleep statements can block / timeout other activities, so may be stopping your other activities. (You have not said what sort of weirdness you are getting). I would possibly put the light flashing into a scene, with delayed actions between on and off, (or you could program it using luup.call_delay)

Octoplayer

The weirdness I’m seeing seems to be that other scenes based on events are sometimes not triggering. It may be only happening when no sensors are tripped, and the system arms.

Why is it there? Unless I’m mistaken, there’s no matching do or function or if, etc., at the start, apart from the implicit function() statement which the code is wrapped in before it’s called.

I thought I needed that end to mark the end of the conditional, no? Its a big if/then.

If [nothing open]
then [long arming sound, delay, another final arming sound, change to away mode]
else [chime, flash porch light 3 times]

I found the (sensor1 == “0”) bit of code from somewhere in the community postings, and the conditional SEEMS to work great–If I open a door, and try to arm it, it beeps at me and flashes the lights. If all doors are closed, it seems to arm fine. since the change, I’m just getting reports of random additional beeps at other times of day, and that another scene to auto-lock the front door after 10 minutes isn’t triggering anymore. also, in regards to sleep, I like the fact that nothing else will work for those 9 seconds–is that the only downside to sleeping? allowing nothing to interrupt the arming sequence is a good thing. If so, I may want to keep it the way it is.

The else innards aren’t properly indented, so it makes the end statement look extraneous. It’s correct code, but it’s misleading and hard to follow because of that simple thing.

On the luup.variable_get() for original_status, the variable name is not properly capitalized. If that’s working, it’s a happy accident of the way SetTarget responds to the nil you’re probably giving it later/below.

And those luup.sleep()s are a nightmare, but given what you have to do to make simple delays, it’s understandable why. Still, risky business. Long-running scripts, to which luup.sleep() can be a contributor, can cause deadlocks (which cause reloads).

2 Likes

Thanks for chipping in to help me–I really appreciate the way people are giving of their time to help. What changes do you recommend to make this better? What did I do wrong exactly with the original status part? I’m fairly clever, but clever enough to know when I’m out of my depth, which is why I’m here asking for help.

1 Like
  1. In the luup.variable_get() statement, you have the variable name “status” but it must have a capital-S to start (“Status”). Variable names are case-sensitive.

  2. Take all of the code from the else to the final end, but not including those two lines, and indent it one level.

  3. Don’t worry about the sleeps unless it becomes a problem, but you may want to leave yourself a comment not to stretch those out too much, or add too many more blinks (each blink contributes 2 seconds of delay/sleep). This is such a comment in Lua (comments begin with --):

-- Note: don't make the sleeps longer or add blinks--long-running scripts can cause deadlocks

My bad (but it was better than the original!)

I’ve corrected the OP formatting again. Apologies.

How does this look? I also moved orig_status to the beginning instead of inside the conditional.

Device = "urn:micasaverde-com:serviceId:SecuritySensor1"
orig_status = luup.variable_get("urn:upnp-org:serviceId:SwitchPower1","Status",27)

KidsSlider = luup.variable_get(Device,"Tripped",16)
LoungeSlider = luup.variable_get(Device,"Tripped",12)
MasterSlider = luup.variable_get(Device,"Tripped",14)
FrontDoor = luup.variable_get(Device,"Tripped",10)

function arm (empty) 
    luup.call_action('urn:micasaverde-com:serviceId:HomeAutomationGateway1', 'SetHouseMode', { Mode=2 }, 0)
end

-- Note: don't make the sleeps longer or add more blinks--rigpapa says long-running scripts can cause deadlock

if (KidsSlider == "0") and (LoungeSlider == "0") and (MasterSlider == "0") and (FrontDoor == "0") then

	luup.call_action('urn:micasaverde-com:serviceId:ZWaveNetwork1','SendData',{Node='4',Data='112 4 6 1 5'},1)
	luup.sleep(9000)
	luup.call_action('urn:micasaverde-com:serviceId:ZWaveNetwork1','SendData',{Node='4',Data='112 4 6 1 6'},1)
	luup.call_delay("arm", 20, "")

else
--play a beep and flash the porch light 3 times

	luup.call_action('urn:micasaverde-com:serviceId:ZWaveNetwork1','SendData',{Node='4',Data='112 4 6 1 4'},1)

	counter = 3
	while counter > 0 do
		counter = counter-1
		luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget",{ newTargetValue="1" },27)
		luup.sleep(1000)
		luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget",{ newTargetValue="0" },27)
		luup.sleep(1000)
	end

	luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget",{ newTargetValue=orig_status },27)
end

Casual code review looks good. One would normally indent the comment under the else as well, but style is individual.

I have pasted the code back into the Scene. Fingers crossed!!

100% stable now–thanks for all the help!!