Persisting variable states

I am trying to create the following scene:

Turn off lights when there has been no motion in the room for 30 minutes

local lul_tmp = luup.variable_get(“urn:micasaverde-com:serviceId:SecuritySensor1”,“Tripped”,31)
varTimes= varTimes+1
else varTimes=0

if (tonumber(varTimes)>29)
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”,“SetTarget”,{ newTargetValue=“0” },17)

I will call this scene every 1 minute via timers

Anyone knows if the state can be remembered between calls?

Thank you

Doesn’t that mean the sensor is tripped?

As shown in @Ap15e’s reply, you’ll want to set a variable on the device using luup.variable_set for it to persist. As an added bonus, this will survive a reboot of Vera, whereas setting global Lua variables doesn’t.

Global variables do persist for as long as the Lua interpreter runs. I use one in the Caddx alarm plugin to maintain a copy of the alarm system state. The trouble is that the lifetime of a Lua interpreter on Vera is not especially clear for things other than full-fledged Luup devices.

Also, your Lua code has a bug, using = for comparison; in Lua, use == for comparing two values for equality.

(Digression: In the linked example “Scene that runs only if a security sensor has been tripped for a set period of time”, I’m not sure that it’s good style to be creating variables in the “urn:micasaverde-com:serviceId:SecuritySensor1” namespace. Probably the code should be using its own private namespace for the TripPeriod variable.)

There’s no problem in creating variables in existing services, because they are created only for the current device. So if I create a variable in the SecuritySensor service for the device #3, other devices won’t be able to see it because it wouldn’t exist for them. The namespaces are per device, not per service.

Agreed, but that wasn’t my point. My point is that if I go around creating variables in someone else’s namespace, then I am assuming that that the owner of that namespace will never, not even in the future, add a variable with the same name (and probably a different meaning). If I use my own namespace then this isn’t going to happen.