is it possible to create a local variable out of a parameter

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

Moderator: leecollings

Post Reply
Gravityz
Posts: 652
Joined: Wednesday 16 December 2015 19:13
Target OS: NAS (Synology & others)
Domoticz version: 2025.1
Location: Netherlands
Contact:

is it possible to create a local variable out of a parameter

Post by Gravityz »

maybe i try to solve something the wrong way but here is my question

if i call a global helper function like this
domoticz.helpers.test(domoticz,'switch A','On','Variable-switch-A')

how can i crete a local variable in that helper function that uses one of the passed information as the name of the local variable

eg

helpers = {
test = function(domoticz, Device, command, variable)

local $variable= "On"
end

so local definition needs to be
local Variable-switch-A="On"
so how can i create a local variable by using the name of a parameter
User avatar
boum
Posts: 136
Joined: Friday 18 January 2019 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: France
Contact:

Re: is it possible to create a local variable out of a parameter

Post by boum »

It would help to know what you want to do with this variable. You cannot do that directly.

The easiest way to do something like that would be to use a table (local) instead of just a local variable.

Code: Select all

test = function(domoticz, Device, command, variable)
  local values = { 
    Variable-switch-A = 'Off', 
    Variable-switch-B = 'Off', 
  }
  values[variable] = command
  -- and you can access it later with by iterating on the full table: for key,value in pairs(values) do ... end 
  -- or access each value by doing
  if values.Variable-switch-A == 'On' then ... end
end
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: is it possible to create a local variable out of a parameter

Post by waaren »

Also possible without knowing the list of possible variables in advance.

Code: Select all

test = function(domoticz, Device, command, variable)
    local values = { [variable] = command } 
    
    if values[variable] == 'On' then 
        domoticz.log('On: content of values[' .. variable .. '] is ' .. tostring(values[variable]) , domoticz.LOG_FORCE)
    else
        domoticz.log('not On: content of values[' .. variable .. '] is ' .. tostring(values[variable]) , domoticz.LOG_FORCE)
    end
end 
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Gravityz
Posts: 652
Joined: Wednesday 16 December 2015 19:13
Target OS: NAS (Synology & others)
Domoticz version: 2025.1
Location: Netherlands
Contact:

Re: is it possible to create a local variable out of a parameter

Post by Gravityz »

ok thanks, both might be good solutions.

the reason i wanted this is i wanted to save the last state of a light and in case it switches on again(while it is still on) it is ignored.

one helper for all switches would be nice but then again i do not have that many switches so defining them upfront(example boum) would do the trick.

waarens example looks like exactly what i was looking for.

i know it is killing a mosquito with a tank(solving a jitter problem) but clean code is always nicer to look at :-)

if this happens a lot i would have asked dannybloe to think about an .nojitter option in dzvents but this is killing the symptoms instead of killing the problem.

but still would look really nice if you could do domoticz.devices('test').switchon().nojitter

my qubino switch used to have that jitter problem as well but it seems to be gone

thanks guys
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: is it possible to create a local variable out of a parameter

Post by waaren »

Gravityz wrote: Wednesday 25 September 2019 7:51 the reason i wanted this is i wanted to save the last state of a light and in case it switches on again(while it is still on) it is ignored.
This would require the use of (global) persistent storage
but still would look really nice if you could do domoticz.devices('test').switchon().nojitter
If only used to prevent an unneeded switch then consider the use of checkFirst():

checkFirst(): Function. Checks if the current state of the device is different than the desired new state. If the target state is the same, no command is sent. If you do mySwitch.switchOn().checkFirst(), then no switch command is sent if the switch is already on. This command only works with switch-like devices.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Gravityz
Posts: 652
Joined: Wednesday 16 December 2015 19:13
Target OS: NAS (Synology & others)
Domoticz version: 2025.1
Location: Netherlands
Contact:

Re: is it possible to create a local variable out of a parameter

Post by Gravityz »

i looked at checkFirst some time ago for something else.

will test if this works because i do not know if the switchon will go fast enough to register as ON before the next ON command fires.
i believe Zwave does an acknowlegde before it registers as ON
let you know, this would be indeed the easiest way
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest