string and number  [Solved]

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

Moderator: leecollings

Post Reply
pvklink
Posts: 822
Joined: Wednesday 12 November 2014 15:01
Target OS: Raspberry Pi / ODroid
Domoticz version: latest b
Contact:

string and number

Post 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)

Raspberry (raspbian on rpi 3) , Domoticz Beta, dzVents , RFXtrx433e, P1, Hue, Yeelight, Zwave+, X10, ESP(easy), MQTT,Weather Underground, System Alive Checker, Domoticz Remote Server to RPI with Google Assistant,
Jablotron connection, Ikea
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: string and number  [Solved]

Post 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
	
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
pvklink
Posts: 822
Joined: Wednesday 12 November 2014 15:01
Target OS: Raspberry Pi / ODroid
Domoticz version: latest b
Contact:

Re: string and number

Post 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!
Raspberry (raspbian on rpi 3) , Domoticz Beta, dzVents , RFXtrx433e, P1, Hue, Yeelight, Zwave+, X10, ESP(easy), MQTT,Weather Underground, System Alive Checker, Domoticz Remote Server to RPI with Google Assistant,
Jablotron connection, Ikea
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: string and number

Post 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.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
pvklink
Posts: 822
Joined: Wednesday 12 November 2014 15:01
Target OS: Raspberry Pi / ODroid
Domoticz version: latest b
Contact:

Re: string and number

Post by pvklink »

Clear!
Raspberry (raspbian on rpi 3) , Domoticz Beta, dzVents , RFXtrx433e, P1, Hue, Yeelight, Zwave+, X10, ESP(easy), MQTT,Weather Underground, System Alive Checker, Domoticz Remote Server to RPI with Google Assistant,
Jablotron connection, Ikea
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest