Time calculating to check temp sensor active

Python and python framework

Moderator: leecollings

Post Reply
Tukker
Posts: 673
Joined: Sunday 01 November 2015 22:45
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Twente
Contact:

Time calculating to check temp sensor active

Post by Tukker »

Hi,
I have a thermostat script which uses a temp sensor.
2 days ago the battery of the temp sensor was empty and coming home the thermostat script had fired up the boiler and the temperature had risen far above normal.

Now I want to switch the heating off when no valid temperature is received, in other words, when there is too much a time difference between actual time and LastUpdate
Output for LastUpdate:

Code: Select all

LastUpdate	"2020-04-25 22:52:40"
On the other hand I have

Code: Select all

(now.strftime("%Y-%m-%d %H:%M:%S")
I want to activate some kind of system-alive-check either in python or in dzVents.
I'm not able to find the proper calculating trick in python nor am I able to create such a thing in dzVents.

Can someone help me with the code.
Bugs bug me.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Time calculating to check temp sensor active

Post by waaren »

HvdW wrote: Saturday 25 April 2020 23:36 Now I want to switch the heating off when no valid temperature is received, in other words, when there is too much a time difference between actual time and LastUpdate
..
I want to activate some kind of system-alive-check either in python or in dzVents.
.. nor am I able to create such a thing in dzVents.
in dzVents the code snippet to do this could look like.

Code: Select all

local mySensor = domoticz.devices('temperature sensor')
local lastUpdated = mySensor.lastUpdate.minutesAgo

if lastUpdated > 15 then
   domoticz.log('Last update for '  .. mySensor.name .. 'was ' .. lastUpdated .. ' minutes ago. Switching off heating system', domoticz.LOG_ERROR )
   -- more code to switch off heating
end
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
Tukker
Posts: 673
Joined: Sunday 01 November 2015 22:45
Target OS: Raspberry Pi / ODroid
Domoticz version: 2023.2
Location: Twente
Contact:

Re: Time calculating to check temp sensor active

Post by Tukker »

Thanks @waaren
So in fact it's a System_Alive_Check where only

Code: Select all

device.state == 'Off' and 
is omitted

Code: Select all

-- this script can be used in conjunction with the System-alive checker plug-in.
-- the plugin pings a list of devices and creates switches for these devices
-- the reason for this script is to not treat devices as dead immediately after they
-- do not respond. More often than not, the second ping atempt does work. So only
-- if the device has been not responding for a while AFTER it is been presumed dead
-- then this script will notify you
-- put the names of these switches in the devicesToCheck list
-- you may have to tweak the THRESHOLD depending on the check interval

local THRESHOLD = 120 -- minutes
local devicesToCheck = 
        {
            'TFA1', 
            'TFA2',
            'Oregon',
        }

return 
{
    on = 
    {
        devices = devicesToCheck,

        timer = 
        { 
            'every other hour',   
        },
    },

    data = 
    {
        notified = { initial = {} },
    },

    logging = 
    {
        level = domoticz.LOG_ERROR, -- change from LOG_DEBUG to LOG_ERROR when script executes without problems
        marker = 'notify',
    },

    execute = function(dz, item)
        if item.isTimer then
            
            for index, deviceName in ipairs(devicesToCheck) do
                local device = dz.devices(deviceName) 
                --dz.notify('notifyer achter de eerste if.',dz.PRIORITY_HIGH)
                dz.log('device name  ' .. device.name, dz.LOG_DEBUG) 
                dz.log('device state ' .. device.state, dz.LOG_DEBUG)        
                dz.log('device lastUpdate ' .. device.lastUpdate.minutesAgo, dz.LOG_DEBUG)        
                dz.log('device notified ' .. tostring(dz.data.notified[deviceName]), dz.LOG_DEBUG)        
                                
                if device.lastUpdate.minutesAgo >= THRESHOLD and dz.data.notified[deviceName] ~= true  then
                    --dz.notify('notifyer achter de tweede if.',dz.PRIORITY_HIGH)
                    dz.log('Idle device ' .. deviceName, dz.LOG_DEBUG)        
                    dz.notify('notifyer', deviceName .. ' is not responding anymore.',dz.PRIORITY_HIGH)
                    dz.data.notified[deviceName] = true
                end
            end
        else
            dz.data.notified[item.name] = false
        end
    end
}
Can you explain me how come it displays temp-sensors every 30 seconds while it does not in the original System_Alive_Checker.

Code: Select all

2020-04-26 12:52:41.579 Status: dzVents: Info: Handling events for: "TFA1", value: "20.4;49;1"
2020-04-26 12:52:42.031 (Slimme meter) P1 Smart Meter (Stroom)
2020-04-26 12:52:46.306 (RFXCOM) Temp + Humidity (TFA2)
2020-04-26 12:52:46.451 Status: dzVents: Info: Handling events for: "TFA2", value: "20.4;47;1"
2020-04-26 12:52:52.023 (Slimme meter) P1 Smart Meter (Stroom)
2020-04-26 12:53:02.029 (Slimme meter) P1 Smart Meter (Stroom)
2020-04-26 12:53:12.036 (Slimme meter) P1 Smart Meter (Stroom)
2020-04-26 12:53:22.026 (Slimme meter) P1 Smart Meter (Stroom)
2020-04-26 12:53:24.437 (RFXCOM) Temp + Humidity (TFA1)
2020-04-26 12:53:24.586 Status: dzVents: Info: Handling events for: "TFA1", value: "20.4;49;1"
2020-04-26 12:53:31.310 (RFXCOM) Temp + Humidity (TFA2)
2020-04-26 12:53:31.433 Status: dzVents: Info: Handling events for: "TFA2", value: "20.4;47;1"
2020-04-26 12:53:32.028 (Slimme meter) P1 Smart Meter (Stroom)
2020-04-26 12:53:40.991 (Ranninkstraat 52) Temp + Humidity + Baro (Ranninkstraat)
2020-04-26 12:53:40.994 (Ranninkstraat 52) Wind (Unknown)
2020-04-26 12:53:40.998 (Ranninkstraat 52) Rain (Unknown)
2020-04-26 12:53:42.038 (Slimme meter) P1 Smart Meter (Stroom)
2020-04-26 12:53:52.044 (Slimme meter) P1 Smart Meter (Stroom)
2020-04-26 12:53:56.687 (RFXCOM) Temp (Oregon)
2020-04-26 12:53:56.839 Status: dzVents: Info: Handling events for: "Oregon", value: "20.2"
2020-04-26 12:54:02.035 (Slimme meter) P1 Smart Meter (Stroom)
2020-04-26 12:54:07.443 (RFXCOM) Temp + Humidity (TFA1)
2020-04-26 12:54:07.562 Status: dzVents: Info: Handling events for: "TFA1", value: "20.4;49;1"
2020-04-26 12:54:12.041 (Slimme meter) P1 Smart Meter (Stroom)
2020-04-26 12:54:16.313 (RFXCOM) Temp + Humidity (TFA2)
2020-04-26 12:54:16.467 Status: dzVents: Info: Handling events for: "TFA2", value: "20.4;47;1"
2020-04-26 12:54:22.050 (Slimme meter) P1 Smart Meter (Stroom)
2020-04-26 12:54:27.930 (RFXCOM) Temp (Oregon)
2020-04-26 12:54:28.066 Status: dzVents: Info: Handling events for: "Oregon", value: "20.2"
2020-04-26 12:54:32.039 (Slimme meter) P1 Smart Meter (Stroom)
2020-04-26 12:54:42.045 (Slimme meter) P1 Smart Meter (Stroom)
2020-04-26 12:54:50.435 (RFXCOM) Temp + Humidity (TFA1)
2020-04-26 12:54:50.586 Status: dzVents: Info: Handling events for: "TFA1", value: "20.4;49;1"
2020-04-26 12:54:52.052 (Slimme meter) P1 Smart Meter (Stroom)
Is that because this

Code: Select all

device.state == 'Off' and 
is omitted?

I have found this one as well:

Code: Select all

local devicesToCheck = {
	['TFA1'] = 80, -- threshold in seconds
	['TFA2'] = 90,
	['Oregon'] = 90
}


return {
	active = true,
	on = { timer = { 'every 3 minutes' } },
	data = { notified = { initial = {} } },
	execute = function(dz)
	
		local notified = dz.data.notified -- short reference
		local toCheck = devicesToCheck
		
		for deviceName, threshold in pairs(toCheck) do
			local lastUpdate = dz.devices(deviceName).lastUpdate.secondsAgo
			if (lastUpdate > threshold and notified[deviceName] == nil) then
				dz.log(deviceName .. 'is inactive for ' .. lastUpdate .. 's. Threshold=' .. threshold, dz.LOG_ERROR)
				notified[deviceName] = lastUpdate -- mark as notified
			else
				notified[deviceName] = nil -- remove from notified list
			end
		end
	end
}
Which one is more elegant in the way that it asks less resources from domoticz/rasperrypi.

Extra question: Can the system_alive_check be adapted in a way that it checks all domoticz connected devices (WiFi and 433/866 MHz) in one loop for being up and running?
Bugs bug me.
Post Reply