dzVents - Possible to use a variable for a timed event

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

Moderator: leecollings

User avatar
rbisschops
Posts: 63
Joined: Monday 12 May 2014 11:20
Target OS: Raspberry Pi / ODroid
Domoticz version: 2.2563
Location: Schiedam, Netherlands
Contact:

dzVents - Possible to use a variable for a timed event

Post by rbisschops »

Hi All,

As I'm using dzVents for all my Lua scripts now, i stumbled into a thought, but not sure how to do this or even if it is possible.

I created a script controling my blinds. It lowers the blinds at a fixed time in the morning and raises them in the evening. Both at a fixed time coded in the script as follows:

Code: Select all

return {
    active = true,
    on = {
        timer = {
            'at 07:30',
            'at 17:30'
        }
    },
As we (the wife) wants to be able to influence the timers, I would like to put these in a variable in Domoticz. Is it possible to use the variables for the timed events. So something like:

Code: Select all

timer = at {someVariable}
I know this code won't work, but is put in as an example to explain my thinking :-)

Thanks all for your thoughts.

Ralph
RPi 3B (master): RFXtrx433, Harmony Hub, Z-Wave devices, CoCo units, Hue, Xiaomi Aquara
RPi 2B (slave): P1 connection, MySensors with custom built watermeter sensors on Aquadis+
RPi’s with: Mosquitto MQTT, Home Bridge, Home Assistant (for evaluation only)
TheCondor
Posts: 78
Joined: Thursday 18 June 2015 10:32
Target OS: Linux
Domoticz version:
Contact:

Re: dzVents - Possible to use a variable for a timed event

Post by TheCondor »

User avatar
rbisschops
Posts: 63
Joined: Monday 12 May 2014 11:20
Target OS: Raspberry Pi / ODroid
Domoticz version: 2.2563
Location: Schiedam, Netherlands
Contact:

Re: dzVents - Possible to use a variable for a timed event

Post by rbisschops »

Hi,

That is not exactly what i was lookling for. I want the time set in the domoticz variable to be the trigger. So 7:30 in the variable should be the trigger to kick off the script. For now I have a 15 minute time trigger and check in the script if the domo variable time is the same as the system time. This has some limitations in the time you set in the variable.
RPi 3B (master): RFXtrx433, Harmony Hub, Z-Wave devices, CoCo units, Hue, Xiaomi Aquara
RPi 2B (slave): P1 connection, MySensors with custom built watermeter sensors on Aquadis+
RPi’s with: Mosquitto MQTT, Home Bridge, Home Assistant (for evaluation only)
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: dzVents - Possible to use a variable for a timed event

Post by waaren »

I guess the solution is to read the content of the var when it changed and with that content calculate what the afterSec should be to be used with an openURL of which the repsonse will trigger the event that will do your required action and do sent the openURL in 24 hours.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
rbisschops
Posts: 63
Joined: Monday 12 May 2014 11:20
Target OS: Raspberry Pi / ODroid
Domoticz version: 2.2563
Location: Schiedam, Netherlands
Contact:

Re: dzVents - Possible to use a variable for a timed event

Post by rbisschops »

@waaren: but unfortunately the var does not change that often (its more a setting)
RPi 3B (master): RFXtrx433, Harmony Hub, Z-Wave devices, CoCo units, Hue, Xiaomi Aquara
RPi 2B (slave): P1 connection, MySensors with custom built watermeter sensors on Aquadis+
RPi’s with: Mosquitto MQTT, Home Bridge, Home Assistant (for evaluation only)
randytsuch
Posts: 90
Joined: Sunday 20 March 2016 18:56
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: LA, Ca USA
Contact:

Re: dzVents - Possible to use a variable for a timed event

Post by randytsuch »

Define the time as a user variable of type string.

Then you can read it into dzvents
randytsuch
Posts: 90
Joined: Sunday 20 March 2016 18:56
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: LA, Ca USA
Contact:

Re: dzVents - Possible to use a variable for a timed event

Post by randytsuch »

Oops, I lied

Make a user variable of type time

Then, if you can do something like this to create local variables with hours and mins, for example.

local thour = domoticz.variables('SpkrStart').time.hour
local tmin = domoticz.variables('SpkrStart').time.min

user variable is name SpkrStart in the user variable page, with value set to whatever time you want to use, ie 19:00
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: dzVents - Possible to use a variable for a timed event

Post by waaren »

rbisschops wrote: Monday 06 August 2018 16:26 @waaren: but unfortunately the var does not change that often (its more a setting)
What you can do is trigger a script on that variable change and use the appropriate API call to delete / add the timer for that the device.
Look at the domoticz API wiki domoticz API wiki for the exact syntaxis of the calls
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: dzVents - Possible to use a variable for a timed event

Post by waaren »

rbisschops wrote: Monday 06 August 2018 16:26 @waaren: but unfortunately the var does not change that often (its more a setting)
Something like this ?

Code: Select all

--   setTimers.lua

return { 
    on = {   variables   =   {"timer*"}},   -- One time var for On and one for Off 

    execute = function(dz,triggerObject)
        local myDevice    = 1012                      -- myDevice    = idx of myDevice
        local onTimeVar   = "timerTestOn"             -- uservariable of type time, format hh:mm
        local offTimeVar  = "timerTestOff"

        local function  clearTimers(idx)
                local api = dz.settings['Domoticz url'] .. "/json.htm?type=command&param=cleartimers&idx=".. idx
                dz.openURL({ url = api, method = "GET" })
        end

        local function decomposeVarTime(timeVar)
            return dz.variables(timeVar).time.hour, dz.variables(timeVar).time.minutes
         
        end

        local function  setTimers(idx,sethour,setminute,state)
           local command = 0
           if state == "Off" then 
            command = 1
           end
           local api  = dz.settings['Domoticz url'] .. "/json.htm?active=true&command=" .. command .. 
                                                        "&hour=".. sethour ..
                                                        "&min=" .. setminute .. "&param=addtimer" ..
                                                        "&randomness=false&timertype=2;&type=command&days=1234567&idx=" .. idx
           dz.openURL({ url = api, method = "GET" })
        end

        clearTimers(myDevice)
        local hour,minute = decomposeVarTime(onTimeVar)
        setTimers(myDevice,hour,minute)
        hour,minute = decomposeVarTime(offTimeVar)
        setTimers(myDevice,hour,minute,"Off")

    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
randytsuch
Posts: 90
Joined: Sunday 20 March 2016 18:56
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: LA, Ca USA
Contact:

Re: dzVents - Possible to use a variable for a timed event

Post by randytsuch »

User avatar
rbisschops
Posts: 63
Joined: Monday 12 May 2014 11:20
Target OS: Raspberry Pi / ODroid
Domoticz version: 2.2563
Location: Schiedam, Netherlands
Contact:

Re: dzVents - Possible to use a variable for a timed event

Post by rbisschops »

@waaren: Thx for setting this up. Highly appreciated!
Is it possible to do this:

Code: Select all

dz.variables(timeVar).time.hour, dz.variables(timeVar).time.minutes
As far as I understand from the documentation, its a string (even though its a date var)
What I can't figure out is why you use:

Code: Select all

 on = {   variables   =   {"timer*"}},
This requires one of the variables to change right? Otherwise the script won't fire.

Thx

Ralph
RPi 3B (master): RFXtrx433, Harmony Hub, Z-Wave devices, CoCo units, Hue, Xiaomi Aquara
RPi 2B (slave): P1 connection, MySensors with custom built watermeter sensors on Aquadis+
RPi’s with: Mosquitto MQTT, Home Bridge, Home Assistant (for evaluation only)
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: dzVents - Possible to use a variable for a timed event

Post by waaren »

rbisschops wrote: Thursday 09 August 2018 17:01 @waaren: Thx for setting this up. Highly appreciated!
Is it possible to do this:

Code: Select all

dz.variables(timeVar).time.hour, dz.variables(timeVar).time.minutes
As far as I understand from the documentation, its a string (even though its a date var)
yes this works. I tested this with uservars type time


What I can't figure out is why you use:

Code: Select all

 on = {   variables   =   {"timer*"}},
This requires one of the variables to change right? Otherwise the script won't fire.

Thx

Ralph
The idea is that when one uservar is changed a new set of timers (on/off) will be set after the current ones are cleared.
Until the uservar is changed again the timerset will stay active.
That is what I understand was your requirement.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
User avatar
rbisschops
Posts: 63
Joined: Monday 12 May 2014 11:20
Target OS: Raspberry Pi / ODroid
Domoticz version: 2.2563
Location: Schiedam, Netherlands
Contact:

Re: dzVents - Possible to use a variable for a timed event

Post by rbisschops »

waaren wrote: Thursday 09 August 2018 18:28
rbisschops wrote: Thursday 09 August 2018 17:01 @waaren: Thx for setting this up. Highly appreciated!
Is it possible to do this:

Code: Select all

dz.variables(timeVar).time.hour, dz.variables(timeVar).time.minutes
As far as I understand from the documentation, its a string (even though its a date var)
yes this works. I tested this with uservars type time
Which version did you test this on (i'm on V3.9639)? I have user vars of typ time in my Domoticz, but when I add the code you propose, I get a nil value.
RPi 3B (master): RFXtrx433, Harmony Hub, Z-Wave devices, CoCo units, Hue, Xiaomi Aquara
RPi 2B (slave): P1 connection, MySensors with custom built watermeter sensors on Aquadis+
RPi’s with: Mosquitto MQTT, Home Bridge, Home Assistant (for evaluation only)
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: dzVents - Possible to use a variable for a timed event

Post by waaren »

rbisschops wrote: Friday 10 August 2018 8:35
waaren wrote: Thursday 09 August 2018 18:28
rbisschops wrote: Thursday 09 August 2018 17:01 @waaren: Thx for setting this up. Highly appreciated!
Is it possible to do this:

Code: Select all

dz.variables(timeVar).time.hour, dz.variables(timeVar).time.minutes
As far as I understand from the documentation, its a string (even though its a date var)
yes this works. I tested this with uservars type time
Which version did you test this on (i'm on V3.9639)? I have user vars of typ time in my Domoticz, but when I add the code you propose, I get a nil value.
Tested with V4.9796
timeVars.PNG
timeVars.PNG (60.12 KiB) Viewed 5083 times
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Inso80
Posts: 12
Joined: Thursday 17 May 2018 13:28
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: dzVents - Possible to use a variable for a timed event

Post by Inso80 »

randytsuch wrote: Monday 06 August 2018 17:50 Define the time as a user variable of type string.

Then you can read it into dzvents
If I may ask: how?
I am planning to do a lot with my global variables. Timer will become important, also f.e.

Code: Select all

active = true,
would be awesome to be changeable with (global?) variables.
If I try

Code: Select all

active = domoticz.variables(domoticz.globalData.test),
it tells me

Code: Select all

Status: dzVents: Error (2.4.6): .../domoticz/scripts/dzVents/generated_scripts/testVari.lua:3: attempt to index field 'globalData' (a nil value) 
Printing domoticz.globalData.test in log shows true.
Also if I just try "active = domoticz.globalData.test" , active = domoticz.variables(globalData.test) and so on, I always get an error.

I also had the problem with time =domoticz.globalData.test

What am I missing? Dzvents documentation does not show anything helpful, it only says

Code: Select all

active = function(domoticz)
Seems to be a bit awkward to create dummies within domotics for every global variable and then change them using the api and so on.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: dzVents - Possible to use a variable for a timed event

Post by waaren »

Inso80 wrote: Monday 17 September 2018 15:19 .....

What am I missing? Dzvents documentation does not show anything helpful, it only says

Code: Select all

active = function(domoticz)
...
the active = section defaults to true and can only be true or false
If you need to evaluate something from domoticz you have to parse the domoticz object to it.
Something like

Code: Select all

active = function(domoticz)
			  return domoticz.startTime.secondsAgo < 180
			end,
works
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Inso80
Posts: 12
Joined: Thursday 17 May 2018 13:28
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: dzVents - Possible to use a variable for a timed event

Post by Inso80 »

waaren wrote: Monday 17 September 2018 18:19
Inso80 wrote: Monday 17 September 2018 15:19 .....

What am I missing? Dzvents documentation does not show anything helpful, it only says

Code: Select all

active = function(domoticz)
...
the active = section defaults to true and can only be true or false
If you need to evaluate something from domoticz you have to parse the domoticz object to it.
Something like

Code: Select all

active = function(domoticz)
			  return domoticz.startTime.secondsAgo < 180
			end,
works
Thanks for your reply.

Do you know any way to parse (global) variables this way (which can also be true or false)? Atm the only way I see to parse "a text of my choice" seems to be to set a text note to the text I want, (f.e.: true, 6:30..) and then parse the value of it where I need my variable..
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: dzVents - Possible to use a variable for a timed event

Post by waaren »

Inso80 wrote: Monday 17 September 2018 20:11
Do you know any way to parse (global) variables this way (which can also be true or false)? Atm the only way I see to parse "a text of my choice" seems to be to set a text note to the text I want, (f.e.: true, 6:30..) and then parse the value of it where I need my variable..
Not quite sure I understand your question.

Do you mean domoticz variables or variables from dzVents persistent data or something else?
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Inso80
Posts: 12
Joined: Thursday 17 May 2018 13:28
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: dzVents - Possible to use a variable for a timed event

Post by Inso80 »

waaren wrote: Monday 17 September 2018 22:56
Not quite sure I understand your question.

Do you mean domoticz variables or variables from dzVents persistent data or something else?
In short what I am planning to do: I´d like to change the time of my WakeUpLight on multiple places. MySensors, here in Domoticz event manager, later on Raspi, tasker at my phone and so on.
So i´d like to have a variable to store the time, which I can read, update and so on.
Some of my variables are global (f.e. CT and brightness), located in domoticz/scripts/dzVents/scripts/global_data.lua
I can read and manipulate them perfectly within execute = function...
but if I try to replace

Code: Select all

active = true
with

Code: Select all

active = domoticz.globalData.myVar
while myVar = 'true', I get the error it has a nil value. Also havent found a way to convert String to boolean in Lua or something like this.
same with

Code: Select all

timer = { domoticz.globalData.myVar }
, I can not just set the variable to 'at 6:30'
log always says:

Code: Select all

attempt to index field 'globalData' (a nil value)
and

Code: Select all

timer = { 'domoticz.globalData.myVar'  }
simply does not get executed.

Would be perfect if I could get that to work.



For the case it is not possible:

I have checked other options. Easiest seemed to be a text dummy. With your hint I got

Code: Select all

active = function(domoticz)
			 return domoticz.devices('Texttest').text == os.date("%H")
			 end,
running

Strangely,

Code: Select all

active = true,

	on ={
		timer = function(domoticz)
			    return domoticz.devices('Texttest').text == os.date("%H")
			    end,
	},
does _not_ run a single time. As the wiki says, timer = should be triggered every minute. "active = " runs fine while "timer =" using same function call was not executed a single time within ~30 minutes.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: dzVents - Possible to use a variable for a timed event

Post by waaren »

Inso80 wrote: Monday 17 September 2018 23:58 Strangely,

Code: Select all

active = true,

	on ={
		timer = function(domoticz)
			    return domoticz.devices('Texttest').text == os.date("%H")
			    end,
	},
does _not_ run a single time. As the wiki says, timer = should be triggered every minute. "active = " runs fine while "timer =" using same function call was not executed a single time within ~30 minutes.
You are close...

Code: Select all

return {
       
     on = { 
        timer = {  function(domoticz)
                     return domoticz.devices('Texttest').text == os.date("%H")
                   end
        }},

        execute = function(domoticz)
        print ( "Executing!!!")
        
    end
}
But would it not be less complicated to use timer = { "every minute" }, and a helper function in the execute = section as described here ?
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest