pvangorp wrote: ↑Saturday 02 September 2017 20:53
I removed all crontab and init.d stuff and created a LUA SCRIPT. In my case it's more accurate and runs directly from Domoticz. I had both init.d and LUA active and the LUA is accurate where the init.d script was of sometimes.
My LUA Script
commandArray = {}
-- Water usage
-- Retrieve value from water meter device:
sWaterUsage = otherdevices_svalues['Watermeter']
-- 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']);
if (devicechanged['GPIO Watermeter'] == 'Off')
then
sWaterUsageTot = (sWaterUsage + 1)
print("Water usage is now " .. sWaterUsageTot / 1000 .. "m3 ");
commandArray['UpdateDevice'] = '34|0|'..sWaterUsageTot..''
end
return commandArray
The red colored parts need to me reconfigured for your case.
To set the initial meter values http://<<YOUR DOMOTICZ IP>>/json.htm?type=command¶m=udevice&idx=34&svalue=999999
Where "999999" = 999.999m3
Make sure to set LUA to DEVICE instead off ALL
Hi pvangorp,
Thnx for the great script! I works very well and my meter is much more accurate now!
just one question.
I get an error in line 15 of your scripts mentioning:
Error: EventSystem: in /home/pi/domoticz/scripts/lua/script_device_Watermeter.lua: /home/pi/domoticz/scripts/lua/script_device_Watermeter.lua:15: attempt to concatenate field 'GPIO Watermeter' (a nil value)
print('GPIO Watermeter = '..otherdevices['GPIO Watermeter'])
Prints to the log the state of the device GPIO Watermeter. That's all. Did you do all my steps exactly?
Last edited by pvangorp on Thursday 05 October 2017 4:25, edited 1 time in total.
Ordered the NPN sensor and connected it to the GPIO of the Pi. Works like a charm.
I used the NPN sensor KSOL DC6-36V 300mA NPN GEEN 3-wire 4mm Tubular Inductieve Proximity Sensor Switch LJ12A3-4-Z-BX and both of the LUA scripts pvangorp wrote.
One question: now i see the GPIO Watermeter switch at the switches tab.
The switch's only purpose is to give the LUA script a pulse when the watermeter rotates.
When i set the state of the switch to "inactive" the LUA script doesn't work anymore.
Is there an option to remove it from the switches tab and let the lua script function?
No, there isn't as far as I know and it won't work when the device is inactive. You can only hide it on the dashboard by disable the star beside the icon.
Add it to the LUA script that reads the value from the watermeter and use JSON to set the value on the SLAVE using LUA.
os.execute('wget http://<<YOUR SLAVE DOMOTICZ IP>>/json.htm?type=command¶m=udevice&idx=<<<YOUR SLAVE IDX>>>&svalue='..sWaterUsageTot..' -O /dev/null')
You can add it after the part with: commandArray['UpdateDevice'] = '34|0|'..sWaterUsageTot..'
pvangorp wrote: ↑Wednesday 11 October 2017 10:50
Add it to the LUA script that reads the value from the watermeter and use JSON to set the value on the SLAVE using LUA.
os.execute('wget http://<<YOUR SLAVE DOMOTICZ IP>>/json.htm?type=command¶m=udevice&idx=<<<YOUR SLAVE IDX>>>&svalue='..sWaterUsageTot..' -O /dev/null')
You can add it after the part with: commandArray['UpdateDevice'] = '34|0|'..sWaterUsageTot..'
Thanks, but I have a feeling that I wasn't clear enough - my mistake, sorry
I'd like to have the data on my Master PI (my main device with all other devices and stuff) from the Slave PI (which's sole function is to read the watermeter - it's physically placed in the metering cabinet outside my flat). If I'm not mistaken the above LUA's not for that Or am I wrong?
So basically I'd like to display the data from the Slave on my Master.
pvangorp wrote: ↑Wednesday 11 October 2017 19:52
That works the same. Replace slave with master and visa versa. Assuming they have a network connection.
Oh I see! Have to put my master pi's IP address, create a virtual counter sensor and put that virtual sensor's IDX to the IDX field.
Hi Louis22, i think i might have the same question. You probably now have all the data on a remote pi? In my case, I can’t get the info to the master Pi through the “master-slave” functionality but am I correct that with using the solution above, this is possible? Thnx!
pvangorp wrote: ↑Wednesday 11 October 2017 19:52
That works the same. Replace slave with master and visa versa. Assuming they have a network connection.
Oh I see! Have to put my master pi's IP address, create a virtual counter sensor and put that virtual sensor's IDX to the IDX field.
Trigun wrote: ↑Thursday 12 October 2017 10:29
Hi Louis22, i think i might have the same question. You probably now have all the data on a remote pi? In my case, I can’t get the info to the master Pi through the “master-slave” functionality but am I correct that with using the solution above, this is possible? Thnx!
Trigun wrote: ↑Thursday 12 October 2017 10:29
Hi Louis22, i think i might have the same question. You probably now have all the data on a remote pi? In my case, I can’t get the info to the master Pi through the “master-slave” functionality but am I correct that with using the solution above, this is possible? Thnx!
Sent from my iPhone using Tapatalk
I believe so, stated in the above comment
Ok, i am really going to sound like a Noob here but I am lost.
what exactly do I need to copy and past into the Lua script?
Trigun wrote: ↑Thursday 12 October 2017 10:29
Hi Louis22, i think i might have the same question. You probably now have all the data on a remote pi? In my case, I can’t get the info to the master Pi through the “master-slave” functionality but am I correct that with using the solution above, this is possible? Thnx!
Sent from my iPhone using Tapatalk
I believe so, stated in the above comment
Ok, i am really going to sound like a Noob here but I am lost.
what exactly do I need to copy and past into the Lua script?
thnx in advanced!!
thnx in advanced
Edit: is there a chance you can post an example of how it should look like?
pvangorp wrote: ↑Wednesday 11 October 2017 19:52
That works the same. Replace slave with master and visa versa. Assuming they have a network connection.
Oh I see! Have to put my master pi's IP address, create a virtual counter sensor and put that virtual sensor's IDX to the IDX field.
if (devicechanged['Watermeter GPIO'] == 'Off')
then
sWaterUsageTot = (sWaterUsage + 1)
print("Water usage is set to " .. sWaterUsageTot / 1000 .. "m3 ");
commandArray['UpdateDevice'] = '6|0|'..sWaterUsageTot..''
os.execute('wget http://192.168.0.19:8080/json.htm?type=command¶m=udevice&idx=3571&svalue='..sWaterUsageTot..' -O /dev/null')
else
-- Keep a live device
if (timedifference(otherdevices_lastupdate["Watermeter"]) > 300)
then
print("Water usage is still " .. sWaterUsage / 1000 .. "m3 ");
commandArray['UpdateDevice'] = '6|0|'..sWaterUsage..''
end
end
return commandArray
what I did:
created a new virtual counter on MASTER pi (192.168.0.19) with IDX 3571
placed the requested line on SLAVE Pi (see above)
3571 does not get updated, it stays zero (well, actually 1, I initiated a manual update).
What happens when you execute http://192.168.0.19:8080/json.htm?type= ... value=9999 from your browser. Does it update your Master? You should also add "os.execute('wget http://192.168.0.19:8080/json.htm?......" to the keep alive part otherwise it shows offline after a time of no water usage. Instead of "os.execute" you can also try "commandArray["OpenURL"] = "http://....". This shouldn't make a difference but you can try.
Last edited by pvangorp on Friday 13 October 2017 10:19, edited 2 times in total.
I tried it as well, with the same result as LouiS22. I do see it in the logging though. But is stays 0. I’ll try when it get home this afternoon, thnx for you help!
pvangorp wrote: ↑Friday 13 October 2017 10:09
What happens when you execute http://192.168.0.19:8080/json.htm?type= ... value=9999 from your browser. Does it update your Master? You should also add "os.execute('wget http://192.168.0.19:8080/json.htm?......" to the keep alive part otherwise it shows offline after a time of no water usage. Instead of "os.execute" you can also try "commandArray["OpenURL"] = "http://....". This shouldn't make a difference but you can try.
When I paste the line into the keep alive part, I get the following error line:
2017-10-13 19:23:42.801 Error: EventSystem: in Watermeter: [string "commandArray = {} ..."]:44: attempt to concatenate global 'sWaterUsageTot' (a nil value)