Page 1 of 1

Use user variable to trigger event

Posted: Friday 21 June 2019 14:46
by jberinga
Can I and how can I use a user variable that triggers an event?

I have this dzVents script but the when the nomotionCounter variable hits 3 the lights won't turn off:

Code: Select all

return {
    on = {
        timer = { 'at 22:15' },
		variables = { 'nomotionCounter' },
        devices = { 'IsDonker', 'IemandThuis' },
    },
    execute = function(domoticz, device, variable)

      --Niemand thuis, Alle verlichting aan = alle verlichting UIT 
	   if 
            domoticz.devices('VerlichtingBeneden_IemandThuis').state == 'On' and
			domoticz.devices('IemandThuis').state == 'Off' and
			domoticz.time.matchesRule('between 22:15 and sunrise')
		then
                domoticz.scenes('VerlichtingBeneden_AllOff').switchOn()
                domoticz.log('Verlichting beneden uit', domoticz.LOG_INFO)

	  --Iemand thuis, Alle verlichting aan, 10 minuten geen beweging = sommige verlichting DIMMEN 
	   elseif 
            domoticz.devices('VerlichtingBeneden_IemandThuis').state == 'On' and
			domoticz.devices('VerlichtingBeneden_BijnaUit').state == 'Off' and
			domoticz.variables('nomotionCounter').value == 10 and
			domoticz.time.matchesRule('between 22:15 and sunrise')
		then
                domoticz.devices('DimmerKeukenTafel').dimTo(20)
				domoticz.devices('DimmerStaandeLamp').dimTo(20)
				domoticz.devices('VerlichtingBeneden_BijnaUit').switchOn()
                domoticz.log('Verlichting beneden bijna uit', domoticz.LOG_INFO)

      --Niemand thuis, Alle verlichting aan, Slaapkamer verlichting 3 minuten aan = alle verlichting UIT 
	   elseif 
            domoticz.devices('VerlichtingBeneden_IemandThuis').state == 'On' and
			domoticz.devices('EntertainmentUit').state == 'On' and
			domoticz.devices('DimmerSlaapkamer').state ~= 'Off' and
			domoticz.variables('nomotionCounter').value == 3 and
			domoticz.time.matchesRule('between 22:15 and sunrise')
		then
                domoticz.scenes('VerlichtingBeneden_AllOff').switchOn()
                domoticz.log('Verlichting beneden uit', domoticz.LOG_INFO)
	   
	   --Donker, Niemand thuis = Sommige verlichting UIT
		elseif
            domoticz.devices('VerlichtingBeneden_NiemandThuis').state == 'On' and
			domoticz.time.matchesRule('at 22:15')
		then
                domoticz.scenes('VerlichtingBeneden_SomeOff').switchOn()
                domoticz.log('Sommige Verlichting beneden uit', domoticz.LOG_INFO)	

	   --Donker, Niemand thuis, Na 22:15 = Keuken verlichting tijdelijk UIT
		elseif
            domoticz.devices('VerlichtingBeneden_Tijdelijk').state == 'On' and
			domoticz.devices('IemandThuis').state == 'Off' and
			domoticz.time.matchesRule('between 22:15 and sunrise')
		then
                domoticz.devices('DimmerKeukenTafel').switchOff()
				domoticz.devices('VerlichtingBeneden_Tijdelijk').switchOff()
                domoticz.log('Keuken Verlichting tijdelijk uit', domoticz.LOG_INFO)				
        end
    end
}
This variable is updated by a lua script that updates the variable by 1 when a sensor isn't turn on\active:
Spoiler: show
-- script_time_nomotion.lua

local motion_switch = 'SensorMotionKeuken'
local nomotion_uservar = 'nomotionCounter'
local status_switch = 'IemandThuis'
local media_switch = 'EntertainmentUit'

commandArray = {}

no_motion_minutes = tonumber(uservariables[nomotion_uservar])

if (otherdevices[motion_switch] == 'Off') and (otherdevices[media_switch] == 'On') then
no_motion_minutes = no_motion_minutes + 1
--print('<font color="red">No motion has been detected for: ' ..no_motion_minutes.. ' minutes</font>')
else
no_motion_minutes = 0
end

commandArray['Variable:' .. nomotion_uservar] = tostring(no_motion_minutes)

