Noob: Can't get the constant values to work

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

Moderator: leecollings

Post Reply
Marque1968
Posts: 60
Joined: Tuesday 03 April 2018 18:41
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Contact:

Noob: Can't get the constant values to work

Post by Marque1968 »

I have a global_data via the gui:
Spoiler: show

Code: Select all

-- global_data.lua
return {
   helpers = {
      findInArray = function(searchvalue, checkarray)
         local foundvalue = false
    		for key,value in pairs(checkarray) do 
    		    if (value == searchvalue) then 
    		        foundvalue=true 
    		    end
    		end
         return foundvalue
      end,
      findInDeviceArray = function(domoticz, searchvalue, checkarray)
         local foundvalue = false
    		for key,value in pairs(checkarray) do 
    		    if (domoticz.devices(value).state == searchvalue) then 
    		        foundvalue=true 
    		    end
    		end
         return foundvalue
      end,
      findInVariableArray = function(domoticz, searchvalue, checkarray)
         local foundvalue = false
    		for key,value in pairs(checkarray) do 
    		    if (domoticz.variables(value).value == searchvalue) then 
    		        foundvalue=true 
    		    end
    		end
         return foundvalue
      end,
      -- Device ID's
      DID_radiation = 193,
      DID_radiationdisplay = 149,
      
      -- Variable ID's
      VID_proxM = 1,
      VID_proxN = 4,
      
      -- Global variables
      MY_CONSTANT = 100 -- doesn't have to be a function
   }
}
The findIn... functions I have impemented in the system already, and work as expected, however not so for the constant variables. I have this script:
Spoiler: show

Code: Select all

local idx_radiation = domoticz.helpers.DID_radiation
local idx_virtual = domoticz.helpers.DID_radiationdisplay

return {
	on = {
		devices = { idx_radiation },
	},

	execute = function(domoticz, triggeredItem, info)
        domoticz.devices(idx_virtual).updateCounter(domoticz.utils.round(triggeredItem.radiation,0))
	end
}
But the calling of these constant variables result in the following log entry:

Code: Select all

2019-03-01 09:12:42.878 Status: dzVents: Error (2.4.14): ...icz/scripts/dzVents/generated_scripts/test radiation.lua:1: attempt to index field 'helpers' (a nil value)
(as you can see, I started from the example on https://www.domoticz.com/wiki/DzVents:_ ... _functions )

I also read there was a DATA section to the global_data.lua, but I do not want these values to be changed by functions in any other script (or even this one, for that matter).

Any input greatly appreciated.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Noob: Can't get the constant values to work

Post by waaren »

Marque1968 wrote: Friday 01 March 2019 9:37 I have a global_data via the gui:
The findIn... functions I have impemented in the system already, and work as expected, however not so for the constant variables.
But the calling of these constant variables result in the following log entry:

Code: Select all

2019-03-01 09:12:42.878 Status: dzVents: Error (2.4.14): ...icz/scripts/dzVents/generated_scripts/test radiation.lua:1: attempt to index field 'helpers' (a nil value)
I also read there was a DATA section to the global_data.lua, but I do not want these values to be changed by functions in any other script (or even this one, for that matter).
Your global_data is correct but you try to access the domoticz object in your script in an area where it is not accessible.
To retrieve the value from domoticz.helpers.DID_radiationdisplay, you will have to move the assign statement inside the execute = function part
like:

Code: Select all

return {
	on = {
		devices = { idx_radiation },
		timer = {"every minute"},
	},


	execute = function(domoticz, triggeredItem, info)
	    local idx_virtual = domoticz.helpers.DID_radiationdisplay
        domoticz.log("idx_virtual: " .. idx_virtual)
	end
}
The other thing is that the term CONSTANT used in global_data could led you believe that it cannot be changed in your script. Which is not the case as you can see from this example: (Let it execute at least twice to see what happens)

Code: Select all

return {
	on = {
		timer = {"every minute"},
	},


	execute = function(domoticz, triggeredItem, info)
	    local idx_virtual = domoticz.helpers.DID_radiationdisplay
	    domoticz.log("idx_virtual: " .. idx_virtual)
	   
	    domoticz.helpers.DID_radiationdisplay = "Not a real constant"
	    local idx_virtual = domoticz.helpers.DID_radiationdisplay
	    domoticz.log("idx_virtual: " .. idx_virtual)
	    
	end
}
 

In fact Lua does not know a constant type as such. As explained here there is a (complicated) way to work around that but I never found a good reason inside the domoticz area to use that approach.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Marque1968
Posts: 60
Joined: Tuesday 03 April 2018 18:41
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Contact:

Re: Noob: Can't get the constant values to work

Post by Marque1968 »

So what I want (and think I need), is not possible?

I want a list of devices and variables, accessible by name, so when a power plug fails (which happened to me last week) and gets replaced by another power plug, I want to changed the value in the list of constants so ALL scripts who reference that power plug in any way, now use the new device.

And if those constants can be changed in an other script is not a real issue for me, as I won't do that, AND if the system resets it would refer to the original value again, which is not the case with the data section.
Marque1968
Posts: 60
Joined: Tuesday 03 April 2018 18:41
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Contact:

Re: Noob: Can't get the constant values to work

Post by Marque1968 »

I think I found a work around
I now created a lua file in my home directory:

Code: Select all

var1=1
var2=2
var3=182
And added to the dzVents script:

Code: Select all

dofile("/home/pi/Documents/scripts/vartest.lua")

local idxs_of_switch = {var3} --{182}


return {
	on = {
		devices = idxs_of_switch,

	},

	execute = function(domoticz, triggeredItem, info)
		if (triggeredItem.active) then
		    domoticz.helpers.zendTelegram(domoticz,"info\r\n Device on")
		else
		    domoticz.helpers.zendTelegram(domoticz,"ALARM. Device is switched off !!!")
		end
	end
}
This does the trick:

HOWEVER

I am in a bit of doubt if this is fool proof. If the variable in the 'vartest' file is changed, how would domoticz know if it needs to trigger on a new value? Does it activate each and every script upon an event to check if the script needs to be executed? I would have thought it to be a set to a database trigger, but during my testing I could change the 'vartest' file without touching the dzvents script, and the triggers would react according to the new values. So I think I found myself a solution here...
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Noob: Can't get the constant values to work

Post by waaren »

Marque1968 wrote: Sunday 03 March 2019 11:37 So what I want (and think I need), is not possible?

I want a list of devices and variables, accessible by name, so when a power plug fails (which happened to me last week) and gets replaced by another power plug, I want to changed the value in the list of constants so ALL scripts who reference that power plug in any way, now use the new device.

And if those constants can be changed in an other script is not a real issue for me, as I won't do that, AND if the system resets it would refer to the original value again, which is not the case with the data section.
If the fact that they can be changed in other scripts is not an issue for you, it is possible and in fact you already did it. Just define them in global_data and use them as in my example.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Marque1968
Posts: 60
Joined: Tuesday 03 April 2018 18:41
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Contact:

Re: Noob: Can't get the constant values to work

Post by Marque1968 »

Ok, didn't think of that. I had a seperate file, which was not accessable from the UI, so I could not alter it from a remote location. Your solution lets me alter the work-installation from the comfort of my couch at home :)
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest