luup code to have sensor change state

I have been using a snippet of code that basically changes whatever the state of a light is to the other. If the light is on, it turns it off. It it’s off, it turns it on. The code is

local device = 115
– Switch on device 115:
local lul_tmp = luup.variable_get(“urn:upnp-org:serviceId:SwitchPower1”, “Status”, device)
if (lul_tmp == “1”) then
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”,“SetTarget”,{ newTargetValue=“0” },device)
else
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”,“SetTarget”,{ newTargetValue=“1” },device)
end

I wanted to use this with my craftsman garage door which does report a state, but doesn’t work. I have a sensor attached to the garage so that when the garage door opens at night it turns on the light in my entry way. This gives me a real time open or closed state. The garage door does report state, but it is delayed by a good 20 seconds before it changes.

If my sensor is device 120 will the following work?

local device = 120
– Poll device 120:
local lul_tmp = luup.variable_get(“urn:micasaverde-com:serviceId:SecuritySensor1”, “Tripped”, device)
if (lul_tmp == “0”) then
luup.call_action
else
luup.call_action
end

What would I need to have as the actions to run two different scenes, say scenes 100 for Tripped=0 and 101 for Tripped=1?

Thanks!

After you put so much work into it, I almost hate to tell you this.

You can toggle the state of a switch with a single line of Lua/LUUP

local DEVICEnum = 115 luup.call_action("urn:micasaverde-com:serviceId:HaDevice1", "ToggleState", {}, DEVICEnum)

Your garage door sensing code will probably not work. Not because of your code, but because the the door/window sensor that I presume you are using on your garage door is a battery operated device and polling such a device could take an hour or more to get a response. Battery devices sleep and Vera must wait for the waveup interval before Vera can poll the device.

Your real issue is that the senor’s notifications are taking too long(20 seconds) to reach Vera. Assuming that this is indeed what is happening, and that your delay is not in some even more convoluted code that you have not disclosed in this post, then the issue is most likely a weak mesh and or routing issue.

The notification(trip/untrip) from a door/window sensor should be almost instantaneous. You need to improve your mesh for the garage door’s sensor. Then your scenes will run immediately without unnecessary polling code.

Just for future reference, you can run any scene with LUUP like this:

local SCENEnum = 7 luup.call_action("urn:micasaverde-com:serviceId:HomeAutomationGateway1","RunScene",{ SCENEnum })
It’s useful for having one scene run one or more other scenes.

The window for sensor change is immediate. The garage doors built in notification to vera is where the lag is.

So the sensor (which does update immediately) wouldn’t work for status to determine which scene to run? ie if open run scene 2, if closed run scene 6?

Thanks for the help.

If your door sensor is notifying Vera immediately, then it certainly can be used with your code or as a standard scene trigger. There’s no need to poll.

What Craftsman garage door opener are you using that has a built-in notification? Are you using the MyQ plugin or something?

[quote=“Z-Waver, post:4, topic:190868”]If your door sensor is notifying Vera immediately, then it certainly can be used with your code or as a standard scene trigger. There’s no need to poll.

What Craftsman garage door opener are you using that has a built-in notification? Are you using the MyQ plugin or something?[/quote]

I am using the MyQ plugin and there is a significant 15-20 second delay in updating.