Use user variable to trigger event  [Solved]

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

Moderator: leecollings

Post Reply
jberinga
Posts: 60
Joined: Tuesday 11 August 2015 14:20
Target OS: NAS (Synology & others)
Domoticz version: 2024.7
Location: The Netherlands
Contact:

Use user variable to trigger event

Post 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.
Lebo2d9
Posts: 139
Joined: Tuesday 06 September 2016 20:39
Target OS: Raspberry Pi / ODroid
Domoticz version: L stab
Location: Belgium
Contact:

Re: Use user variable to trigger event

Post by Lebo2d9 »

I think user variables aren't treated by dzventz
Domoicz on RPI3 (wifi) directly connected 3x ds18b20 for CV temp, Evohome (9 zone), 1 remote 220V switch based on ESP-12. RFXtrx433E, 16x AMST-606, 5 Somfy RTS motors
Domoticz on RPI3(wifi) as slave for terraruim control
More to come
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: Use user variable to trigger event

Post 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
  -- ....
jberinga
Posts: 60
Joined: Tuesday 11 August 2015 14:20
Target OS: NAS (Synology & others)
Domoticz version: 2024.7
Location: The Netherlands
Contact:

Re: Use user variable to trigger event

Post 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.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Use user variable to trigger event

Post 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.
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: Use user variable to trigger event  [Solved]

Post 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
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: Use user variable to trigger event

Post 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
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
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: Use user variable to trigger event

Post 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 ;)
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest