Global variable as trigger for a script  [Solved]

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

Moderator: leecollings

Post Reply
guenniac
Posts: 23
Joined: Wednesday 27 October 2021 18:13
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Aachen, Germany
Contact:

Global variable as trigger for a script

Post 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.
Raspberry Pi 3B+
Raspbian GNU/Linux 12 (bookworm) / 6.1.61-v7+
Version: 2023.2
Build Hash: 19efd039c
Compile Date: 2023-07-21 17:23:44
dzVents Version: 3.1.8
Python Version: 3.11.2 (main, Mar 13 2023, 12:18:29) [GCC 12.2.0]
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: Global variable as trigger for a script

Post 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).
guenniac
Posts: 23
Joined: Wednesday 27 October 2021 18:13
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Aachen, Germany
Contact:

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

Post 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?
Raspberry Pi 3B+
Raspbian GNU/Linux 12 (bookworm) / 6.1.61-v7+
Version: 2023.2
Build Hash: 19efd039c
Compile Date: 2023-07-21 17:23:44
dzVents Version: 3.1.8
Python Version: 3.11.2 (main, Mar 13 2023, 12:18:29) [GCC 12.2.0]
guenniac
Posts: 23
Joined: Wednesday 27 October 2021 18:13
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Aachen, Germany
Contact:

Re: Global variable as trigger for a script

Post 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,
	    },
},
Raspberry Pi 3B+
Raspbian GNU/Linux 12 (bookworm) / 6.1.61-v7+
Version: 2023.2
Build Hash: 19efd039c
Compile Date: 2023-07-21 17:23:44
dzVents Version: 3.1.8
Python Version: 3.11.2 (main, Mar 13 2023, 12:18:29) [GCC 12.2.0]
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest