Lua Code for Blinking a Lamp, JackOLantern, Tree, Sign, Light etc.

Thought I’d post a customizable snippet of Lua scene code I wrote to vary DURATION and DEVICE #
Use:
CLICKING on you Scene will start your device blinking; CLICKING AGAIN will stop it blinking.

------  SET THESE TWO VARIABLES to match needs of your system -----
Secs   = 2   ---->>  Secs: Number of seconds between "Blinks"
Device = 123 ---->>  Device: Device#  of Outlet or Light to "BLINK"
-------------------------------------------------------------------
Call ='urn:upnp-org:serviceId:SwitchPower1';Do ='SetTarget';Nil =""
function BlinkOn()
 luup.call_action(Call,Do,{newTargetValue=1},Device)  -->BlinksOn
  if (Run == "1") then luup.call_timer("BlinkOff",1,Secs,Nil,Nil) end
end
function BlinkOff()
 luup.call_action(Call,Do,{newTargetValue=0},Device)  -->BlinksOff
  if (Run == "1") then luup.call_timer("BlinkOn",1,Secs,Nil,Nil) end
end
if (Run == "1") then Run = "0" --> NewClick while running=Stop,i.e.Run=0
 else Run = "1" luup.call_timer("BlinkOff",1,Secs,Nil,Nil) -->Else go on
end

For those not familiar with how to use Lua code… here’s more detail on how to use this to make your light / lamp / fan / tree / jack o lantern / warning sign or whatever blink on and off at a chosen interval:

1 Choose a Light or Outlet you’d like to “Blink” on and off; Look at SETTINGS tab to see “Device#”

2 Create a SCENE, give it a name. I’d suggest something such as “Blink Light” :slight_smile:

3 Click Scene’s wrench; go to “Luup” tab. Paste this entire code block into the tab

4 Change the DEVICE variable to match your device’s #. (My Example uses 123)

5 Change the SECS variable for seconds between blinks. (My Example uses 2)

6 Click on “Save luaa”. It will say “Success”; Click CLOSE to finish up the lua programming entry

7 Click on the Upper Right “X” to Close and Save the Scene. The SAVE button at the Top Right will be red, indicating there are changes. Click SAVE; Click CONTINUE. After the “Server Busy” indicator is finished, you’re ready to Blink your device.

That’s it!

– Troubleshooting –
The most common errors are:

  1. Choosing the wrong DEVICE #. Be sure you get the Device# and not Node or ID by mistake.
    Note that there are three “numbers” for every device on that Advanced Tab:
    Device #
    Note #
    ID #
    Sometimes in some systems, node and device can be identical. But Choose DEVICE# to be safe!

  2. Not copying the full code block into your Lua tab, or altering the code improperly.
    Include the header block/comment/variable sets in what you copy; you’ll need those.
    The only two pieces you should need to change would be:
    Secs = ## variable, and
    Device = ### variable.
    (Otherwise, leave the code intact, including spacing before/after variables, etc until you know it works)

Once things work and you are feeling adventurous, feel free to mess with it, but understanding what you’re doing is helpful. Comment statements within lines or at beginning of lines begin with ‘–’ So those can be altered at will, to end of line. But don’t let them “wrap” unless you start a new line also with ‘–’.

Much of the Quotes and Spacing are very picky; you can’t necessarily make any changes you like. : )

I tried to use variables that made sense for making things easy to change and I wanted the entire code block to fit into one lua tab screen, so you could see the whole code at once.

Play with it and have fun!

-JS

thanks a lot for that code!

One little suggestion from my side :wink:

It would be fine if the device would be switched off after completing the scene…

[quote=“juhuu, post:2, topic:170427”]thanks a lot for that code!

One little suggestion from my side :wink:

It would be fine if the device would be switched off after completing the scene…[/quote]
program a delay in the scene to set the amount of time you want it to run :slight_smile:

there might be a missunderstanding… :-\

I want to use the scene as alarm indicator, because of that the blinking should end after i press a button - and then all the lights should be switched off

add a virtual switch ? i.e. check virtual switch if it on , if not do not run lua

i will think about it, thank you

i am a beginner, so it might take a little bit longer :wink:

[quote=“juhuu, post:6, topic:170427”]i will think about it, thank you

i am a beginner, so it might take a little bit longer ;-)[/quote]
np we all have been there once…

I want to use this code as an alarm deterent. I would like to know how to do a lua code that could flash a light for about 5 minutes that stay ON steady after the 5 minutes?