Thanks to everyone in this forum for making all this possible.
I have now also successfully added my watermeter to Domoticz.
1 bug that keeps popping up and is really annoying is that whenever you change a device or lua script, the log will show :
Code: Select all
2018-07-27 09:19:58.007 Status: EventSystem: reset all device statuses...
And then the LUA script starts throwing errors.
Code: Select all
2018-07-27 09:20:02.164 Error: EventSystem: in WaterMeterLuav2: [string "commandArray = {}
..."]:18: attempt to perform arithmetic on global 'sWaterUsage' (a nil value)
The only way to get it working again (to my knowledge) is to fill the uservariable with the total water usage.
This is of course quite annoying because if any water is used during this error period, its usage is not recorded. That means you have to check on the actual meter. Sadly, in my case, the meter is quite inaccessible.
The error is thrown on line 18, Line 18 is :
Code: Select all
sWaterUsagePrint = tonumber(sWaterUsage / 1000);
Anyone have any idea how I (and the others facing the same problem) can fix this?
My lua script in full :
Code: Select all
commandArray = {}
-- Set IDX of Watermeter
iIDX = 256
-- Set Watermeter based on user variable "WaterMeter"
if ( uservariables["WaterMeter"] > 0 ) then
print("Water usage is set to " .. uservariables["WaterMeter"] / 1000 .. "m3 by user")
commandArray['UpdateDevice'] = ''..iIDX..'|0|'..uservariables["WaterMeter"]..''
commandArray['Variable:WaterMeter'] = tostring(0)
end
-- Water usage
-- Retrieve value from water meter device:
sWaterUsage = otherdevices_svalues['Waterverbruik']
-- To have a better readable format, divide number by 1000:
sWaterUsagePrint = tonumber(sWaterUsage / 1000);
-- calculation is done with the unmodified water value
sWaterUsage = tonumber(sWaterUsage);
-- For Debuging
--print("Water usage until now is " .. sWaterUsagePrint .. "m3 ")
--print('GPIO Watermeter = '..otherdevices['GPIO Watermeter'])
function timedifference(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
if (devicechanged['GPIO Watermeter'] == 'Off')
then
sWaterUsageTot = (sWaterUsage + 1)
print("Water usage is set to " .. sWaterUsageTot / 1000 .. "m3 ")
commandArray['UpdateDevice'] = ''..iIDX..'|0|'..sWaterUsageTot..''
else
-- Keep a live device
if (timedifference(otherdevices_lastupdate["Waterverbruik"]) > 300)
then
print("Water usage is still " .. sWaterUsage / 1000 .. "m3 ")
commandArray['UpdateDevice'] = ''..iIDX..'|0|'..sWaterUsage..''
end
end
return commandArray
Thanks for taking the time to read this!