Page 7 of 20

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Posted: Sunday 17 September 2017 14:23
by Trigun
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&param=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:

Code: Select all

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)
If I delete the

Code: Select all

print('GPIO Watermeter = '..otherdevices['GPIO Watermeter'])
line, the error is gone but I am wondering why it is there?

can you please help me out?

thnx in advance!

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Posted: Wednesday 27 September 2017 21:49
by pvangorp
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?

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Posted: Monday 02 October 2017 16:33
by mauricehouben70
Hi All,

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.

Thanks markiemark and pvangorp!! :)

For the noobs like me: if you want to know how to export a GPIO, look at the Domoticz wiki: https://www.domoticz.com/wiki/GPIO

Greetings,
Maurice

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Posted: Monday 02 October 2017 16:43
by mauricehouben70
Guys,

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?

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Posted: Thursday 05 October 2017 4:19
by pvangorp
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.

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Posted: Wednesday 11 October 2017 10:35
by LouiS22
Is there any way to transfer these values from a slave PI to a Master PI? (Virtual counters don't get updated this way :( ).

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Posted: Wednesday 11 October 2017 10:50
by pvangorp
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&param=udevice&idx=<<<YOUR SLAVE IDX>>>&svalue='..sWaterUsageTot..' -O /dev/null')
You can add it after the part with: commandArray['UpdateDevice'] = '34|0|'..sWaterUsageTot..'

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Posted: Wednesday 11 October 2017 18:53
by LouiS22
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&param=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.

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Posted: Wednesday 11 October 2017 19:52
by pvangorp
That works the same. Replace slave with master and visa versa. Assuming they have a network connection.

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Posted: Thursday 12 October 2017 10:24
by LouiS22
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.

Watermeter lezen met raspberry en Inductieve NPN sensor

Posted: Thursday 12 October 2017 10:29
by Trigun
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

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Posted: Thursday 12 October 2017 10:31
by pvangorp
LouiS22 wrote: Thursday 12 October 2017 10:24
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.
Yes, that should be all.

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Posted: Thursday 12 October 2017 12:28
by LouiS22
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 :)

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Posted: Thursday 12 October 2017 21:33
by Trigun
LouiS22 wrote: Thursday 12 October 2017 12:28
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

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Posted: Thursday 12 October 2017 22:29
by Trigun
Trigun wrote: Thursday 12 October 2017 21:33
LouiS22 wrote: Thursday 12 October 2017 12:28
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?

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Posted: Friday 13 October 2017 7:14
by LouiS22
pvangorp wrote: Thursday 12 October 2017 10:31
LouiS22 wrote: Thursday 12 October 2017 10:24
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.
Yes, that should be all.
Hi,

It's not working for me and it's drive me crazy :(

Code: Select all

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&param=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).

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Posted: Friday 13 October 2017 10:09
by pvangorp
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.

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Posted: Friday 13 October 2017 10:14
by Trigun
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!


Sent from my iPhone using Tapatalk

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Posted: Friday 13 October 2017 19:24
by LouiS22
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:

Code: Select all

2017-10-13 19:23:42.801 Error: EventSystem: in Watermeter: [string "commandArray = {} ..."]:44: attempt to concatenate global 'sWaterUsageTot' (a nil value)

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Posted: Friday 13 October 2017 19:36
by pvangorp
Should be sWaterUsage in keep a live part!