You first need to make-activate 2 uservariables.
-begin INTEGER value=0
-begintijd INTEGER value=0
Then you need to make a virtual device Waterflow
- go to HARDWARE and make another virtualsensor "Waterflow" at Watermeter and use this as 'waterflow'
After this, you can use this script
jIDX is number of Waterflow device.
BE CAREFUL... AFTER 5 minutes of inactive watermeter, the flow resets to zero. When you use water again and the switch (on the watermeter) is almost directly ON, you can read a high flow-result, after the second liter the reading will be correct. When you turn off the water the flow will give the last value, but resets after 5 minutes. Will you turn on the water after about 4 minutes, you will get a low flow-value, because you only used that liter in the last 4 minutes. So the flow is 0,25liter/min ( 1 liter in 4 minutes)
Hope you will understand the start and end values of the flowmeter. When you use water for a longer time, the readings will be spot on.
EDITED 26-8 18:42
Edited line56 (putted - 300 in it), so the flowvalue isn't very high when you turn on water and metal plate is right in front of sensor. So no high values like 120liters/min anymore
Code: Select all
commandArray = {}
-- IDX Waterflow
jIDX = 404
-- Begintijd in uservariables zetten
if (uservariables['begin'] == 0)
then
-- print ("begin instellen")
commandArray['Variable:begin'] = tostring(1)
commandArray['Variable:begintijd']= tostring(os.time())
end
-- Decimalen
function round(num, numDecimalPlaces)
local mult = 10^(numDecimalPlaces or 0)
return math.floor(num * mult + 0.5) / mult
end
-- timedifference
functiontimedifference(s)
year = string.sub(s, 1, 4)
month = string.sub(s, 6, 7)
day = string.sub(s, 9, 10)
hour = string.sub(s, 12, 13)
minutes = string.sub(s, 15, 16)
seconds = string.sub(s, 18, 19)
t1 = os.time()
t2 = os.time{year=year, month=month, day=day, hour=hour, min=minutes, sec=seconds}
difference = os.difftime (t1, t2)
return difference
end
-- Script
if (devicechanged['GPIO Watermeter'] == 'Off')
then
-- print ("Watermeter gaat naar Off")
btijd = uservariables['begintijd']
duur = os.time() - btijd
lpm = 60 /duur
-- print (lpm)
lpm = round(lpm, 2)
-- print (btijd)
-- print (os.time() - btijd)
-- print (os.time())
-- print (duur)
-- print (lpm)
commandArray['Variable:begintijd']= tostring(os.time())
commandArray['UpdateDevice'] = ''..jIDX..'|0|'..lpm..''
else
-- Keep a live device and set flow to zero
if (timedifference(otherdevices_lastupdate["GPIO Watermeter"]) > 300)
then
-- print("tijd langer dan 300")
flow = 0
-- commandArray['Variable:begintijd']= tostring(os.time() - 300)
commandArray['UpdateDevice'] = ''..jIDX..'|0|'..flow..''
end
end
return commandArray