multiple devices turn off after time in luup code

Hello. I’m not at all smart in luup codes. The thing is I need two switches to turn off after a while when motion sensor is tripped. I made the scene, everything works fine just that only one off the switches turns off. The code I am using is:


local device = 13 – Switch on device 13:
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”,“SetTarget”,{ newTargetValue=“1” },device)
luup.call_delay( ‘switch_off’, 10) – Call the switch off function after a delay of 10 seconds
function switch_off()
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”,“SetTarget”,{ newTargetValue=“0” },device)

end

local device = 20 – Switch on device 20:
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”,“SetTarget”,{ newTargetValue=“1” },device)
luup.call_delay( ‘switch_off’, 10) – Call the switch off function after a delay of 10 seconds
function switch_off()
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”,“SetTarget”,{ newTargetValue=“0” },device)

end

return true

and I am sure it is far from correct. :smiley: I tried to play with it, changed it million times but without results, only one switch (#13) is turning off after those 10 seconds.

EDIT:

The right code was this:

[11:01:22 PM] u------u: local device = 13

luup.call_delay( ‘switch_off1’, 10) – Call the switch off function after a delay of 10 seconds
function switch_off1()
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”,“SetTarget”,{ newTargetValue=“0” },device)

end

local device = 25

luup.call_delay( ‘switch_off2’, 15) – Call the switch off function after a delay of 15 seconds
function switch_off2()
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”,“SetTarget”,{ newTargetValue=“0” },device)

end

return true

and in interface I made for this sceene to turn on the two lights + trigger for sensor tripping.

You have two functions with the same name. Function names have to be unique. Rename one of your switch_off to switch_off1 and the other to switch_off2. Also change the names in call_delay to match.

(There are other ways of getting the same effect, such as consolidating everything into one function, but let’s keep things simple.)

Thank You, got it working.

I have another question. I want one device to switch on with a delay and then switch off later. What is wrong with this:


local device = 13 – Switch on device 13:

luup.call_delay( ‘switch_on’, 10) – Call the switch on function after a delay of 10 seconds
function switch_on()
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”,“SetTarget”,{ newTargetValue=“1” },device)

luup.call_delay( ‘switch_off’, 10) – Call the switch off function after a delay of 10 seconds
function switch_off()
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”,“SetTarget”,{ newTargetValue=“0” },device)

end

return true

Your switch_on function is missing an “end”.

It doesn’t work with a delay, it turns on immediately. Maybe because I have a trigger (sensor is tripped)?

Can I make the lights turning on without a trigger when the sensor is tripped?

You can have a scene that has no triggers or schedules, yes. You will have to activate the scene manually with the Run button.

Go to your scene’s “Devices” tab. If it is turning on the light, remove the action by pressing the little red X. Your Luup code turns on the light all by itself.

I am just curious but why did you do this with luup code.
I have some similar scenes that turn on the devices and after some time turn it off with the manage delays in the scene. No coding at all.

NOTE: That if Vera restarts in the middle of a scene … It will NOT run the delayed parts of the scene when it restarts.

Neither will this LUUP code example.

This can be a real problem if for example you intend to turn a pump on for 10 minutes.
Then Vera restarts in the middle … and the pump burns out because it keeps running!

You want to use PLEG or CountDown Timer plugins if you want reliable timed behavior in the context of Vera restarting.