how can i get the value of a timer into a variable?

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

Moderator: leecollings

Post Reply
bitzy
Posts: 36
Joined: Tuesday 18 April 2023 8:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Contact:

how can i get the value of a timer into a variable?

Post by bitzy »

english is not primary language so i'm sure the title is not suggestive and the explanation will be a struggle :D

i want to take the value that i set for a timer into a variable inside the script, something like:

timer = { 'every x minutes' }
...
local dt = time_Of_The_Timer -- (x).

i know i can do this manually when i set the value of the timer (but i'm kinda lazy) so i want this to be done automatically when i change 'x', in case i forget to change dt also. is there a way to do this?
User avatar
waltervl
Posts: 5846
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: how can i get the value of a timer into a variable?

Post by waltervl »

You could do it the other way around:

local dt = time_Of_The_Timer

timer = { 'every dt minutes' }
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
willemd
Posts: 642
Joined: Saturday 21 September 2019 17:55
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.1
Location: The Netherlands
Contact:

Re: how can i get the value of a timer into a variable?

Post by willemd »

Or you could take the second value after you split the trigger into words, as shown in the example dzVents script below

Code: Select all

return {
	on = {
		timer = {
			'every 2 minutes',	
		}
	},
	logging = {
		level = domoticz.LOG_INFO,
		marker = 'template',
	},
	execute = function(domoticz, timer)
		domoticz.log('Timer event was triggered by ' .. timer.trigger, domoticz.LOG_INFO)
		local deltaT=domoticz.utils.stringSplit(timer.trigger," ")[2]
		domoticz.log("deltaT="..deltaT, domoticz.LOG_INFO)
	end
}
Kedi
Posts: 575
Joined: Monday 20 March 2023 14:41
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Somewhere in NL
Contact:

Re: how can i get the value of a timer into a variable?

Post by Kedi »

waltervl wrote: Sunday 21 January 2024 18:28 You could do it the other way around:

local dt = time_Of_The_Timer

timer = { 'every dt minutes' }
Sorry @waltervl that does not work, this works:

Code: Select all

local dt = 2

return {
    on = {
        timer = {
            'every '..tostring(dt)..' minutes',
        },
And sorry @willemd I think it is the better solution: does not involve further programming.
Logic will get you from A to B. Imagination will take you everywhere.
HvdW
Posts: 612
Joined: Sunday 01 November 2015 22:45
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Twente
Contact:

Re: how can i get the value of a timer into a variable?

Post by HvdW »

Or use

Code: Select all

local current_time =  os.date("%c")
and

Code: Select all

local subject = 'My time in a string. '
                    local message = 
                    ' ' .. current_time .. '\n'.. 
                    'Succeede ! \n' 
                    domoticz.notify(subject, message, domoticz.PRIORITY_NORMAL)
Where current_time is your string.

Formatting the output of os.date() is explained over here.
Bugs bug me.
bitzy
Posts: 36
Joined: Tuesday 18 April 2023 8:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Contact:

Re: how can i get the value of a timer into a variable?

Post by bitzy »

waltervl wrote: Sunday 21 January 2024 18:28 You could do it the other way around:

local dt = time_Of_The_Timer

timer = { 'every dt minutes' }
was so easy, i feel a little bit ashamed now :oops: :D

for me worked:
dt = 2
timer = {'every '..dt..' minutes'} and also the Kedi's version.

the local dt=domoticz.utils.stringSplit(timer.trigger," ")[2] don't work for me. i get an error related to the use of 'timer.trigger' - attempt to index a nil value 'timer'
thanks for the answears.
willemd
Posts: 642
Joined: Saturday 21 September 2019 17:55
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.1
Location: The Netherlands
Contact:

Re: how can i get the value of a timer into a variable?

Post by willemd »

It does not work because you probably don't have execute=function(domoticz,timer) in your script but something else for example "item" instead of "timer".

If you use "item" then you should also use "item.trigger". It has to match.
bitzy
Posts: 36
Joined: Tuesday 18 April 2023 8:46
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Contact:

Re: how can i get the value of a timer into a variable?

Post by bitzy »

willemd wrote: Tuesday 23 January 2024 9:50 It does not work because you probably don't have execute=function(domoticz,timer) in your script but something else for example "item" instead of "timer".

If you use "item" then you should also use "item.trigger". It has to match.
u are right :D. is working now
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest