Define uservariables Topic is solved

Moderator: leecollings

Post Reply
alkempter
Posts: 26
Joined: Sunday 17 February 2019 10:34
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.2
Contact:

Define uservariables

Post by alkempter »

Hello,
I am currently considering a new install of domoticz, but have accumulated a good considerable number of uservariables.
Is there a means that spares me the need to define the variables and setting their last values again?
I am thinking about a LUA script that dumps the variables and creates a LUA script that can be run to have the uservariables up and running again fast.
Or is it more efficient to reinstall Domoticz and restore the database?
Thank you a lot in advance!
Cheers Al
alkempter
Posts: 26
Joined: Sunday 17 February 2019 10:34
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.2
Contact:

Re: Define uservariables

Post by alkempter »

I recently stumbled over the information that it is possible to add/define uservariables with a JSON e.g.:
http://192.168.0.10:8080/json.htm?type= ... =Uvarvalue
and that it is also possible to dump all the uservariables with a JSON e.g.:
http://192.168.0.10:8080/json.htm?type= ... rvariables
where the beginning is the IP address and port of your Domoticz server (will also work with SSL).

So I tried to realize to store all the variables and make a LUA script that will be able to check the effective variables against the values in the script and (if desired) create the variable if not present and update the value if different.
I documented the way I did it, knowing that more can be automated and scripted but I did not see enough value in that. The script could be useful to help with testing or to build up a clean new environment without transferring the database.

Use the update function with caution.

To begin you can save the raw result of the getuservariables command as a file which will look e.g. like that:

Code: Select all

{
	"result" : 
	[
		{
			"LastUpdate" : "2019-12-31 10:43:13",
			"Name" : "Thermo01_Korrektur",
			"Type" : "1",
			"Value" : "0.0",
			"idx" : "30"
		},
		{
			"LastUpdate" : "2020-01-17 19:26:29",
			"Name" : "xSecondBedR",
			"Type" : "2",
			"Value" : "G\u00e4stezimmer",
			"idx" : "38"
		}
	],
	"status" : "OK",
	"title" : "GetUserVariables"
}
You can also get this file via curl (here an example from my Synology implementation, no https):
/usr/bin/curl -s -v -o /volume1/Folder/uservar.txt "http://192.168.0.10:8080/json.htm?type= ... rvariables"
One problem with this output can be seen in the second example. The value is presented in the UTF-16 encoding, so you have to do manual editing with the values later on. But you have to do some educated editing of this information anyway as you may not include all your uservariables in this script and/or not with the values they have in the moment of the issue of the command.

Next step is to edit the file to get it (with find/replace) into the following format so it will fit into the script later:

Code: Select all

-- "LastUpdate" : "2019-12-31 10:43:13"
NVName = "Thermo01_Korrektur"
NVType = "1"
NVValue = "0.0"
-- idx "30"
checkUvar(NVName, NVType, NVValue, NVCreateupdate)
	
-- "LastUpdate" : "2020-01-17 19:26:29"
NVName = "xSecondBedR"
NVType = "2"
NVValue = "G\u00e4stezimmer"
-- idx "38"
checkUvar(NVName, NVType, NVValue, NVCreateupdate)

After also removing (or commenting out) unwanted uservariables for this purpose and correcting the values (now NVValue), it is possible to copy/paste these data into the following LUA script (I used the type uservariable):

Code: Select all

-- uservariables types
-- 0 = Integer, e.g. -1, 1, 0, 2, 10  
-- 1 = Float, e.g. -1.1, 1.2, 3.1
-- 2 = String e.g. On, Off, Hello
-- 3 = Date in format DD/MM/YYYY
-- 4 = Time in 24 hr format HH:MM

commandArray = {}

function checkUvar(checkUvarname, checkUvartype, checkUvarvalue, checkUvarcreateupdate)
    -- may be called by e.g.: checkUvar('TestVariable1', '0', '1', "yes")
    local Uvarname = tostring(checkUvarname)
    local Uvartype = tostring(checkUvartype)
    local Uvarvalue = tostring(checkUvarvalue)
    local Uvarcreateupdate = tostring(checkUvarcreateupdate)
    local UvarRC = 0
    print ("UV " ..tostring(Uvarname).. "!")
    if uservariables[Uvarname] then
        Uvareffvalue = tostring(uservariables[Uvarname])
        if Uvareffvalue ~= Uvarvalue then
            -- print ("*" ..Uvareffvalue.. "*" ..Uvarvalue.. "*")
            print ("Wert von Uservariable " ..tostring(Uvarname).. " has the value " ..tostring(Uvareffvalue).. " instead of " ..tostring(Uvarvalue).. "!")
            UvarRC = 4
            if Uvarcreateupdate == "yes" then
                print ("Uservariable " ..tostring(Uvarname).. " will be set to " ..tostring(Uvarvalue).. "!")
                commandArray[#commandArray + 1] = {['Variable:' ..Uvarname.. ''] = Uvarvalue}
                UvarRC = 0
            end
        else
            print ("Uservariable " ..Uvarname.. " ok")
        end
    else
        print ("Uservariable "  ..tostring(Uvarname).. " does not exist!")
        UvarRC = 8
        if ((Uvarcreateupdate == "yes") and (Uvarname ~= "nil")) then
            print ("Uservariable "  ..tostring(Uvarname).. " will be defined")
            commandArray[#commandArray + 1] = {['OpenURL']='http://192.168.0.10:8080/json.htm?type=command&param=adduservariable&vname='..myurlencode(Uvarname)..'&vtype='..Uvartype..'&vvalue='..myurlencode(Uvarvalue)..''}
            UvarRC = 0
        end
    end
    return UvarRC
end

function myurlencode(urlstr)
    if (urlstr) then
        urlstr = string.gsub (urlstr, "\n", "\r\n")
        urlstr = string.gsub (urlstr, "([^%w ])",
            function (c) return string.format ("%%%02X", string.byte(c)) end)
        urlstr = string.gsub (urlstr, " ", "+")
    end
    return urlstr
end

NVCreateupdate = "no"

-- "LastUpdate" : "2019-12-31 10:43:13"
NVName = "Thermo01_Korrektur"
NVType = "1"
NVValue = "0.0"
-- idx "30"
checkUvar(NVName, NVType, NVValue, "yes")
	
-- "LastUpdate" : "2020-01-17 19:26:29"
NVName = "xSecondBedR"
NVType = "2"
NVValue = "Gästezimmer"
-- idx "38"
checkUvar(NVName, NVType, NVValue, NVCreateupdate)

-- loop through all the changed variables
for variableName,variableValue in pairs(uservariablechanged) do
    print ("Variable based event fired on '"..variableName.."', value '"..tostring(variableValue).."'");
--    if (variableName=='myVariable') then
--        if variableValue == 1 then
--            commandArray['a device name'] = "On"
--            commandArray['another device name'] = "Off AFTER 10"
--  	    commandArray['Variable:myVariable'] = 'new value'
--        end
--    end
end

return commandArray
Setting the fourth parameter of the checkUvar function manually to "yes" will cause an update or the define of this uservariable.
Should also work with variables having special characters in the name or values (after editing the values in the script).

Best regards
Al

tested with code level 2020.1 with Python on Synology NAS (Apollolake)
btw: the function myurlencode was derived from GITHUB
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest