How to improve my curent LUA script?

Hi All,

I currently have 3 scenes to achieve what I want. Each scene has LUA code to trigger the desired action. I have used three scenes as I do not know how to do what I want in one LUA but I am sure it is possible and having one scene with a well written LUA script I know will make more sense and be more efficient all round.

An example of what I want to achieve in simple terms:

Check status of virtual switch ?A? If Virtual switch ?A? is on then do something End Else Go to ?next step? ?next step? Check status of virtual switch ?B? If Virtual switch ?B? is on then do something End Else Go to ?next step 2? ?next step 2? Check status of virtual switch ?C? If Virtual switch ?C? is on then do something End

The current set up has each else / if in an individual scene such as:

Check status of virtual switch ?A? If Virtual switch ?A? is on then do something Else Run Scene number?.

I believe the main issue I am encountering is the script keeps running through and won?t jump to the place I need it to.

I think for someone in the know this will be obvious but would be really helpful for some pointers if possible.

If I can address the question above I can post the code for scrutiny! 

Thank you.

[quote=“LightsOn, post:1, topic:178004”]Hi All,

I currently have 3 scenes to achieve what I want. Each scene has LUA code to trigger the desired action. I have used three scenes as I do not know how to do what I want in one LUA but I am sure it is possible and having one scene with a well written LUA script I know will make more sense and be more efficient all round.

An example of what I want to achieve in simple terms:

Check status of virtual switch ?A? If Virtual switch ?A? is on then do something End Else Go to ?next step? ?next step? Check status of virtual switch ?B? If Virtual switch ?B? is on then do something End Else Go to ?next step 2? ?next step 2? Check status of virtual switch ?C? If Virtual switch ?C? is on then do something End

The current set up has each else / if in an individual scene such as:

Check status of virtual switch ?A? If Virtual switch ?A? is on then do something Else Run Scene number?.

I believe the main issue I am encountering is the script keeps running through and won?t jump to the place I need it to.

I think for someone in the know this will be obvious but would be really helpful for some pointers if possible.

If I can address the question above I can post the code for scrutiny! 

Thank you.[/quote]

is ‘do something’ always the same action? it would be helpful if you post all your actual code to make things more clear…

I never used the command “goto”, I always wrap everything in functions and call them as I need. But I think it would be smart to post the complete code…

Hi there,

Thank you - was not sure initialy if it would be a help or hinderance before clarifying what was needed to be achived. below is the code - the code is in three parts as above. I want to be abel to merge into one.

[code]Scene 1 of 3

status = luup.variable_get(“urn:upnp-org:serviceId:VSwitch1”,“Status”, 66)
if(status==“1”)then

luup.call_action(“urn:micasaverde-com:serviceId:HomeAutomationGateway1”, “RunScene”, {SceneNum = “180”}, 0) ---- scene 2 of 3

else

luup.call_action(“urn:micasaverde-com:serviceId:HomeAutomationGateway1”, “RunScene”, {SceneNum = “23”}, 0) ---- scene 3 of 3

end

Scene 2 of 3

status = luup.variable_get(“urn:upnp-org:serviceId:VSwitch1”,“Status”, 886)
if(status==“1”)then

luup.call_action(“urn:micasaverde-com:serviceId:HomeAutomationGateway1”, “RunScene”, {SceneNum = “23”}, 0) ---- scene 3 of 3

else

luup.call_action(“urn:futzle-com:serviceId:CountdownTimer1”, “RestartTimer”, {}, 871)

end

Scene 3 of 3

local delay = 1
local lightlevel = luup.variable_get(“urn:micasaverde-com:serviceId:LightSensor1”, “CurrentLevel”, 868)

if (lightlevel < “55”) then
luup.call_action(“urn:futzle-com:serviceId:CountdownTimer1”, “CancelTimer”, {}, 52)
luup.call_action(“urn:futzle-com:serviceId:CountdownTimer1”, “CancelTimer”, {}, 869)
luup.call_delay( ‘Restart_Timer’, delay )

function Restart_Timer ()
luup.call_action(“urn:futzle-com:serviceId:CountdownTimer1”, “RestartTimer”, {}, 52)
luup.call_action(“urn:futzle-com:serviceId:CountdownTimer1”, “RestartTimer”, {}, 869)
end

else

luup.call_action(“urn:futzle-com:serviceId:CountdownTimer1”, “CancelTimer”, {}, 869)
luup.call_delay( ‘Restart_TimerSpeakerLight’, delay )
end

function Restart_TimerSpeakerLight ()
luup.call_action(“urn:futzle-com:serviceId:CountdownTimer1”, “RestartTimer”, {}, 869)
end[/code]

Hi chixxi, could you Give me an example of a ‘goto’ call or ‘function call’? Have done some looking around and don’t think ‘goto’ exists in luup?

Sent from my HTC HD2 using Tapatalk 2

Had a play but no luck and also no one seems to use goto?

[code]status = luup.variable_get(“urn:upnp-org:serviceId:VSwitch1”,“Status”, 907)
if(status==“1”)then
goto b
else
goto a
end

::a::
luup.call_action(“urn:micasaverde-com:serviceId:HomeAutomationGateway1”, “RunScene”, {SceneNum = “8”}, 0)
goto c
end]

::b::
luup.call_action(“urn:micasaverde-com:serviceId:HomeAutomationGateway1”, “RunScene”, {SceneNum = “9”}, 0)
goto c
end

::c::
end[/code]

[quote=“LightsOn, post:5, topic:178004”]Hi chixxi, could you Give me an example of a ‘goto’ call or ‘function call’? Have done some looking around and don’t think ‘goto’ exists in luup?

Sent from my HTC HD2 using Tapatalk 2[/quote]

check this wiki page for some good examples, including functions and how to call them.

Lua doesn’t have a goto statement. Even if it did, your code would effectively be:

status = luup.variable_get("urn:upnp-org:serviceId:VSwitch1","Status", 907) if(status=="1")then luup.call_action("urn:micasaverde-com:serviceId:HomeAutomationGateway1", "RunScene", {SceneNum = "9"}, 0) else luup.call_action("urn:micasaverde-com:serviceId:HomeAutomationGateway1", "RunScene", {SceneNum = "8"}, 0) end

Most languages and programming practices have dropped goto in favour of more structured methods. The result is usually more readable and less prone to getting wrecked by future modifications.

What is it that you want to achieve?

In an earlier post, you wanted to merge three pieces of scene Luup into one. Try:

[code]local status66 = luup.variable_get(“urn:upnp-org:serviceId:VSwitch1”,“Status”, 66)
if(status66==“1”)then
local status886 = luup.variable_get(“urn:upnp-org:serviceId:VSwitch1”,“Status”, 886)
if(status886==“1”)then
doStuff()
else
luup.call_action(“urn:futzle-com:serviceId:CountdownTimer1”, “RestartTimer”, {}, 871)
end
else
doStuff()
end

function doStuff()
local delay = 1
local lightlevel = luup.variable_get(“urn:micasaverde-com:serviceId:LightSensor1”, “CurrentLevel”, 868)
if (lightlevel < “55”) then
luup.call_action(“urn:futzle-com:serviceId:CountdownTimer1”, “CancelTimer”, {}, 52)
luup.call_action(“urn:futzle-com:serviceId:CountdownTimer1”, “CancelTimer”, {}, 869)
luup.call_delay( ‘Restart_Timer’, delay )
else
luup.call_action(“urn:futzle-com:serviceId:CountdownTimer1”, “CancelTimer”, {}, 869)
luup.call_delay( ‘Restart_TimerSpeakerLight’, delay )
end
end

function Restart_Timer ()
luup.call_action(“urn:futzle-com:serviceId:CountdownTimer1”, “RestartTimer”, {}, 52)
luup.call_action(“urn:futzle-com:serviceId:CountdownTimer1”, “RestartTimer”, {}, 869)
end

function Restart_TimerSpeakerLight ()
luup.call_action(“urn:futzle-com:serviceId:CountdownTimer1”, “RestartTimer”, {}, 869)
end[/code]

Hi @RexBeckett

Thank you for looking at this for me.

Yes I agree with you that your solution of if / else would work; my example was only to test the ?goto? option but as you state it does not seem to available, I though it may be as a result of the reference found in a link in the info viewer: lua-users wiki: Goto Statement but as you say in most code I am aware goto is no longer in use.

I shall review your code later today to see if I can work it into the need I have as your example is exactly what I was hoping for to get me started ? once I have had a play with it I will post it here ? would be grateful for your input if you have a moment once I have put something together.

Thanks again for helping me out.

Thank you.

-lightsOn

Just to update this thread.

I played around with the code kindly suggested by @RexBeckett - thank you again for your time providing the example. and I got good results but later had some spare time and got my head round PLEG. I have since begun a process of removing code in scenes and swapping it out for PLXX. I have seen great improvements in speeds which I shall update in my other thread regarding tuning of my system however most notable I have ben able to streamline and improve my “logic” in a far more suitable way and far far faster as well - it was a bit of a learning curve (still is as not quite finished) but it has been a great improvement; the ability to speed up “logic” whilst also reducing ram usage through needing less Plugins (in my case timers and virtual switches mainly) has been an asset.

If you are using lots of code still and are reading this I really recommend taking the time to read up on PLXX and having a go - it is so very valuable to the wider system and has great documentation etc. I cant deny understanding code as a preface has been of great use and benefit but for moving forward…I shall not be looking back.

Many thanks all.