How to use functions in Vera

I would like to implement blinking lights.
This thread includes some code by Rigpapa that makes sense. what I don’t know is how to call the function from a Vera scene and have not found any threads or web pages.

I put the blink functions (on, off, start, stop) in into the start-up Lua and rebooted. I put ‘function StartBlinking()’ and ‘StartBlinking()’ into a scene lua of a scene that is manually triggered.

Does anyone know of a tutorial on how to call a function defined in the start-up Lua when a scene is triggered?

Functions declared in Startup Lua are “in scope” for all scene Lua. That is, if you define a function or variable in Startup Lua without the local qualification, it will be visible in all your scene Lua.

So put this in your Startup Lua:

BlinkRun = 0 -- initialize

function BlinkOn()
    luup.call_action('urn:upnp-org:serviceId:SwitchPower1','SetTarget',{newTargetValue="1"},Device)
    luup.call_timer( "BlinkOff", 1, 1 )
end

function BlinkOff()
    luup.call_action('urn:upnp-org:serviceId:SwitchPower1','SetTarget',{newTargetValue="0"},Device)
    if BlinkRun == 1 then
        luup.call_timer( "BlinkOn", 1, 1 )
    end
end

function StartBlinking()
    if BlinkRun == 0 then
        BlinkRun =  1
        BlinkOn()
    end
end

function StopBlinking()
    BlinkRun = 0
end

and put this into Scene Lua to start it blinking…

StartBlinking() -- notice I have not used "function" here

and put this into a scene to stop blinking…

StopBlinking() -- again, no "function" keyword before

We don’t use the “function” keyword in the scene Lua because we are calling the function. If we include the “function” keyword before, Lua thinks we’re declaring the function (which we’ve already done and certainly don’t wish to repeat).

1 Like

@rigpapa Thanks again!!
what you are recommending is what I put in earlier today. (to clarify my original post, I tried both with and without ‘function’). When I tested the StartBlinking() in the Test Luup code (Lua) utility, it failed. And the scene that called that function failed also.
Several hours later, at the time of this posting, I try again and it all works, though one time trying just now the scene did fail. I ran it again and it worked.

Maybe I need to be more patient with the system update, though I did reboot after adding the start-up Lua.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.

Best Home Automation shopping experience. Shop at getvera!

© 2020 Vera Control Ltd., All Rights Reserved. Terms of Use | Privacy Policy | Forum Rules