Watermeter lezen met raspberry en Inductieve NPN sensor Topic is solved

Topics (not sure which fora)
when not sure where to post, post here and mods will move it to right forum.
Post Reply
Trigun
Posts: 187
Joined: Wednesday 30 November 2016 12:58
Target OS: Raspberry Pi
Domoticz version: 4.9700
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by Trigun » Sunday 17 September 2017 14:23

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!

pvangorp
Posts: 115
Joined: Tuesday 28 March 2017 10:18
Target OS: Raspberry Pi
Domoticz version: Latest B
Location: The Netherlands
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by pvangorp » Wednesday 27 September 2017 21:49

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.

mauricehouben70
Posts: 5
Joined: Sunday 30 October 2016 16:09
Target OS: Raspberry Pi
Domoticz version: V3
Location: Netherlands
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by mauricehouben70 » Monday 02 October 2017 16:33

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

mauricehouben70
Posts: 5
Joined: Sunday 30 October 2016 16:09
Target OS: Raspberry Pi
Domoticz version: V3
Location: Netherlands
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by mauricehouben70 » Monday 02 October 2017 16:43

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?

pvangorp
Posts: 115
Joined: Tuesday 28 March 2017 10:18
Target OS: Raspberry Pi
Domoticz version: Latest B
Location: The Netherlands
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by pvangorp » Thursday 05 October 2017 4:19

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.

User avatar
LouiS22
Posts: 497
Joined: Friday 27 February 2015 14:21
Target OS: Raspberry Pi
Domoticz version: beta
Location: Budapest, Hungary
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by LouiS22 » Wednesday 11 October 2017 10:35

Is there any way to transfer these values from a slave PI to a Master PI? (Virtual counters don't get updated this way :( ).

pvangorp
Posts: 115
Joined: Tuesday 28 March 2017 10:18
Target OS: Raspberry Pi
Domoticz version: Latest B
Location: The Netherlands
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by pvangorp » 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..'

User avatar
LouiS22
Posts: 497
Joined: Friday 27 February 2015 14:21
Target OS: Raspberry Pi
Domoticz version: beta
Location: Budapest, Hungary
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by LouiS22 » Wednesday 11 October 2017 18:53

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.

pvangorp
Posts: 115
Joined: Tuesday 28 March 2017 10:18
Target OS: Raspberry Pi
Domoticz version: Latest B
Location: The Netherlands
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by pvangorp » Wednesday 11 October 2017 19:52

That works the same. Replace slave with master and visa versa. Assuming they have a network connection.

User avatar
LouiS22
Posts: 497
Joined: Friday 27 February 2015 14:21
Target OS: Raspberry Pi
Domoticz version: beta
Location: Budapest, Hungary
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by LouiS22 » 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.

Trigun
Posts: 187
Joined: Wednesday 30 November 2016 12:58
Target OS: Raspberry Pi
Domoticz version: 4.9700
Contact:

Watermeter lezen met raspberry en Inductieve NPN sensor

Post by Trigun » 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

pvangorp
Posts: 115
Joined: Tuesday 28 March 2017 10:18
Target OS: Raspberry Pi
Domoticz version: Latest B
Location: The Netherlands
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by pvangorp » 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.

User avatar
LouiS22
Posts: 497
Joined: Friday 27 February 2015 14:21
Target OS: Raspberry Pi
Domoticz version: beta
Location: Budapest, Hungary
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by LouiS22 » 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 :)

Trigun
Posts: 187
Joined: Wednesday 30 November 2016 12:58
Target OS: Raspberry Pi
Domoticz version: 4.9700
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by Trigun » 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

Trigun
Posts: 187
Joined: Wednesday 30 November 2016 12:58
Target OS: Raspberry Pi
Domoticz version: 4.9700
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by Trigun » Thursday 12 October 2017 22:29

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?

User avatar
LouiS22
Posts: 497
Joined: Friday 27 February 2015 14:21
Target OS: Raspberry Pi
Domoticz version: beta
Location: Budapest, Hungary
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by LouiS22 » Friday 13 October 2017 7:14

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).

pvangorp
Posts: 115
Joined: Tuesday 28 March 2017 10:18
Target OS: Raspberry Pi
Domoticz version: Latest B
Location: The Netherlands
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by pvangorp » 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.
Last edited by pvangorp on Friday 13 October 2017 10:19, edited 2 times in total.

Trigun
Posts: 187
Joined: Wednesday 30 November 2016 12:58
Target OS: Raspberry Pi
Domoticz version: 4.9700
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by Trigun » Friday 13 October 2017 10:14

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

User avatar
LouiS22
Posts: 497
Joined: Friday 27 February 2015 14:21
Target OS: Raspberry Pi
Domoticz version: beta
Location: Budapest, Hungary
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by LouiS22 » Friday 13 October 2017 19:24

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)

pvangorp
Posts: 115
Joined: Tuesday 28 March 2017 10:18
Target OS: Raspberry Pi
Domoticz version: Latest B
Location: The Netherlands
Contact:

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Post by pvangorp » Friday 13 October 2017 19:36

Should be sWaterUsage in keep a live part!
Last edited by pvangorp on Friday 13 October 2017 20:55, edited 1 time in total.

Post Reply

Who is online

Users browsing this forum: Google [Bot], hendrikenrenny and 6 guests