Page 1 of 1

string and number

Posted: Monday 15 March 2021 12:06
by pvklink
I have a script that retrieves config_data for setting the config a siren or doorbell based on inputs from a slector switch like 'medium', 'hard'
i like to change this script so that the input data can be a selected dim level and then do the same trick by retrieving the data.
In this case there has to be a range check for the corresponding data.
Another issue is that the script must handle strings and numbers and detect how to handle the input variable, string (is value matching), number is range matching.

I made a first test script, and like to have feedback if this is the way to go...

Code: Select all

local volume_states =   { 
                            {'uit','Low',0,'Off',0,0,0},           -- p1 = waarde tekst, p2 bel/sirene volume, p3=nodered volume, p4=volumeduur_alarm Off=uit, p5=volumeduur_deurbel 0= uit, p6= range lowondergrens, p7 range volume bovengrens
                            {'fluister', 'Low',10,'30 second',1,1,20}, 
                            {'zacht','Middle',30,'30 second',1,21,40}, 
                            {'medium','Middle',50,'30 second',1, 41,60}, 
                            {'hard','High',70,'30 second',1,61,80}, 
                            {'extra', 'High', 90,'30 second',1,81,100}
                            }

local function xxx(aa)       
        for _, record in ipairs(volume_states) do
	if isInteger(aa) then
	            if volume >= record[6] and volume <= record[7] then 			-- figure out aa in which range it is and then use those values
	                	return record[1],record[2],record[3],record[4],record[5]
	else
	            if record[1] == volume then
                	return record[1],record[2],record[3],record[4],record[5]
	            end
	end
        end
end

a=10    --of
a="10"  --of 
a="medium" 

if isInteger(a) then 
   aa= tonumber(a)
else
   aa=a
end

test1,test2,test3,test4,test5 = XXX(aa)


Re: string and number  [Solved]

Posted: Monday 15 March 2021 14:41
by waaren
pvklink wrote: Monday 15 March 2021 12:06 I have a script that retrieves config_data for setting the config a siren or doorbell based on inputs from a slector switch like 'medium', 'hard'
i like to change this script so that the input data can be a selected dim level and then do the same trick by retrieving the data.
In this case there has to be a range check for the corresponding data.
Another issue is that the script must handle strings and numbers and detect how to handle the input variable, string (is value matching), number is range matching.
If you want to test Lua code you can use a standalone Lua interpreter
on your RPI you can

Code: Select all

sudo apt install Lua5.4 
and execute with Lua5.4

on your desktop/laptop there are many options. I use the Lua plugin in notepad++

The logic of your code should work but isInteger is not a standard function.
You will see how you can check if a value can be converted to a number if you do

Code: Select all

 
 
list = { 10, '10' , 'Medium' }

for _, var in ipairs(list) do
  	print(tonumber(var))
	print(type(var))
	if tonumber(var) then 
		print(var .. ',  (a ' .. type(var) .. '), is or can be converted to a number')
	else
		print(tostring(var) .. ',  (a ' .. type(var) .. '), cannot be converted to a number')
	end
end
	

Re: string and number

Posted: Monday 15 March 2021 15:47
by pvklink
Thanks!

I will study your script and gonna play with the interpreters on rpi and intel pc :-)
i thought (shame) that dzvents is an addon/ extention to LUA for objects for domoticz.
And that large parts of lua are 1:1 the same as dzvents....

again thanks!

Re: string and number

Posted: Monday 15 March 2021 16:05
by waaren
pvklink wrote: Monday 15 March 2021 15:47 i thought (shame) that dzvents is an addon/ extention to LUA for objects for domoticz.
dzVents is a framework for the use of Lua in combination with domoticz.
The dzVents scripts users create are 100% Lua code but some of the methods / functions available do not make sense outside the domoticz context.

Re: string and number

Posted: Monday 15 March 2021 17:52
by pvklink
Clear!