Alternative for goto in lua?

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
tlpeter
Posts: 191
Joined: Wednesday 26 November 2014 18:43
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Netherlands
Contact:

Alternative for goto in lua?

Post by tlpeter »

I have a lua script that i want to convert but this script contains goto. Very simple like if device = 0 then goto :name:
This means that the rest in the script between this line and :name: a bit further in the script is skipped.
I know that my script is not very good and i could make it work like that so i wonder if this is also possible within dzVents.
freijn
Posts: 536
Joined: Friday 23 December 2016 16:40
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: Netherlands Purmerend
Contact:

Re: Alternative for goto in lua?

Post by freijn »

please past your script here, much easier to advise on :-)
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Alternative for goto in lua?

Post by dannybloe »

Euww. You should never have a need for a goto. There's never an excuse either.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
jannl
Posts: 625
Joined: Thursday 02 October 2014 6:36
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.2
Location: Geleen
Contact:

Re: Alternative for goto in lua?

Post by jannl »

Do not agree. In some cases a goto takes care of much easier to read code instead of a big if statement or so.

Besides that things like break in a case are basically goto's as well.

Verstuurd vanaf mijn SM-G930F met Tapatalk

dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Alternative for goto in lua?

Post by dannybloe »

The breaks are part of the construct but I don't like them either. But I'm not gonna argue on this one. Gotos are bad and should be avoided and fortunately almost all modern languages don't have them.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
tlpeter
Posts: 191
Joined: Wednesday 26 November 2014 18:43
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Alternative for goto in lua?

Post by tlpeter »

I understand the discussion but all i did was ask if it is possible within dzVents :mrgreen:
I am used to program PBX's and some do have a visual IVR (GUI) and goto's are very common.
You cannot always program everything in the best way, sometimes quick and dirty is well enough.
I do not mind to discus about'it, dont get me wrong.
mivo
Posts: 80
Joined: Friday 21 April 2017 8:58
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Czechia
Contact:

Re: Alternative for goto in lua?

Post by mivo »

Hi,

please paste example script here. There is probably GOTO statement in LUA http://lua-users.org/wiki/GotoStatement, but in structured programming languages, it is usually not used, or not available at all. My last use of GOTO statement was in Basic programming some tenths years ago :D
My toys:
Raspberry Pi 3 + UPS PIco HV3.0 A Stack
Minibian (Raspbian Jessie) + Domoticz beta
RFLink 433 Gateway, 1wire DS18B20 temp sensors (GPIO)
RaZberry module + 2x Comet Z-Wave + Z-wave socket
---
Plugins: WeMo Switch, UPS PIco HV3.0A on GitHub
tlpeter
Posts: 191
Joined: Wednesday 26 November 2014 18:43
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Alternative for goto in lua?

Post by tlpeter »

Ok finaaly got some time.
I have this script now and i want to add two switches which will override the humidity part.
When i use a goto then i just skip the humidity part if configure above it.

Code: Select all

return {
    active = true,
    on = {
        timer = {'every minute'}
    },
   
    execute = function(domoticz)
        local bathroomSensor = domoticz.devices('Badkamer')
        
         if bathroomSensor.humidity >= 10 and bathroomSensor.humidity <= 75 then
        domoticz.devices('Mechanische ventilatie').switchSelector(10)
    elseif bathroomSensor.humidity >= 76 and bathroomSensor.humidity <= 85 then
        domoticz.devices('Mechanische ventilatie').switchSelector(20)
    elseif bathroomSensor.humidity >=86 and bathroomSensor.humidity <= 100 then
        domoticz.devices('Mechanische ventilatie').switchSelector(30)
        end
        
        end
}
mivo
Posts: 80
Joined: Friday 21 April 2017 8:58
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Czechia
Contact:

Re: Alternative for goto in lua?

Post by mivo »

Hi,

In this case, I will write script like this:

Code: Select all

local LOGGING = true

return {
    active = true,
    on = {
        timer = {'every minute'}
    },

    execute = function(domoticz)
        local bathroomSensor = domoticz.devices('Badkamer')
        -- store override devices to variables - for better readability
        local override1 = domoticz.devices('switch').state
        local override2 = domoticz.devices('switch2').state
        -- ventilator device variable - for better readability and easier change device name in one place etc.
        local ventilator = domoticz.devices('Mechanische ventilatie')

        if override1 == 'On' or override2 == 'On' then
                -- for debugging purpose, log also if overriden
                if LOGGING then
                        domoticz.log('Override active')
                end
        else
                -- if not override, do something
                if bathroomSensor.humidity >= 10 and bathroomSensor.humidity <= 75 then
                        ventilator.switchSelector(10)
                elseif bathroomSensor.humidity >= 76 and bathroomSensor.humidity <= 85 then
                        ventilator.switchSelector(20)
                elseif bathroomSensor.humidity >=86 and bathroomSensor.humidity <= 100 then
                        ventilator.switchSelector(30)
                end
        end
   end
}
Indenting code by space or tab maintains better readability in nested control structures like functions, if..then..else..end conditions etc.

It is also good idea, in case of time script, to not update ventilator device every run (sure, if not overriden) to same value. Conditional update - like compare ventilator current settings with new value is better:

Code: Select all

local LOGGING = true

return {
    active = true,
    on = {
        timer = {'every minute'}
    },

    execute = function(domoticz)
        local bathroomSensor = domoticz.devices('Badkamer')
        -- store override devices to variables - for better readability
        local override1 = domoticz.devices('switch').state
        local override2 = domoticz.devices('switch2').state
        -- ventilator device variable - for better readability and easier change device name in one place etc.
        local ventilator = domoticz.devices('Mechanische ventilatie')

        if override1 == 'On' or override2 == 'On' then
                -- for debugging purpose, log also if overriden
                if LOGGING then
                        domoticz.log('Override active')
                end
        else
                -- if not override, do something
                local ventCurrent = ventilator.level -- current level
                local ventNew = 0 -- variable for new level

                if bathroomSensor.humidity >= 10 and bathroomSensor.humidity <= 75 then
                        ventNew = 10
                elseif bathroomSensor.humidity >= 76 and bathroomSensor.humidity <= 85 then
                        ventNew = 20
                elseif bathroomSensor.humidity >=86 and bathroomSensor.humidity <= 100 then
                        ventNew = 30
                end

                if ventCurrent ~= ventNew then
                -- compare current level with new, and then switch or not
                        domoticz.log('Changing level from: ' .. tostring(ventCurrent) .. ' to: ' .. tostring(ventNew))
                        ventilator.switchSelector(ventNew)
                else
                        -- not switching
                        if LOGGING then
                                domoticz.log('Level is same')
                        end
                end
        end
   end
}
My toys:
Raspberry Pi 3 + UPS PIco HV3.0 A Stack
Minibian (Raspbian Jessie) + Domoticz beta
RFLink 433 Gateway, 1wire DS18B20 temp sensors (GPIO)
RaZberry module + 2x Comet Z-Wave + Z-wave socket
---
Plugins: WeMo Switch, UPS PIco HV3.0A on GitHub
tlpeter
Posts: 191
Joined: Wednesday 26 November 2014 18:43
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Alternative for goto in lua?

Post by tlpeter »

Ok, i had a go with this script.
The humidity part works fine but the override part not.
What i did it that the two switches will set the switchlevel to 20 and 30 and when these are off it is set to switchlevel 10 just like when the humidity level is below 75.
I cannot get that working at all.
I tried creating a variable like the other part but i keep getting errors.

Can you help me out with this?
mivo
Posts: 80
Joined: Friday 21 April 2017 8:58
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Czechia
Contact:

Re: Alternative for goto in lua?

Post by mivo »

OK, post script here.
My toys:
Raspberry Pi 3 + UPS PIco HV3.0 A Stack
Minibian (Raspbian Jessie) + Domoticz beta
RFLink 433 Gateway, 1wire DS18B20 temp sensors (GPIO)
RaZberry module + 2x Comet Z-Wave + Z-wave socket
---
Plugins: WeMo Switch, UPS PIco HV3.0A on GitHub
tlpeter
Posts: 191
Joined: Wednesday 26 November 2014 18:43
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Alternative for goto in lua?

Post by tlpeter »

I used your script

Code: Select all

local LOGGING = true

return {
    active = true,
    on = {
        timer = {'every minute'}
    },

    execute = function(domoticz)
        local bathroomSensor = domoticz.devices('Badkamer')
        -- store override devices to variables - for better readability
        local override1 = domoticz.devices('switch').state
        local override2 = domoticz.devices('switch2').state
        -- ventilator device variable - for better readability and easier change device name in one place etc.
        local ventilator = domoticz.devices('Mechanische ventilatie')

        if override1 == 'On' or override2 == 'On' then
                -- for debugging purpose, log also if overriden
                if LOGGING then
                        domoticz.log('Override active')
                end
        else
                -- if not override, do something
                local ventCurrent = ventilator.level -- current level
                local ventNew = 0 -- variable for new level

                if bathroomSensor.humidity >= 10 and bathroomSensor.humidity <= 75 then
                        ventNew = 10
                elseif bathroomSensor.humidity >= 76 and bathroomSensor.humidity <= 85 then
                        ventNew = 20
                elseif bathroomSensor.humidity >=86 and bathroomSensor.humidity <= 100 then
                        ventNew = 30
                end

                if ventCurrent ~= ventNew then
                -- compare current level with new, and then switch or not
                        domoticz.log('Changing level from: ' .. tostring(ventCurrent) .. ' to: ' .. tostring(ventNew))
                        ventilator.switchSelector(ventNew)
                else
                        -- not switching
                        if LOGGING then
                                domoticz.log('Level is same')
                        end
                end
        end
   end
}
Override 1 and 2 need to set switchselector(20) and switchselector(30)
Somehow it keeps saying level is same and keeps switching every minute.
mivo
Posts: 80
Joined: Friday 21 April 2017 8:58
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Czechia
Contact:

Re: Alternative for goto in lua?

Post by mivo »

Aah, just for sure if I understand correctly:

- override switches are 2 simple On / Off switches ?
- override should happen when any of switches is On ?
- one override switch should set 'Mechanische ventilatie' selector switch manually to 20, and second manually to 30 ?
- if both override switches are off set 'Mechanische ventilatie' selector switch manually to 10
- automatic control by humidity sensor 'Badkamer' should be done at which situation ?
My toys:
Raspberry Pi 3 + UPS PIco HV3.0 A Stack
Minibian (Raspbian Jessie) + Domoticz beta
RFLink 433 Gateway, 1wire DS18B20 temp sensors (GPIO)
RaZberry module + 2x Comet Z-Wave + Z-wave socket
---
Plugins: WeMo Switch, UPS PIco HV3.0A on GitHub
tlpeter
Posts: 191
Joined: Wednesday 26 November 2014 18:43
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Alternative for goto in lua?

Post by tlpeter »

Aah, just for sure if I understand correctly:

- override switches are 2 simple On / Off switches ? Yes they are
- override should happen when any of switches is On ? Yes
- one override switch should set 'Mechanische ventilatie' selector switch manually to 20, and second manually to 30 ? Yes
- if both override switches are off set 'Mechanische ventilatie' selector switch manually to 10 Yes as that is the default setting
- automatic control by humidity sensor 'Badkamer' should be done at which situation ? This should happen when both switches are off

So the last two questions are like this:

Default 'Mechanische ventilatie' is running on level 10 and is controlled by humidity as you know.
When one of the two switches is used then it must be set to level 20 or level 30 and not controlled by humidity anymore.
If those two switches are off then it wil return to humidity control.
mivo
Posts: 80
Joined: Friday 21 April 2017 8:58
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Czechia
Contact:

Re: Alternative for goto in lua?

Post by mivo »

New script for you ;)

Code: Select all

local LOGGING = true

return {
    active = true,
    logging = {
        level = domoticz.LOG_INFO,
    },
    on = {
        timer = {'every minute'}
    },

    execute = function(domoticz)
        local bathroomSensor = domoticz.devices('Badkamer')
        -- store override devices to variables - for better readability
        local override1 = domoticz.devices('switch').state
        local override2 = domoticz.devices('switch2').state
        -- ventilator device variable - for better readability and easier change device name in one place etc.
        local ventilator = domoticz.devices('Mechanische ventilatie')
        local ventNew = 10 -- variable for new ventilator level, default is 10
        local override = false -- helper variable for logging - set to true later if override active
        local ventCurrent = ventilator.level -- current level

        if override2 == 'On' then
                -- first check override2 - if both switches On, switch2 has higher priority -> higher ventilator
                override = true
                ventNew = 30 -- override to 30
        elseif override1 == 'On' then
                -- status override1 switch
                override = true
                ventNew = 20 -- override to 20
        else
        -- if not override, check humidity

                -- dotn check range 10-75, level defaults to -> 10
                -- if bathroomSensor.humidity >= 10 and bathroomSensor.humidity <= 75 then
                -- ventNew = 10
                if bathroomSensor.humidity >= 76 and bathroomSensor.humidity <= 85 then
                        ventNew = 20
                elseif bathroomSensor.humidity >=86 and bathroomSensor.humidity <= 100 then
                        ventNew = 30
                end
        end

        if LOGGING then
                if override then
                        domoticz.log('Override active to ' .. tostring(ventNew))
                end
        end

        if ventCurrent ~= ventNew then
        -- compare current level with new, and then switch or not
                domoticz.log('Changing level from: ' .. tostring(ventCurrent) .. ' to: ' .. tostring(ventNew))
                ventilator.switchSelector(ventNew)
        else
        -- not switching
                if LOGGING then
                        domoticz.log('Level is same')
                end
        end
   end
}
One more idea - there is a option to change script from time based (every min) to device based (change of switches or humidity) - both has pros and cons
My toys:
Raspberry Pi 3 + UPS PIco HV3.0 A Stack
Minibian (Raspbian Jessie) + Domoticz beta
RFLink 433 Gateway, 1wire DS18B20 temp sensors (GPIO)
RaZberry module + 2x Comet Z-Wave + Z-wave socket
---
Plugins: WeMo Switch, UPS PIco HV3.0A on GitHub
tlpeter
Posts: 191
Joined: Wednesday 26 November 2014 18:43
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Alternative for goto in lua?

Post by tlpeter »

:mrgreen: This is it!
Thanks very much. I am going to have a good look on this script as what i see is logical and i can read it but somehow i am not capable enough at the moment to come up with this by my self yet.
But i think i just need some time to get used to dzVents.
Thanks again, you are a hero.
mivo
Posts: 80
Joined: Friday 21 April 2017 8:58
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Czechia
Contact:

Re: Alternative for goto in lua?

Post by mivo »

You are welcome ;)
My toys:
Raspberry Pi 3 + UPS PIco HV3.0 A Stack
Minibian (Raspbian Jessie) + Domoticz beta
RFLink 433 Gateway, 1wire DS18B20 temp sensors (GPIO)
RaZberry module + 2x Comet Z-Wave + Z-wave socket
---
Plugins: WeMo Switch, UPS PIco HV3.0A on GitHub
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest