Excess Water Flow Alert

From Domoticz
Jump to navigation Jump to search

Purpose

This script is used to send a notification message if water is running for an extensive period of time. This could be caused by a leaking or broken faucet or when people forget to turn off the water.

Domoticz Setup - switches, variables, version

There is a sendor called "Water Flow" that displays the current water flow (in liters/minute). Sensor name can be changed in script below.

Installation instructions

Copy the script below in your domoticz\scripts\lua\ folder. The script name should start with script_time_ to ensure Domoticz calls the script every minute. The name used here is: script_time_waterrun.lua.

Also create 2 variables of type Integer with a value of 0 below Setup -> More options -> Custom Variables.
Names should be waterflow_on and waterflow_2long.

Script with comments

-- script_time_waterrun.lua - created 6 dec 2016 BM
-- checks if we have water running for an extensive period of time
-- use variable 'waterflow_on' for tracking if we have waterflow on all the time (set 1 or 0)
-- use variable 'waterflow_2long' for tracking if we have sent excess message

-- trigger is the number of minutes when the notification alarm will be sent
trigger = 30

commandArray = {}

function string2date(hst)
 -- returns a date from a string like 2016-07-11 17:23:12
 local year = string.sub(hst, 1, 4)
 local month = string.sub(hst, 6, 7)
 local day = string.sub(hst, 9, 10)
 local hour = string.sub(hst, 12, 13)
 local minutes = string.sub(hst, 15, 16)
 local seconds = string.sub(hst, 18, 19)
 return os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
end

-- first check if we actually get updates all the time (needs to be at least 10 minutes ago) - sensor may be offline
t1 = os.time()
s = otherdevices_lastupdate['Water Flow']
t2 = string2date(s)
difference = (os.difftime (t1, t2))
flow = otherdevices['Water Flow']
print('Water run check ' ..  s .. ' Seconds ago=' .. difference .. ' Flow=' .. flow)

if (difference < 600) then
  -- we got updates from the water sensor, so now check the current flow
  if (tonumber(flow) > 0) then
    -- we have flowing water, check how long we have it
    if (tonumber(uservariables['waterflow_on']) == 0) then
      print('Waterflow just turned on')
      commandArray['Variable:waterflow_on'] = '1'
    else
      -- flow was already on, now see how long ago this was
      s = uservariables_lastupdate['waterflow_on']
      difference = os.difftime(t1, string2date(s))
      print('Water on since: ' .. s .. ' -> ' .. difference .. ' sec')
      if (difference >= trigger*60 and tonumber(uservariables['waterflow_2long'])==0) then
        -- too long now. make sure we only send notification once
        min = difference / 60
        print('Excess Waterflow notification (>' .. min .. ' min)')
        commandArray['SendNotification']='Excess Waterflow (>' .. min .. ' min)'
        commandArray['Variable:waterflow_2long'] = '1'
      end
    end
  else
    -- we no longer have flow - send message if we had excess before
    if (tonumber(uservariables['waterflow_2long']) > 0) then
      s = uservariables_lastupdate['waterflow_on']
      difference = os.difftime(t1, string2date(s))
      min = difference / 60
      print('Excess waterflow stop notification after ' .. min .. ' min')
      commandArray['SendNotification']='Excess Waterflow stopped. Total ' .. min .. 'min.'
      commandArray['Variable:waterflow_2long'] = '0'
    end
    commandArray['Variable:waterflow_on'] = '0'
  end
else
  if (tonumber(uservariables['waterflow_on']) > 0) then
    -- no updates > 10 minutes, consider we are stopped
    print('Consider waterflow stopped (no sensor data >10 min)')
    if (tonumber(uservariables['waterflow_2long']) > 0) then
      commandArray['SendNotification']='Excess Waterflow stopped (no sensor data >10 min)'
      commandArray['Variable:waterflow_2long'] = '0'
    end
    commandArray['Variable:waterflow_on'] = '0'
  end
end

return commandArray