if otherdevices[status_switch] == 'On' and no_motion_minutes > 15 then --change the 15 to the amount of minutes you prefer
commandArray[status_switch]='Off'
end

return commandArray
Hope someone can help me with this.

Re: Use user variable to trigger event

Posted: Friday 21 June 2019 14:57
by Lebo2d9
I think user variables aren't treated by dzventz

Re: Use user variable to trigger event

Posted: Friday 21 June 2019 15:35
by boum
I don't see a problem just by looking at the script, just a few points:

For the function definition, you should change it to:

Code: Select all

execute = function(domoticz, item)
(it is not important for your issue since you use neither device nor variable, but you would not get what you want in those)

The only thing I can think of is the type of the user variable. Did you set it to integer? The equality test may not pass otherwise. There are multiple conditions on your tests with ands so it may not be the variable part that fails.

You can still put some debug log on top of your execute function:

Code: Select all

execute = function(domoticz, item)
  if item.isVariable then
    domoticz.log("Variable " .. item.name .. " changed to " .. item.value, domoticz.LOG_INFO)
  end
  -- ....

Re: Use user variable to trigger event

Posted: Friday 21 June 2019 15:58
by jberinga
Thanks boum.

The variable is an integer and because I am very new to dzVents I still wonder what to put between the () in execute = function(). That isn't very clear to me :?

And your suggestion for a debug log isn't giving anything, while it should as every minute the variable is updated (+1) because I am not at home and thus the sensor is not triggered.

Re: Use user variable to trigger event

Posted: Friday 21 June 2019 17:02
by waaren
Lebo2d9 wrote: Friday 21 June 2019 14:57 I think user variables aren't treated by dzVents
Please read the dzVents wiki before replying if you are not sure.
There are several topics in this document describing how to trigger a script from a uservariable and about the use of them within the script.

Re: Use user variable to trigger event  [Solved]

Posted: Friday 21 June 2019 17:14
by waaren
jberinga wrote: Friday 21 June 2019 14:46 Can I and how can I use a user variable that triggers an event?
Hope someone can help me with this.
You can but not if the variable is updated by a Lua script. If the same variable is updated by dzVents or by a JSON it will trigger the script. This is designed like this in domoticz in the early days to prevent a deadlock situation and never changed.
dzVents does use another mechanism to update the uservar then Lua uses.

note to myself :D Please update the wiki with this information
[Edit] updated the wiki

Re: Use user variable to trigger event

Posted: Friday 21 June 2019 17:28
by waaren
jberinga wrote: Friday 21 June 2019 14:46 Can I and how can I use a user variable that triggers an event?
Hope someone can help me with this.
The converted Lua script could look like

Code: Select all

return {
    on = {
        timer = { 'every minute' }},
        
    execute = function(dz)
        noMotionCounter = dz.variables('nomotionCounter')
        statusSwitch = dz.devices('IemandThuis')
        mediaSwitch = dz.devices('EntertainmentUit')
        motionSwitch = dz.devices('SensorMotionKeuken')
        
        if motionSwitch.state == 'Off' and mediaSwitch.state == 'On' then
            noMotionCounter.set(noMotionCounter.value + 1)
            dz.log('No motion detected for ' .. (noMotionCounter.value + 1 ) .. ' minutes.',dz.LOG_INFO)
        else
            noMotionCounter.set(0)
        end    
        
        if statusSwitch.state == 'Off' and noMotionCounter.value > 15 then
            statusSwitch.switchOff()
        end
    end
}

Re: Use user variable to trigger event

Posted: Friday 21 June 2019 19:05
by boum
waaren wrote: Friday 21 June 2019 17:14
jberinga wrote: Friday 21 June 2019 14:46 Can I and how can I use a user variable that triggers an event?
Hope someone can help me with this.
You can but not if the variable is updated by a Lua script. If the same variable is updated by dzVents or by a JSON it will trigger the script. This is designed like this in domoticz in the early days to prevent a deadlock situation and never changed.
dzVents does use another mechanism to update the uservar then Lua uses.

note to myself :D Please update the wiki with this information
Thanks, that's good to know. dzVents were already there when I started using domoticz so I never bothered looking at "old-style" Lua scripts ;)