Integer/number user variables are not passed as strings in LUA Topic is solved

Moderator: leecollings

Post Reply
User avatar
boum
Posts: 135
Joined: Friday 18 January 2019 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: France
Contact:

Integer/number user variables are not passed as strings in LUA

Post by boum »

Hi there,

Is there an issue with the documentation there? https://www.domoticz.com/wiki/User_variables#Lua
When using user variables in Lua, keep in mind the following: variables are always stored as a string in the Domoticz database, regardless of their type. The variables are passed in arrays to Lua, at that point they will always be strings also.
I ran a LUA time script, default one and added in the user var loop:

Code: Select all

for variableName,variableValue in pairs(uservariables) do
    print( 'Variable: '.. variableName .. ' = "' .. variableValue .. '" (' .. type(variableValue) .. ')')
end
and I got:
2019-09-16 10:15:00.496 Status: LUA: Time based event fired
2019-09-16 10:15:00.497 Status: LUA: Variable: TestVar = "0" (number)
2019-09-16 10:15:00.497 Status: LUA: Variable: TempMax = "25.5" (number)
TestVar is integer, TempMax is float. None of them are passed in LUA as string as indicated in the Wiki.

Is it a bug? Has the behavior changed recently and the documentation was not updated?
I tried with latest stable 4.10717 on Pi and on 4.11300 on Win.

This question comes from a topic on the french speaking domoticz forum, where an user tried to compare his integer user variable to '0', without tostring or tonumber anywhere. I quickly looked around and found nothing about it here and around.

I wanted to know the official stand on this. Thanks
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Integer/number user variables are not passed as strings in LUA

Post by waaren »

boum wrote: Monday 16 September 2019 10:22 Is there an issue with the documentation there? https://www.domoticz.com/wiki/User_variables#Lua
When using user variables in Lua, keep in mind the following: variables are always stored as a string in the Domoticz database, regardless of their type. The variables are passed in arrays to Lua, at that point they will always be strings also.
Values for all type uservariables are stored in the database as VARCHAR(200) (= string) but floats and integer types are explicitly converted to float / integer in EventSystem.cpp before passing them to dzVents / Lua. It's been like this since at least 2016-05-15 (cannot see any earlier reference to this using the git blame command)
Please note that in the Lua version used in domoticz, type integer and float are both numbers. Lua only started to include integer types since version 5.3
Please go ahead and feel free to update the wiki accordingly.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
boum
Posts: 135
Joined: Friday 18 January 2019 11:31
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.10717
Location: France
Contact:

Re: Integer/number user variables are not passed as strings in LUA

Post by boum »

Thanks.
The source code of domoticz is still largely unknown to me. But with your message, I went and saw that the conversion to Lua number was indeed there when the code was ported from SVN to git (November 2014 it seems). So it's pretty old and that's just the documentation that is out of date.

I followed your advice and changed the Wiki. I also added a link to the relevant dzVent paragraph.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Integer/number user variables are not passed as strings in LUA

Post by waaren »

boum wrote: Monday 16 September 2019 15:23 I changed the Wiki. I also added a link to the relevant dzVent paragraph.
👍👍 Thx !
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest