Notification get send multiple times.

Moderator: leecollings

Post Reply
Draakje
Posts: 140
Joined: Thursday 22 October 2015 21:14
Target OS: Linux
Domoticz version: 4.11539
Contact:

Notification get send multiple times.

Post by Draakje »

Hi all,

I am trying to create a LUA script.
The goal is when the thermostat is set to 25 or higher is reset the setpoint to 22 (the funny thing when you have children)

So I created this LUA script (added as device script)

It does do the job pretty well. but I get the notifications multiple times..
I have tried to build in some protection but without luck.

Any insights ?
(I know some lines could be done better :) feel free to point them out.. this is my real first self written script )

Code: Select all

--------------------------------------------------------------------------------
-- if RoomTemp Set is above 25 reset to default
--------------------------------------------------------------------------------
commandArray = {}
 
-- Define Default room tempature

local defaultTemp   = 22;   -- Degrees
 
--------------------------------------------------------------------------------
-- Implementation
--------------------------------------------------------------------------------

-- Check RoomTemp Set
if ( 
     (otherdevices['KamerTemp Set']    >= '25')
    ) then
    SetTemp = (otherdevices['KamerTemp Set'])
    
    -- check if not already running
    if (tonumber(uservariables["LuaScriptRunning"]) == 0) then
        commandArray['Variable:LuaScriptRunning'] = tostring(1)
        print("RoomTemp set to:" .. (SetTemp) .. "' Degrees")
     
        -- Sending notification (if possible)
        if (tonumber(uservariables["Notification sent"]) == 0) then
            commandArray['Variable:Notification sent'] = tostring(1)
            commandArray['SendNotification']='Reset Room Temp#Temp was set at '..SetTemp..' Degrees#0#sound#extradata#pushbullet'
        end
        
        -- Resetting Room tempature to defaultTemp   
        print("Resetting RoomTemp to:" .. (defaultTemp) .. "' Degrees")
        commandArray['SetSetPoint:38']=''..defaultTemp..''
        
        -- wait 5 seconds for command get set
        local start = os.clock()
        while os.clock() - start < 5 do
        end
        -- Reset User Variables
        commandArray['Variable:Notification sent'] = tostring(0)
        commandArray['Variable:LuaScriptRunning'] = tostring(0)
    else
        print("LUA script already running")
    end
end
return commandArray
Hardware: Raspberry Pi 3, OTGW, 433MHz Superheterodyne 3310 RF Link
Software: Ubuntu 16.04, Domoticz v3.5468, WiringPi, rc-switch
Draakje
Posts: 140
Joined: Thursday 22 October 2015 21:14
Target OS: Linux
Domoticz version: 4.11539
Contact:

Re: Notification get send multiple times.

Post by Draakje »

On a second note..

I did my testing from within domoticz which all seems fine...

But Now I tried using the thermostat and although the LUA script does fire up and loggin is telling me the temperature is set back, I do not see this happening on the thermostat. In fact I do keep seeing the burner is still active...

Am I missing something here?

Using OTGW via ser2net
Hardware: Raspberry Pi 3, OTGW, 433MHz Superheterodyne 3310 RF Link
Software: Ubuntu 16.04, Domoticz v3.5468, WiringPi, rc-switch
rrozema
Posts: 470
Joined: Thursday 26 October 2017 13:37
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Delft
Contact:

Re: Notification get send multiple times.

Post by rrozema »

I would suggest to use dzVents if you're already working on your lua skills. dzVents scripts are also written in lua, but with a lot of the boilerplate code already done by dzVents for you. Here's an (UNTESTED!) example to get you started:

Code: Select all

local THERMOSTAT_NAME = 'KamerTemp Set'

-- Define Default room tempature
local defaultTemp   = 22;   -- Degrees

return {
	on = {
		devices = {
			THERMOSTAT_NAME
		}
	},
	execute = function(domoticz, device)
	    if device.isDevice then
		    if device.name == THERMOSTAT_NAME then
		        if device.setPoint >= 25 then
		            device.updateSetPoint( defaultTemp )
		            domoticz.notify('Reset Room Temp','Temp was set at '.. tostring(device.setPoint) .. ' Degrees' , domoticz.PRIORITY_NORMAL)
		        end
		    end
		end
	end
}
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Notification get send multiple times.

Post by waaren »

Draakje wrote: Thursday 19 March 2020 23:58 Any insights ?
All domoticz event scripts (classic Lua, Blockly or dzVents) are bound to the rules of domoticz event system.

First thing to understand is that the domoticz event system is single threaded. This implies that max one script can be active at the same time. So if a Lua script is executing no other Lua, Blockly or dzVents script can be executing at the same time. That is including a second instance of the same script. In your case it means you don't have to check if the same script is active.
Another aspect is that all domoticz data is passed to the scripts at the start of the script and the commandArray is only passed back to domoticz when the script has finished. Changing a domoticz variable of device inside the script does not take place during execution time of the script. In your case it means that build a wait-loop before changing a variable back to an original value will not work as expected. Both the set and the change back will happen when the script has finished.

Because of the above, a wait-loop inside a script will block the entire event system for the duration of the loop. No other script can start while the script with the loop is executing. It can even lead to domoticz crashing all together.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Draakje
Posts: 140
Joined: Thursday 22 October 2015 21:14
Target OS: Linux
Domoticz version: 4.11539
Contact:

Re: Notification get send multiple times.

Post by Draakje »

waaren wrote: Friday 20 March 2020 10:08
Draakje wrote: Thursday 19 March 2020 23:58 Any insights ?
All domoticz event scripts (classic Lua, Blockly or dzVents) are bound to the rules of domoticz event system.

First thing to understand is that the domoticz event system is single threaded. This implies that max one script can be active at the same time. So if a Lua script is executing no other Lua, Blockly or dzVents script can be executing at the same time. That is including a second instance of the same script. In your case it means you don't have to check if the same script is active.
Another aspect is that all domoticz data is passed to the scripts at the start of the script and the commandArray is only passed back to domoticz when the script has finished. Changing a domoticz variable of device inside the script does not take place during execution time of the script. In your case it means that build a wait-loop before changing a variable back to an original value will not work as expected. Both the set and the change back will happen when the script has finished.

Because of the above, a wait-loop inside a script will block the entire event system for the duration of the loop. No other script can start while the script with the loop is executing. It can even lead to domoticz crashing all together.
Aha, good explanation! Thanks!
Hardware: Raspberry Pi 3, OTGW, 433MHz Superheterodyne 3310 RF Link
Software: Ubuntu 16.04, Domoticz v3.5468, WiringPi, rc-switch
Draakje
Posts: 140
Joined: Thursday 22 October 2015 21:14
Target OS: Linux
Domoticz version: 4.11539
Contact:

Re: Notification get send multiple times.

Post by Draakje »

rrozema wrote: Friday 20 March 2020 9:46 I would suggest to use dzVents if you're already working on your lua skills. dzVents scripts are also written in lua, but with a lot of the boilerplate code already done by dzVents for you. Here's an (UNTESTED!) example to get you started:

Code: Select all

local THERMOSTAT_NAME = 'KamerTemp Set'

-- Define Default room tempature
local defaultTemp   = 22;   -- Degrees

return {
	on = {
		devices = {
			THERMOSTAT_NAME
		}
	},
	execute = function(domoticz, device)
	    if device.isDevice then
		    if device.name == THERMOSTAT_NAME then
		        if device.setPoint >= 25 then
		            device.updateSetPoint( defaultTemp )
		            domoticz.notify('Reset Room Temp','Temp was set at '.. tostring(device.setPoint) .. ' Degrees' , domoticz.PRIORITY_NORMAL)
		        end
		    end
		end
	end
}
Thanks for the info. Will test your script!
Thanks
Hardware: Raspberry Pi 3, OTGW, 433MHz Superheterodyne 3310 RF Link
Software: Ubuntu 16.04, Domoticz v3.5468, WiringPi, rc-switch
Draakje
Posts: 140
Joined: Thursday 22 October 2015 21:14
Target OS: Linux
Domoticz version: 4.11539
Contact:

Re: Notification get send multiple times.

Post by Draakje »

rrozema wrote: Friday 20 March 2020 9:46 I would suggest to use dzVents if you're already working on your lua skills. dzVents scripts are also written in lua, but with a lot of the boilerplate code already done by dzVents for you. Here's an (UNTESTED!) example to get you started:
Ok I have used/test your script. It did not work at start. so I slightly modified it to:

Code: Select all

-- Define Default room tempature
local defaultTemp   = 22;   -- Degrees

return {
	on = {
		devices = {
			'KamerTemp Set'
		}
	},
	execute = function(domoticz, device)
	    
	    if (device.name == 'KamerTemp Set' and tonumber(device.setPoint) >= 25) then
	        print("RoomTemp set to:" .. tonumber(device.setPoint) .. " Degrees")
            device.updateSetPoint( defaultTemp )
            domoticz.notify('Reset Room Temp','Temp was set at '.. tostring(device.setPoint) .. ' Degrees' , domoticz.PRIORITY_NORMAL,nil,nil,domoticz.NSS_PUSHBULLET)
		end
	end
}
Now it does work, but Notifications still comes in twice..
Seems to me this is more a PushBullet problem then Domoticz

I say this because of the log output.

Code: Select all

Mar 20 21:11:48 domoticz domoticz[21654]: 2020-03-20 21:11:48.109  Status: dzVents: Info: Handling events for: "KamerTemp Set", value: "25.50"
Mar 20 21:11:48 domoticz domoticz[21654]: 2020-03-20 21:11:48.109  Status: dzVents: Info: ------ Start internal script: Reset_Room_Temp: Device: "KamerTemp Set (OpenTherm)", Index: 38
Mar 20 21:11:48 domoticz domoticz[21654]: 2020-03-20 21:11:48.109  Status: dzVents: RoomTemp set to:25.5 Degrees
Mar 20 21:11:48 domoticz domoticz[21654]: 2020-03-20 21:11:48.110  Status: dzVents: Info: ------ Finished Reset_Room_Temp
Mar 20 21:11:48 domoticz domoticz[21654]: 2020-03-20 21:11:48.116  Status: EventSystem: Script event triggered: /home/domoticz/domoticz/dzVents/runtime/dzVents.lua
Mar 20 21:11:48 domoticz domoticz[21654]: 2020-03-20 21:11:48.175  Status: OTGW: Setting Room SetPoint to: 22.0
Mar 20 21:11:48 domoticz domoticz[21654]: 2020-03-20 21:11:48.198  Status: OTGW: TT: 22.00
Mar 20 21:11:48 domoticz domoticz[21654]: 2020-03-20 21:11:48.487  Status: dzVents: Info: Handling events for: "KamerTemp Set", value: "22"
Mar 20 21:11:48 domoticz domoticz[21654]: 2020-03-20 21:11:48.488  Status: dzVents: Info: ------ Start internal script: Reset_Room_Temp: Device: "KamerTemp Set (OpenTherm)", Index: 38
Mar 20 21:11:48 domoticz domoticz[21654]: 2020-03-20 21:11:48.488  Status: dzVents: Info: ------ Finished Reset_Room_Temp
Mar 20 21:11:48 domoticz domoticz[21654]: 2020-03-20 21:11:48.808  Status: dzVents: Info: Handling events for: "KamerTemp Set", value: "22.00"
Mar 20 21:11:48 domoticz domoticz[21654]: 2020-03-20 21:11:48.809  Status: dzVents: Info: ------ Start internal script: Reset_Room_Temp: Device: "KamerTemp Set (OpenTherm)", Index: 38
Mar 20 21:11:48 domoticz domoticz[21654]: 2020-03-20 21:11:48.809  Status: dzVents: Info: ------ Finished Reset_Room_Temp
Mar 20 21:11:49 domoticz domoticz[21654]: 2020-03-20 21:11:49.071  Status: dzVents: Info: Handling events for: "KamerTemp Set", value: "22"
Mar 20 21:11:49 domoticz domoticz[21654]: 2020-03-20 21:11:49.071  Status: dzVents: Info: ------ Start internal script: Reset_Room_Temp: Device: "KamerTemp Set (OpenTherm)", Index: 38
Mar 20 21:11:49 domoticz domoticz[21654]: 2020-03-20 21:11:49.071  Status: dzVents: Info: ------ Finished Reset_Room_Temp
Mar 20 21:11:49 domoticz domoticz[21654]: 2020-03-20 21:11:49.303  Status: dzVents: Info: Handling events for: "KamerTemp Set", value: "22.00"
Mar 20 21:11:49 domoticz domoticz[21654]: 2020-03-20 21:11:49.303  Status: dzVents: Info: ------ Start internal script: Reset_Room_Temp: Device: "KamerTemp Set (OpenTherm)", Index: 38
Mar 20 21:11:49 domoticz domoticz[21654]: 2020-03-20 21:11:49.303  Status: dzVents: Info: ------ Finished Reset_Room_Temp
Mar 20 21:11:49 domoticz domoticz[21654]: 2020-03-20 21:11:49.536  Status: dzVents: Info: Handling events for: "KamerTemp Set", value: "22.00"
Mar 20 21:11:49 domoticz domoticz[21654]: 2020-03-20 21:11:49.536  Status: dzVents: Info: ------ Start internal script: Reset_Room_Temp: Device: "KamerTemp Set (OpenTherm)", Index: 38
Mar 20 21:11:49 domoticz domoticz[21654]: 2020-03-20 21:11:49.536  Status: dzVents: Info: ------ Finished Reset_Room_Temp
Mar 20 21:11:49 domoticz domoticz[21654]: 2020-03-20 21:11:49.811  Status: dzVents: Info: Handling events for: "KamerTemp Set", value: "22.00"
Mar 20 21:11:49 domoticz domoticz[21654]: 2020-03-20 21:11:49.811  Status: dzVents: Info: ------ Start internal script: Reset_Room_Temp: Device: "KamerTemp Set (OpenTherm)", Index: 38
Mar 20 21:11:49 domoticz domoticz[21654]: 2020-03-20 21:11:49.811  Status: dzVents: Info: ------ Finished Reset_Room_Temp
Mar 20 21:11:55 domoticz domoticz[21654]: 2020-03-20 21:11:55.952  Status: dzVents: Info: Handling events for: "KamerTemp Set", value: "22.00"
Mar 20 21:11:55 domoticz domoticz[21654]: 2020-03-20 21:11:55.953  Status: dzVents: Info: ------ Start internal script: Reset_Room_Temp: Device: "KamerTemp Set (OpenTherm)", Index: 38
Mar 20 21:11:55 domoticz domoticz[21654]: 2020-03-20 21:11:55.953  Status: dzVents: Info: ------ Finished Reset_Room_Temp
Mar 20 21:12:26 domoticz domoticz[21654]: 2020-03-20 21:12:26.016  Status: dzVents: Info: Handling events for: "KamerTemp Set", value: "22.00"
Mar 20 21:12:26 domoticz domoticz[21654]: 2020-03-20 21:12:26.017  Status: dzVents: Info: ------ Start internal script: Reset_Room_Temp: Device: "KamerTemp Set (OpenTherm)", Index: 38
I do see that the dzVents script is running every 30 sec. (have not looked it up but this seems normal behaviour)
I do not see the notification being sent twice..
Hardware: Raspberry Pi 3, OTGW, 433MHz Superheterodyne 3310 RF Link
Software: Ubuntu 16.04, Domoticz v3.5468, WiringPi, rc-switch
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Notification get send multiple times.

Post by waaren »

Draakje wrote: Friday 20 March 2020 21:16 I do see that the dzVents script is running every 30 sec. (have not looked it up but this seems normal behaviour)
This script should not execute every 30 seconds but only when the device is updated.

Can you try this (tested) one ?

Code: Select all

-- Define Default room temperature
local defaultTemp   = 22;   -- Degrees

return 
{
    on = 
    {
        devices = 
        {
            'KamerTemp Set',
        }
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = 'reset setpoint',
    },

    execute = function(dz, item)
        if item.setPoint >= 25 then
            dz.log('RoomTemp set to ' .. item.setPoint .. '  Degrees', dz.LOG_DEBUG)
            item.updateSetPoint( defaultTemp ).silent()
             
            dz.notify('Reset Room Temp','Temp was set at '.. item.setPoint .. ' Degrees' , dz.PRIORITY_NORMAL,nil,nil,dz.NSS_PUSHBULLET)
        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Draakje
Posts: 140
Joined: Thursday 22 October 2015 21:14
Target OS: Linux
Domoticz version: 4.11539
Contact:

Re: Notification get send multiple times.

Post by Draakje »

As for the dzVents script being run every 30 seconds,

As I see that the device is updated every 30 seconds I can only assume that is way the dzVents script is triggert every 30 seconds .
Hardware: Raspberry Pi 3, OTGW, 433MHz Superheterodyne 3310 RF Link
Software: Ubuntu 16.04, Domoticz v3.5468, WiringPi, rc-switch
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Notification get send multiple times.

Post by waaren »

Draakje wrote: Friday 20 March 2020 22:04 As for the dzVents script being run every 30 seconds,

As I see that the device is updated every 30 seconds I can only assume that is way the dzVents script is triggert every 30 seconds .
Correct. That is why the script I posted is using the option silent() so it will trigger it self.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Draakje
Posts: 140
Joined: Thursday 22 October 2015 21:14
Target OS: Linux
Domoticz version: 4.11539
Contact:

Re: Notification get send multiple times.

Post by Draakje »

waaren wrote: Friday 20 March 2020 22:08
Draakje wrote: Friday 20 March 2020 22:04 As for the dzVents script being run every 30 seconds,

As I see that the device is updated every 30 seconds I can only assume that is way the dzVents script is triggert every 30 seconds .
Correct. That is why the script I posted is using the option silent() so it will trigger it self.
Nevermind, found your updated script :)
Hardware: Raspberry Pi 3, OTGW, 433MHz Superheterodyne 3310 RF Link
Software: Ubuntu 16.04, Domoticz v3.5468, WiringPi, rc-switch
Draakje
Posts: 140
Joined: Thursday 22 October 2015 21:14
Target OS: Linux
Domoticz version: 4.11539
Contact:

Re: Notification get send multiple times.

Post by Draakje »

Draakje wrote: Friday 20 March 2020 22:38
waaren wrote: Friday 20 March 2020 22:08
Draakje wrote: Friday 20 March 2020 22:04 As for the dzVents script being run every 30 seconds,

As I see that the device is updated every 30 seconds I can only assume that is way the dzVents script is triggert every 30 seconds .
Correct. That is why the script I posted is using the option silent() so it will trigger it self.
Nevermind, found your updated script :)
I have tested your script with the silent() option but still, the dzVents script is triggert every 30 seconds (when OpenTherm Update gets in)
Hardware: Raspberry Pi 3, OTGW, 433MHz Superheterodyne 3310 RF Link
Software: Ubuntu 16.04, Domoticz v3.5468, WiringPi, rc-switch
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Notification get send multiple times.

Post by waaren »

Draakje wrote: Friday 20 March 2020 22:46 I have tested your script with the silent() option but still, the dzVents script is triggert every 30 seconds (when OpenTherm Update gets in)
Ok, I did not realize that the setPoint was updated every 30 seconds from an external source.

This updated script checks every 5 minutes and send a notification once every two hours

Code: Select all

-- Define Default room tempature
local defaultTemp   = 22;   -- Degrees
local frequencyDelay = 7200 -- two hours 

return 
{
    on = 
    {
        timer = 
        {
            'every 5 minutes',
        }
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = 'reset setpoint',
    },
    
    data =
    {
        lastSend = { initial = 0 },
    },
    

    execute = function(dz)
        device = dz.devices('KamerTemp Set')
        if device.setPoint >= 25 then
            dz.log('RoomTemp set to ' .. device.setPoint .. '  Degrees', dz.LOG_DEBUG)
            device.updateSetPoint( defaultTemp ).silent()
            if ( dz.data.lastSend + frequencyDelay ) < dz.time.dDate then     
                dz.notify('Reset Room Temp','Temp was set at '.. device.setPoint .. ' Degrees' , dz.PRIORITY_NORMAL,nil,nil,dz.NSS_PUSHBULLET)
                dz.data.lastSend = dz.time.dDate 
            end
        end
    end
}
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
rrozema
Posts: 470
Joined: Thursday 26 October 2017 13:37
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Delft
Contact:

Re: Notification get send multiple times.

Post by rrozema »

I think the issue is that your device gets updated via some script or plugin every 30 seconds. Normally, devices only get their value updated if the new value is actually different than that which it already has. Yours apparently gets updated every 30 seconds regardless if the value is the same.

I personally prefer to have my scripts triggered by device changes instead of via a timer. That way the response is quicker and les resources are wasted in my domoticz.

If your device gets updated too often, we can easily fix this by storing the previous value and comparing the new value to the previous value before we actually start our logic. Here's how you could do that (untested, so could include typos etc.):

Code: Select all

-- Define Default room temperature
local defaultTemp   = 22;   -- Degrees

return 
{
    on = 
    {
        devices = 
        {
            'KamerTemp Set',
        }
    },
    data = 
    {
	previous = {initial = nil}
    },

    logging =
    {
        level = domoticz.LOG_DEBUG,
        marker = 'reset setpoint',
    },

    execute = function(dz, item)
	if nil == dz.data.previous or item.setPoint ~= dz.data.previous then
	    dz.data.previous = item.setPoint
	
            if item.setPoint >= 25 then
                dz.log('RoomTemp set to ' .. item.setPoint .. '  Degrees', dz.LOG_DEBUG)
                item.updateSetPoint( defaultTemp ).silent()
             
                dz.notify('Reset Room Temp',
				'Temp was set at '.. item.setPoint .. ' Degrees' ,
 				dz.PRIORITY_NORMAL,
				nil,
				nil,
				dz.NSS_PUSHBULLET)
            end
        end
    end
}
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Notification get send multiple times.

Post by waaren »

rrozema wrote: Saturday 21 March 2020 0:17 I personally prefer to have my scripts triggered by device changes instead of via a timer. That way the response is quicker and les resources are wasted in my domoticz.
In general this is true but not when the domoticz device is updated every 30 seconds by an external source
If your device gets updated too often, we can easily fix this by storing the previous value and comparing the new value to the previous value before we actually start our logic. Here's how you could do that (untested, so could include typos etc.):
Please note that comparing values in this way requires persistent storage. Persistent storage is implemented in dzVents using OS files. So every time the script is started by a device update, dzVents will read the contents of the OS file into a table and to make it even more resource hungry, when the script has finished the OS file will be updated by writing the table back to it.
No need to tell you that reading of an OS file consumes a relative large amount of resources and writing- even more.

So in this case the approach using a time trigger is much less resource hungry then by using a device trigger
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
rrozema
Posts: 470
Joined: Thursday 26 October 2017 13:37
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Delft
Contact:

Re: Notification get send multiple times.

Post by rrozema »

waaren wrote: Saturday 21 March 2020 8:15
rrozema wrote: Saturday 21 March 2020 0:17 I personally prefer to have my scripts triggered by device changes instead of via a timer. That way the response is quicker and les resources are wasted in my domoticz.
In general this is true but not when the domoticz device is updated every 30 seconds by an external source
It actually does respond quicker: Your timer can't go any faster than once a minute, while the device gets updated twice in that time. Plus, if you poll once a minute, you're on average 15 seconds, and at most 30 seconds, after the last device got updated. In other words: by using the timer option you introduce an additional delay of on average 45 seconds.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Notification get send multiple times.

Post by waaren »

rrozema wrote: Saturday 21 March 2020 11:04
waaren wrote: Saturday 21 March 2020 8:15
rrozema wrote: Saturday 21 March 2020 0:17 I personally prefer to have my scripts triggered by device changes instead of via a timer. That way the response is quicker and les resources are wasted in my domoticz.
In general this is true but not when the domoticz device is updated every 30 seconds by an external source
It actually does respond quicker: Your timer can't go any faster than once a minute, while the device gets updated twice in that time. Plus, if you poll once a minute, you're on average 15 seconds, and at most 30 seconds, after the last device got updated. In other words: by using the timer option you introduce an additional delay of on average 45 seconds.
My comment was aimed at your statement
les resources are wasted
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
rrozema
Posts: 470
Joined: Thursday 26 October 2017 13:37
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Delft
Contact:

Re: Notification get send multiple times.

Post by rrozema »

waaren wrote: Saturday 21 March 2020 11:34 My comment was aimed at your statement
les resources are wasted
ah, ok. I wasn't really clear there I must admit. What I wanted to say was that I -in general- favor using device triggers over using timer triggers. If possible I try to avoid using timer triggers, as they tend to make logic complicated. For examply by having to use all sorts of checks: does the action need to be triggered? has the action already been triggered? , etc.. Plus, the code gets called many times without anything having to be done. Device triggers instead have these nice qualities that they only fire when a change has been made, and this happens right after that change has been made, with no (significant) delays.

Now to this specific case: yes, it does use more resources to read the previous from disk and later have it written again instead of using a 1 minute timer. But this solution is a work around for a bug in the plugin or Domoticz affecting only this single device. If it is only this one device that we need this work around for, I think we can live with that penalty of some extra io. But I agree, I wouldn't recommend using this as a pattern for scripts on all triggers. So this is why I still suggest to store the previous value in the data variable and compare the new value to it: it has the advantage of having the quick response of the device trigger plus the trigger code closely resembles that of the preferred pattern to use for all (correctly working) devices. The latter keeps the -script- code clear to read and understand for many and thus avoids making mistakes.
Draakje
Posts: 140
Joined: Thursday 22 October 2015 21:14
Target OS: Linux
Domoticz version: 4.11539
Contact:

Re: Notification get send multiple times.

Post by Draakje »

Ok so I finalised my dzVents script.

It is working pretty well (although I do get some multiple messages when the SetTemp is instant reset to the default. But well .. I just live with it.


for whom it may concern.
Here is my script.


Code: Select all

-- Define Default room tempature
local defaultTemp   = 22;   -- Degrees


return {
	on = {
		devices = {
			'KamerTemp Set'
		}
	},
	execute = function(domoticz, device)
	    local RoomTemp = tonumber(string.format("%.1f", device.setPoint))
	    local PreviousTemp = tonumber(domoticz.variables('PreviousRoomTemp').value)
	    
	    if ((RoomTemp) >= 24) then
	        print("RoomTemp set to:" .. (RoomTemp) .. " Degrees")
            device.updateSetPoint( defaultTemp )
            domoticz.variables('PreviousRoomTemp').set(defaultTemp)
            domoticz.notify('Reset Room Temp','Temp was set at '.. (RoomTemp) .. ' Degrees' , domoticz.PRIORITY_NORMAL,nil,nil,domoticz.NSS_PUSHBULLET)
	    elseif ((RoomTemp) > (PreviousTemp) and (RoomTemp) > (defaultTemp)) then
	         print("RoomTemp set to:" .. tonumber(device.setPoint) .. " Degrees")
	         domoticz.variables('PreviousRoomTemp').set(RoomTemp)
	         domoticz.notify('Room Temp Changed','Temp was set at '.. (RoomTemp) .. ' Degrees' , domoticz.PRIORITY_NORMAL,nil,nil,domoticz.NSS_PUSHBULLET)
	    else
	        domoticz.variables('PreviousRoomTemp').set(RoomTemp)
	        print("Nothing to Do.....")
	    end
	end
}
The Script function is to watch the SetPoint for the Thermostat.
If the temp is set higher than 24 the temp gets reset to default (22) and a notification is sent.
If the temp is set higher than 22 and is higher than the previous setting a notification is sent.
if the temp is set lower than 22 or lower than the previous temp setting the User Var gets updated. No notification is sent
Hardware: Raspberry Pi 3, OTGW, 433MHz Superheterodyne 3310 RF Link
Software: Ubuntu 16.04, Domoticz v3.5468, WiringPi, rc-switch
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest