Blinking Christmas tree

so i got time and a dimmer to test on which makes it a lot easier debugging :slight_smile:

-- code for multiple dimmers to make a top-notch christmas tree
-- Enter the device ID's here in the array_dimmers comma-separated

local array_dimmers = {34,47}
local original_status= {}
local counter = 10
local delay   = 1
local dimmin  = 0
local dimmax  = 100
local a = 0
function set_switch(device)
  for a = 1, dimmin do luup.call_action("urn:upnp-org:serviceId:Dimming1", "SetLoadLevelTarget", {newLoadlevelTarget=a}, device)
  end

  for a = dimmax, 1, -1 do luup.call_action("urn:upnp-org:serviceId:Dimming1", "SetLoadLevelTarget", {newLoadlevelTarget=a}, device)
  end
end

function tree_on()
   for i, device in ipairs(array_dimmers) do
     set_switch(device)
   end
   luup.call_delay( 'tree_off', delay )
end

function tree_off()
  counter = counter-1
   if counter > 0 then
    for i, device in ipairs(array_dimmers) do set_switch(device, "0")
    end
  luup.call_delay( 'tree_on', delay )

 else

 --Set to original status
   for i, device in ipairs(array_dimmers) do set_switch(device,original_status[i])
   end
 end
end

--Save Original status

for i, device in ipairs(array_dimmers) do original_status[i] = luup.variable_get("urn:upnp-org:serviceId:Dimming1", "SetLoadLevelTarget", {newLoadlevelTarget=status}, device)
end

luup.call_delay( 'tree_off', "0" )

still give me error, probably messed up some arguments… :frowning:
if i use luup.call_action i get an error in lua , but if i use luup.call_delay it can run a function … ehh, can someone explain ? by design or bug or ?

the following works and makes the fibaro dimmer blink 1 time or if u press the run scene button multiple times it turns it into a disco light.

here is how :

click on AUTOMATION
click on New scene
click on LUUP

On the left top you see : “New scene” , call it whatever you want (it is the name of the scene) or just: “blink light”

in the field which says “Code” you place the code :

-change the device nr 34 into the device you want to blink, has to be appliance switch or light or dimmer
-find the device number :
on device in dashboard click on the wrench on device and in the window that pops up click on settings tab, top left it says :
Device #
<device_number> is the number you need.

-- set variable device to the dimmer device number you want to blink and the delay in seconds for how long you want the device to be turned on
local device  = 34
local adelay = 1

-- call function with delay to execute it, note there is no END as nothing started here it just triggers.
luup.call_delay( 'switch_on', 0)
luup.call_delay( 'switch_off', adelay)

-- the function itself doing the switchwork
function switch_on()
   luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget",{newTargetValue="1"},device)
end
function switch_off()
   luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget",{newTargetValue="0"},device)
end

click on SAVE LUA below the inputfield
click on CONFIRM CHANGES green button on right top
click on red SAVE button on right top
saving data press CONTINU

now u have a scene which you can put in a schedule to make it blink on certain time interval

for the Fibaro FGD211 v1.6 dimmer in combination with dummy load , dimmable led and a in-wall on/off switch i used this parameters:

VariablesSet 10,1d,255,13,1d,1,14,1d,1,16,1d,0,19,1d,1,20,1d,110,9,1d,255

to set these :

click wrench on fibaro dimmer device and go to device options
click on add configuration settings and type in the new field:
variable 10 , data size 1 byte dec, desired value 255
close the window and press on red SAVE button and continu
click wrench on Fibaro FGD211 v1.6 dimmer again and go to Advanced
now look for “VariablesSet” in the box next to it paste this

10,1d,255,13,1d,1,14,1d,1,16,1d,0,19,1d,1,20,1d,110,9,1d,255

close window and press red SAVE on right top
the variables set the dimmer to use a bi-stabile switch (on/off, mono-stabile is pulse) , dim leds of 0,5-25 watt (dummy load AND dimmable led light required), adds more stability by lowering and extending the dimming-range of the dimmer so the dimmable-led lights work better and fluctuate less.
Warning: Do not use this variableset for non-dimmable lights or for normal dimmable lights or for cfl or normal led lights.they might overload.

you could use the code also to:

  • trigger a mono-stabile switch
  • make a hourly chime using the sirene or a light or a doorbell/horn connected to a switch (end of workday horn for on work)
  • change the delay to 3 minutes and put a old radio set to the news broadcast on a switch for hourly news
  • send a pulse to a water pump so it gives just a little ammount of water for a second or longer if you change the delay

explanation:

luup.call_delay( ‘switch_off’, 1) < the 1 here is the delay in seconds

so 3 seconds would be: luup.call_delay( ‘switch_off’, 3)
5 seconds would be: luup.call_delay( ‘switch_off’, 5)

after that i put the delay in a variable: local delay =
making it : luup.call_delay(‘switch_off’, delay)
edit: something happens which keeps on rebooting vera because of failing lua… maybe the variable delay is used for something else… lets change it.

figuring out what gone wrong with the more complicated script, but documentation is lacking (beside that i wrote half of it i think, from what i know… not that much alas) . i take it the device and dimming value get mixed up .
also found out that if you have 1 scene with an lua error, the rest of the scenes aint working anymore … at least not with the device you are using in the faulty code.

[quote=“Da_JoJo, post:21, topic:167189”] for i, dimmin do luup.call_action("urn:upnp-org:serviceId:Dimming1", "SetLoadLevelTarget", {newLoadlevelTarget=i}, device) .. for i, dimmax, -1 dimmax do luup.call_action("urn:upnp-org:serviceId:Dimming1", "SetLoadLevelTarget", {newLoadlevelTarget=i}, device) [/quote]

This jumped out at me as a Lua syntax error. I think you want:

  for i = 1, dimmin do luup.call_action("urn:upnp-org:serviceId:Dimming1", "SetLoadLevelTarget", {newLoadlevelTarget=i}, device)
..
  for i = dimmax, 1, -1 do luup.call_action("urn:upnp-org:serviceId:Dimming1", "SetLoadLevelTarget", {newLoadlevelTarget=i}, device)

That’s at least valid Lua, though I may have changed the intent of your code.

thanx for looking at it :slight_smile:
i dont think u changed the intent. it was ment like this to ramp the dimmer up from 0 to 100.
but the thing is it changes the device and the dimming level so it get stuck. probably some end is missing to

Anyone tried this code on UI5? Trying to get some flashing light to get me out of bed.

I get nothing happening and no errors.

Pasted code into lua, changed device ID at top to the light. Nothing when scene is run.

Then tried editing the scene and turning the light I want to control on to see if that was needed, just turns light on no flashing.

Am I missing a step?

Aeotec Dimming module G2

I’m using the code from post #2 with UI5 and it has always worked just fine.

I wanted to update this thread since there didn’t seem to be any others that met my specific criteria - you can certainly use this for alarms or any purpose you need.
In my case I installed a door sensor in my mailbox so it would tell me when the mail had arrived. (I know - but it’s fun to tinker with!) My specific needs were to blink a light and then return that light to it’s original setting - this is for notification only.

I cleaned up the code a little from the early posts and made it a little easier to follow. I re-edited since I found a minor bug - If you have too many lights to blink, it fails because the previous z-wave command hasn’t completed before the next gets sent. I added a delay (adjustable - set to 1 second now) to wait between sending zwave commands. That seems to work much better in a busy zwave network.

Here’s my updated code for your use.

--Enter the device ID's here in the array with your device id's
-- 4 - Stair lights
-- 19 - kitchen pendant
-- 60 - bedroom lamp
-- 69 - LR lamp 3
local array_lights = {4,19,60,60}
local original_status={}
-- loopspeed is the timeout between sending commands
local loopspeedms = 1000   

-- counter is the number of times to blink
local counter = 5

-- delay is the number of seconds to wait between switching off-> on, and on->off
local delay   = 1



-- sleep() takes milliseconds not seconds - so we do the math here
local delayms = delay * 1000

function set_switch(device,value)
	luup.call_action("urn:upnp-org:serviceId:SwitchPower1","SetTarget",{ newTargetValue=value },device)
end


--  Turn all the switches in the array ON
function switch_on()
	for i, device in ipairs(array_lights) do
		set_switch(device,"1")
		luup.sleep(loopspeedms)
	end
	-- Run a delay and then call switch_Off which turns them all back off.
	-- luup.call_delay( 'switch_off', delay )
end


-- Turn off all the switches in the array
-- This is only a stub - does not call switch_on
function switch_off()
	for i, device in ipairs(array_lights) do
			set_switch(device,"0")
			luup.sleep(loopspeedms)
	end

end



-- Main program
-- Loop over the array "counter" times, with "delay" between loops.
function switch_run()
	

	while counter > 0 do
		counter = counter-1
		switch_on()
		luup.sleep(delayms)
		switch_off()
		luup.sleep(delayms)

	end



	--Set to original status
	for i, device in ipairs(array_lights) do
			set_switch(device,original_status[i])

	end

end

-- ------------
-- Program run starts here
-- ------------

--Save Original status
for i, device in ipairs(array_lights) do
		original_status[i] = luup.variable_get("urn:upnp-org:serviceId:SwitchPower1","Status", device)
end

switch_run()

Thanks Biggsworld for the code. I have it working and seems to run fine. I do have a question about it though.
Instead of giving it a number times to flash, Is there a way to make the code run until the scene is ran again?
Being a notification for ambulance, fire or police, I would like to make sure it stays on until they arrive, where I can turn it off.

This question has to do with implementation for the scene this code resides.,

I have this code working in a scene triggered by a virtual switch. If I trigger the virtual switch manually, it triggers the scene with this flashing code and my lights flash like they are suppose to.
Unfortunately when I trigger the virtual switch from another scene, it does not trigger the scene with this code. I can see the virtual switch change status, but the scene does not trigger.
Anyone know why this is?

[quote=“Jamr, post:27, topic:167189”]I have this code working in a scene triggered by a virtual switch. If I trigger the virtual switch manually, it triggers the scene with this flashing code and my lights flash like they are suppose to.
Unfortunately when I trigger the virtual switch from another scene, it does not trigger the scene with this code. I can see the virtual switch change status, but the scene does not trigger.
Anyone know why this is?[/quote]

Jamr, I am trying to do something very siimilar and am having the same problem. I know this thread is a bit old, but does anyone have any suggestions on how to make a blinking light scene run by turning on a virtual switch, running indefinitely until the switch is turned off, and then returning the lights to their previous state?

Instead of a counter, can I ‘run’ the function ‘while’ a virtual switch is on, like this:

[code]function switch_run()

while vswitch_status == "1" do
	switch_on()
	luup.sleep(delayms)
	switch_off()
	luup.sleep(delayms)
end

--Set to original status
for i, device in ipairs(array_lights) do
		set_switch(device,original_status[i])
end

end[/code]

You can’t use luup.sleep in a thread where the total wait time is more than a few seconds.

You have it in a loop …

Good way to destabilize Vera!!!

Thanks Richard, that’s good to know. Originally I was using the command:

luup.call_delay("switch_on", delay)

Is that more stable?

You need to make your loop event driven and use lua.delay for timing.

Sent from my SAMSUNG-SM-G935A using Tapatalk

Ok, I think I understand the the basic principle of what you’re suggesting, but not sure if I’m executing it properly. I made some changes and here is the code I put into a Scene’s LUA section which is triggered when the virtual switch is turned on. It seems to work as intended:

[code]local array_lights = {22} --using just one device to test for now
local original_status = {}
local delay = 3
local vswitch = 21 --device ID of virtual switch
local vswitch_status = luup.variable_get(“urn:upnp-org:serviceId:VSwitch1”,“Status”,vswitch)

if vswitch_status == “1” then
–save original status
for i, device in ipairs(array_lights) do
original_status[i] = luup.variable_get(“urn:upnp-org:serviceId:SwitchPower1”,“Status”, device)
end
luup.call_delay(“blink_on”, 0)
end

function blink_on()
for i, device in ipairs(array_lights) do
set_switch(device,“1”)
end
vswitch_status = luup.variable_get(“urn:upnp-org:serviceId:VSwitch1”,“Status”,vswitch)
if vswitch_status == “1” then
luup.call_delay(“blink_off”, delay)
else
luup.call_delay(“restore_status”, 0)
end
end

function blink_off()
for i, device in ipairs(array_lights) do
set_switch(device,“0”)
end
vswitch_status = luup.variable_get(“urn:upnp-org:serviceId:VSwitch1”,“Status”,vswitch)
if vswitch_status == “1” then
luup.call_delay(“blink_on”, delay)
else
luup.call_delay(“restore_status”, 0)
end
end

function set_switch(device,value)
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”,“SetTarget”,{ newTargetValue=value }, device)
end

function restore_status()
for i, device in ipairs(array_lights) do
set_switch(device,original_status[i])
end
end[/code]

Looks good.

[quote=“biggsworld, post:26, topic:167189”]…I cleaned up the code a little from the early posts and made it a little easier to follow. I re-edited since I found a minor bug - If you have too many lights to blink, it fails because the previous z-wave command hasn’t completed before the next gets sent. I added a delay (adjustable - set to 1 second now) to wait between sending zwave commands. That seems to work much better in a busy zwave network.

Here’s my updated code for your use.

[CODE]
–Enter the device ID’s here in the array with your device id’s
– 4 - Stair lights
– 19 - kitchen pendant
– 60 - bedroom lamp
– 69 - LR lamp 3
local array_lights = {4,19,60,60}
local original_status={}
– loopspeed is the timeout between sending commands
local loopspeedms = 1000

– counter is the number of times to blink
local counter = 5

– delay is the number of seconds to wait between switching off-> on, and on->off
local delay = 1

– sleep() takes milliseconds not seconds - so we do the math here
local delayms = delay * 1000

function set_switch(device,value)
luup.call_action(“urn:upnp-org:serviceId:SwitchPower1”,“SetTarget”,{ newTargetValue=value },device)
end

– Turn all the switches in the array ON
function switch_on()
for i, device in ipairs(array_lights) do
set_switch(device,“1”)
luup.sleep(loopspeedms)
end
– Run a delay and then call switch_Off which turns them all back off.
– luup.call_delay( ‘switch_off’, delay )
end

– Turn off all the switches in the array
– This is only a stub - does not call switch_on
function switch_off()
for i, device in ipairs(array_lights) do
set_switch(device,“0”)
luup.sleep(loopspeedms)
end

end

– Main program
– Loop over the array “counter” times, with “delay” between loops.
function switch_run()

while counter > 0 do
	counter = counter-1
	switch_on()
	luup.sleep(delayms)
	switch_off()
	luup.sleep(delayms)

end



--Set to original status
for i, device in ipairs(array_lights) do
		set_switch(device,original_status[i])

end

end


– Program run starts here


–Save Original status
for i, device in ipairs(array_lights) do
original_status[i] = luup.variable_get(“urn:upnp-org:serviceId:SwitchPower1”,“Status”, device)
end

switch_run()

[/CODE][/quote]

Im trying to implement this for my door bell.

When saving this code it only gives me: ERROR : Error in lua for scenes and events

(Using VeraPlus, UI7)

Found the problem!

There were some bad characters :wink:

the code works perfectly

But trying to make my dimmer go back to it’s original level

I know i have to change

–Save Original status
for i, device in ipairs(array_lights) do
original_status[i] = luup.variable_get(“urn:upnp-org:serviceId:SwitchPower1”,“Status”, device)
end

switch_run()

but what needs to be changed?

I tried to change it to

for i, device in ipairs(array_lights) do
original_status[i] = luup.variable_get(“urn:upnp-org:serviceId:Dimming1”,“Target”, device)
end

switch_run()

but all it does it turn it off