Luup help on switching a siren on and off

Hmm, it seems im going to have to dig into scene codeing with luup as the standard options do not provide the flexibility needed for some features.

So… als a total programming noob im going to start off here with a specifick example after looking through some code snippets and not being able to find what i would like to achieve.

All feedback is welcome, even if its just a link to a usefull thread.

  1. Id like to trigger a siren for a certain amount of time upon scene activation (ALARM ON) and then stop the siren again (1 sec).

I took a look at the code for emergency lights an christmastree (blinking lights) on how to turn on and off something, but couldnt quite hack it.
How do i convert this code by @aa6vh to switching on a siren (change of serviceId) and then stopping it after 1 second? (does sBlink work for sirens or do i need another piece of code there?)

function DipOnPorchLight(stuff)
  luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget",{ newTargetValue="1" },19)
  if (sBlink == "1") then
    luup.call_timer("DipOffPorchLight", 1, "1", "", "")
  end
end
function DipOffPorchLight(stuff)
  luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget",{ newTargetValue="0" },19)
  if (sBlink == "1") then
    luup.call_timer("DipOnPorchLight", 1, "1", "", "")
  end
end

Or would this be closer? (havent got it to work yet)

local siren1 = 15
local On1 = 0.2

-- Keep siren on for preset time
function SirenOn()
luup.call_action("urn:upnp-org:serviceId:siren1","SetLoadLevelTarget",{ newLoadlevelTarget="1" },siren1)
luup.call_timer("SirenOn", 1, On1 , "", "")
end

-- Turn siren off
luup.call_action("urn:upnp-org:serviceId:siren1","SetLoadLevelTarget",{ newLoadlevelTarget="0" },Device1)
end

Anyone have any suggestions?

Is the “one second” the tricky part? I can do this with a standard scene GUI with an interval of 5 seconds, 10 seconds, … all the way up to 24 hours.

How do i convert this code by @aa6vh to switching on a siren (change of serviceId) and then stopping it after 1 second?

You should just be able to do luup.call_action(); luup.sleep(); luup.call_action(), assuming you plan to only have the siren sound once.

The exact parameters to luup.call_action() depend on the service file for your device. Post the D_xxx.xml and S_xxx.xml file here and we can point out the strings that have to match.

I bet luup.call_timer can’t do intervals shorter than one second. Indeed, if you are seeking exactly 0.2 seconds of siren sound, I think you are going to be disappointed. Delays and latency will ruin your day, I suspect.

You haven’t said whether you are putting this into a scene’s Luup code textarea, or into a plugin’s implementation file, or somewhere else. Some background information would help.

@futzle

Thanks for your feedback and sorry if I was a bit vague on the description, not usually my style.

The trick is in custom timing and stopping the device after that.

What I would like to achieve is activating a scene (alarm ON)
And then provide a short audible signal (much like a car alarm) of less than a second.

I want to try and do this via luup code in the scene.

Does that help to narrow it down?

Hi Henk,

Let’s break the problem down into parts that can be done independently.

  1. See if you can make it work with a known device like a binary switch first. the sample code you started with should pretty much work as-is.
  2. See if you can turn the siren on, or off, with a snippet of code in the “Test Luup Code” window.
  3. Then try to make them work together.

The code that you copied is to alternatively turn on and then turn off a light or device. And your code snipped does not include all of the code (there is additional code required to initialize the sequence).

Since you want the device to turn on, then off only once (if I read you correctly), you can simplify things considerably:

function SirenOff(stuff)
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”,“SetTarget”,{ newTargetValue=“0” },19)
end
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”,“SetTarget”,{ newTargetValue=“1” },19)
luup.call_timer(“SirenOff”, 1, “1”, “”, “”)

The minimum time that you can delay calling the SirenOff procedure (using the above code) is one second. If you need a smaller time, then you will need to utilize completly different logic.

The “call_action” statement above may not be the correct statement for actually turning on or off the siren - you will need to determine what works correctly for you.

@aa6vh

Thank you for chipping in.
I guess i need completely different Logic then as i would like a short “beep” from the device. Preferably less than a second and somewhere near to 0.1 or 0.2sec.

The device (Everspring Z-wave siren)
Shows acts as a binary light switch in Vera if that helps…

Geesh and i thought i was at least close :wink: Steep learning curve here!

Any suggestions on the Logic then?!?

Thx

Henk

Have you tried this simple code?

luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”,“SetTarget”,{ newTargetValue=“1” },19)
luup.sleep(100)
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”,“SetTarget”,{ newTargetValue=“0” },19)

This will turn on the siren, sleep 100 millisconds, then turn it off. That number in the sleep call is in milliseconds.

One problem I see is that ZWave devices can take some time to respond. You may find out that less than a second is too short. The device, being slow to respond, gets shut off before it has a chance to start. In your testing, be sure to use a long duration first to ensure it is working, before shortening the time.

I have not tried this, cannot say if it will work…

@futzle & @aa6vh

I have tested the steps @futzle suggested and used the code @aa6vh provided.
My findings are (tested against a (virtual) state device to make sure response times would be as little affected as possible by the hardware.

What i found is that the next 2 snippets of code will work:

Snippet1

function SirenOff(stuff)
  luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget",{ newTargetValue="0" },19)
end
luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget",{ newTargetValue="1" },19)
luup.call_timer("SirenOff", 1, "1", "", "")

Snippet2

local Siren1 = 19
local On1 = 500

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

My findings are that both have the same effect, switching the device on and of at the set timeinterval.
I still added the local variablesets to make the code a bit more easier to adapt for other device# and timeintervals
(which is why i like the second approch best).

HOWEVER and this was quite interesting, i have experimented with timings varying from 100 to approx 700 milliseconds. Surprisingly i could not actually see a difference in the actual time the state device was turned on.
Actually it seemed rather close to 1 second, same as the codesnippet provided above with the timing set to 1 sec.

It seems to me that maybe the luacode will accept timings smaller thn 1 second, but either Vera response times (or hardcoded minimum) do simply not allow for faster responses.

What are your thoughts on my findings?

Henk

I thought so. This isn’t uncommon in the computing world. A lot of “sleep” commands in programming languages will accept arbitrarily small numbers, but the actual sleep time can vary according to the operating system. Actually, it might not even be the sleep function itself; it might be that the Luup engines that handle devices are only checking their event queue once a second.

Either way, I think your plan to chirp a siren by using a small delay between “on” and “off” isn’t going to work.