Page 1 of 1

Global variable as trigger for a script

Posted: Sunday 20 November 2022 15:45
by guenniac
Is it possible to use a variable, which is defined in the global_data part as a trigger to fire a dzvents script,
eg. global_data
HeatingPeriod = { initial = ' on 01/11-31/12, 01/01-30/04' },

here in my script:

return {
on = {
timer = {
SUNRISE .. domoticz.globalData.HeatingPeriod,
},
.....
where SUNRISE is locally defined

I couldn't find any solution for that.

Re: Global variable as trigger for a script

Posted: Monday 21 November 2022 0:48
by boum
No, it is not possible, the domoticz part of `domoticz.globalData.HeatingPeriod` you use in the execute function is only defined as a parameter and passed from the event system when the script is executed.
When the script is compiled and first executed to get the "return" table, the variable is not defined. Besides, it would be called only when the script is reevaluated, not every time the trigger are checked.

You could work around that by having a script running every minutes (or a shorter fixed time interval). In the execute function, you check your gloabl persistent variable. If that matches the current time, you can then send a custom event (or execute directly your code there).

Re: Global variable as trigger for a script  [Solved]

Posted: Monday 21 November 2022 9:53
by guenniac
Hi, meanwhile I have found this: viewtopic.php?t=35190
and I gave it a try.
In the data part of global_data I defined this:

Code: Select all

        HEATINGPERIOD = { initial = ' on 01/11-31/12, 01/01-30/04' },
And I used it here:

Code: Select all

local dz = require ("global_data")

local SUNRISE = 'every minute '
local PROG = 'requireglobaldata'

return {
	on = {
	    timer = {  
	        SUNRISE .. dz.globalData.HEATINGPERIOD,
	    },
	},
	data = {},
	execute = function(dz, timer)

        timer.dump()
    end
}
with this result:

Code: Select all

2022-11-21 09:44:51.652 Error: dzVents: Error: (3.1.7) .../scripts/dzVents/generated_scripts/requireglobaldata.lua:9: attempt to index a nil value (field 'globalData')

In the example URL they used dz.helpers.
I tried both dz.globalData.HEATINGPERIOD as well as dz.helpers. No success.

Any ideas?

Re: Global variable as trigger for a script

Posted: Monday 21 November 2022 10:43
by guenniac
It works now!
I moved HEATINGPERIOD to the end of the helpers definition

Code: Select all

....
        end,
        HEATINGPERIOD = ' on 01/11-31/12, 01/01-30/04',
	}
}
And in my script I used:

Code: Select all

on = {
	    timer = {  
	        SUNRISE .. dz.helpers.HEATINGPERIOD,
	    },
},