Page 19 of 20

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Posted: Thursday 02 April 2020 21:19
by Draakje
Hi All,

I have a problem.. I kinda broke my python... and now this script is not working any more..

I have tried many things but my knowledge of pyhton is not that high.

when I run the script I get this error:

/usr/bin/python /home/domoticz/domoticz/scripts/watermeter.py
Traceback (most recent call last):
File "/home/domoticz/domoticz/scripts/watermeter.py", line 20, in <module>
f = file(fn, "r+")
NameError: name 'file' is not defined


Currently python v3.7 is installed

Other python scripts (like DeConz ) are running fine..

Any help is appreciated

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Posted: Thursday 02 April 2020 22:36
by Draakje
Draakje wrote: Thursday 02 April 2020 21:19 Hi All,

I have a problem.. I kinda broke my python... and now this script is not working any more..

I have tried many things but my knowledge of pyhton is not that high.

when I run the script I get this error:

/usr/bin/python /home/domoticz/domoticz/scripts/watermeter.py
Traceback (most recent call last):
File "/home/domoticz/domoticz/scripts/watermeter.py", line 20, in <module>
f = file(fn, "r+")
NameError: name 'file' is not defined


Currently python v3.7 is installed

Other python scripts (like DeConz ) are running fine..

Any help is appreciated
Ok, I fixed it myself (and google:) )
turns out the command file is not supported anymore and has been replaced by open.

So after changing file to open the script worked again!
(Lines 20 & 40. f = file(fn, "r+") )

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Posted: Tuesday 07 July 2020 9:57
by jvrietveld
Hi everyone,

Like many of us I try to setup a watermeter with inductive NPN sensor. The led lights on and off when the wheel is spinning. I have setup all the devices in Domoticz but still the data on my dashboard is unchanged. The GPIO is connected on 21

pi@domoticz:~ $ sudo raspi-gpio get 21
GPIO 21: level=1 fsel=0 func=INPUT

In the cronlog.log I found this.
Spoiler: show

Code: Select all

pi@domoticz:/var/log $ more cronlog.log
Callback function called!
Traceback (most recent call last):
  File "/home/pi/domoticz/scripts/watermeter.py", line 59, in Interrupt
    response1 = urllib2.urlopen(req1)
  File "/usr/lib/python2.7/urllib2.py", line 154, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/lib/python2.7/urllib2.py", line 429, in open
    response = self._open(req, data)
  File "/usr/lib/python2.7/urllib2.py", line 447, in _open
    '_open', req)
  File "/usr/lib/python2.7/urllib2.py", line 407, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.7/urllib2.py", line 1228, in http_open
    return self.do_open(httplib.HTTPConnection, req)
  File "/usr/lib/python2.7/urllib2.py", line 1198, in do_open
    raise URLError(err)
urllib2.URLError: <urlopen error [Errno 101] Network is unreachable>
pi@domoticz:/var/log $

With the 'event script triggers' on I get this in the log:
 2020-07-07 09:54:51.825 Status: EventSystem: Script event triggered: /home/pi/domoticz/scripts/lua/script_device_watermeter.lua
2020-07-07 09:54:51.936 Status: EventSystem: Script event triggered: /home/pi/domoticz/scripts/lua/script_device_watermeter.lua
2020-07-07 09:54:51.943 Status: EventSystem: Script event triggered: /home/pi/domoticz/scripts/lua/script_device_watermeter.lua
2020-07-07 09:54:58.053 MQTT: Topic: domoticz/in, Message: {"idx":78,"nvalue":0,"svalue":"0;55380.2","Battery":100,"RSSI":8}
2020-07-07 09:54:58.239 Status: EventSystem: Script event triggered: /home/pi/domoticz/scripts/lua/script_device_watermeter.lua 
Any ideas?

Best regards,
Johan

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Posted: Tuesday 07 July 2020 10:01
by Trigun
jvrietveld wrote: Like many of us I try to setup a watermeter with inductive NPN sensor. The led lights on and off when the wheel is spinning. I have setup all the devices in Domoticz but still the data on my dashboard is unchanged. The GPIO is connected on 21
Why use many different and complex scripts? I have an esp8266( with esp easy)with a proximity sensor. I’ve sanded 2mm of the orange tip for better detection and all the data is send to domoticz. I’ve been running it for three months now without any liters being lost.

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Posted: Wednesday 08 July 2020 9:05
by jvrietveld
Trigun wrote: Tuesday 07 July 2020 10:01 Why use many different and complex scripts? I have an esp8266( with esp easy)with a proximity sensor. I’ve sanded 2mm of the orange tip for better detection and all the data is send to domoticz. I’ve been running it for three months now without any liters being lost.
Hi Trigun,

Your comment prompted me to look at this solution. I have worked with esp8266 before in a NodeMCu and Wemos D1. The ESP Easy software is also very easy to configure. I think I'll try this url https://robothuis.nl/2020/05/01/waterme ... -domoticz/ as a new way to solve this.

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Posted: Wednesday 08 July 2020 10:17
by Trigun
jvrietveld wrote:
Trigun wrote: Tuesday 07 July 2020 10:01 Why use many different and complex scripts? I have an esp8266( with esp easy)with a proximity sensor. I’ve sanded 2mm of the orange tip for better detection and all the data is send to domoticz. I’ve been running it for three months now without any liters being lost.
Hi Trigun,

Your comment prompted me to look at this solution. I have worked with esp8266 before in a NodeMCu and Wemos D1. The ESP Easy software is also very easy to configure. I think I'll try this url https://robothuis.nl/2020/05/01/waterme ... -domoticz/ as a new way to solve this.
Good idea! Although I don’t even use the voltage regulator. I soldered the sensor on to my wemos. Used a stable raspberry pi power supply and configured espeasy. As easy as thatImage.

Goog luck!

Oh yeah, if you, by accident, sanded to much of the sensor tip, use some painting tape to add an extra layer or two Image the tape is thin enough for a good detection.Image


Sent from my iPhone using Tapatalk

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Posted: Friday 22 January 2021 13:51
by Joost996me
Hi all,

Got my sensor working using the LUA scripting. Still got a problem with the counter increasing, although the led is not on activated. This causes the meter to read appr 50-70 an hour without any usage..

Read something about shielding the cable, but could it also be a error in the script?

Someone able to give me a clue how to solve this?

thanks!

----


the Script:

commandArray = {}

-- IDX van de virtuele Watermeter
iIDX = 311

-- Stand van fysieke watermeter overnemen naar de virtuele watermeter (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:WaterVerbruik'] = tostring(uservariables["WaterMeter"])
commandArray['Variable:WaterMeter'] = tostring(0)
end

if ( uservariables["WaterMeter"] == -1 ) then
commandArray['UpdateDevice'] = ''..iIDX..'|0|0'
commandArray['Variable:WaterMeter'] = tostring(0)
return commandArray
end

-- Waterverbruik
-- Waterverbruik ophalen uit de variabele WaterVerbruik:
sWaterUsage = uservariables["WaterVerbruik"]

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("Waterverbruik is gewijzigd naar " .. sWaterUsageTot / 1000 .. "m3 ")
commandArray['UpdateDevice'] = ''..iIDX..'|0|'..sWaterUsageTot..''
commandArray['Variable:WaterVerbruik'] = tostring(sWaterUsageTot)
else
-- Iedere 5 minuten het waterverbruik registreren (t.b.v. grafieken/statistieken), ook al is er niets verbruikt.
if otherdevices_lastupdate["Waterverbruik"] == nil then
commandArray['UpdateDevice'] = ''..iIDX..'|0|'..sWaterUsage..''
elseif (timedifference(otherdevices_lastupdate["Waterverbruik"]) > 300) then
print("Laatste waterverbruik is meer dan 5 minuten geleden")
print("Waterverbruik is nog steeds " .. sWaterUsage / 1000 .. "m3 ")
commandArray['UpdateDevice'] = ''..iIDX..'|0|'..sWaterUsage..''
end
end

return commandArray

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Posted: Tuesday 02 February 2021 13:37
by freijn
Joost,

The issue is that you only detect a status from the sensor you need to detect the change only. The so called flank.


if (devicechanged['GPIO Watermeter'] == 'Off') then

when the meter stops just under the sensor your led is always activated or vise vera, not activated.

The Lua script would get compilcated and might miss stuff. Hence I would advise you to eighter use Python or goto an
arduino with easpEASY where flank detection is already build in.

Cheers,

Frank

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Posted: Thursday 04 February 2021 18:41
by Joost996me
Thansk Frank. Ill try to use a python script!


Cheers

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Posted: Wednesday 24 February 2021 10:55
by aDJa
pvangorp wrote: Wednesday 06 September 2017 20:45 I've upgraded my script by adding a "uservariable" this makes it possible to set the initial value and to make a correction when your PI is off for a while. Create a user variable called "WaterMeter" and enter the meter value eg "999999" (Where "999999" = 999.999m3). When the value is "0" it ignores the user variable.
I also added a variable for IDX, to make it easier to change the script.
So you don't need the line
To set the initial meter values http://<<YOUR DOMOTICZ IP>>/json.htm?type=command&param=udevice&idx=34&svalue=999999
any more

My updated script

Code: Select all

commandArray = {}

-- Set IDX of Watermeter
iIDX = 34

-- 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
First off all, awesome post, have been working on it for several days and now it works! Day 2 succesfully :D
Started with de python but very happy with the lua script inside Domoticz.

To share my experience to get this working, some off the bumps i got into, that it may help others:
- had some issue with libary (I think), so updated the GPIO driver to 2.52, see http://wiringpi.com/wiringpi-updated-to ... rry-pi-4b/ Issue was; I wasn't able to select the port when setting up a virtual switch to the GPIO port.
- set the port in init.d solved my issue my port (GPIO15) changed eached time I booted
- didnt understand the jason inital meter setup but found out (yes, noob) I could just put it in the adress bar in a browser. Thanks to this update of pvangorp happy to use a variable, again inside Domoticz = I like!
- in above script of pvangorp changed the status trigger to Closed (instead of Off) and it worked! And the names of the devices off course + ID as mentioned.

Hardwarde; I used the BS170 MOSFET with two 10k resistors, 5V power supply to the sensor works well, the MOSFET switches the 3V3 from Raspberry so nice protection to prevent 5V on the GPIO input. Herewith also a very clear switching, no flanking issues.

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Posted: Wednesday 24 February 2021 10:59
by aDJa
Joost996me wrote: Friday 22 January 2021 13:51 Hi all,

Got my sensor working using the LUA scripting. Still got a problem with the counter increasing, although the led is not on activated. This causes the meter to read appr 50-70 an hour without any usage..

Read something about shielding the cable, but could it also be a error in the script?
if the led is not activated it sounds like the sensor isn't working.. I would check power supply, distance to meter (metal)

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Posted: Friday 19 March 2021 16:39
by feedtaleten
I have seen that fiddling with the raspberry gives a lot of false readings (from the contacts wiggling I guess), so if you're experimenting a lot with it you can expect a fair bit of errors.

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Posted: Sunday 25 April 2021 13:39
by Draakje
did somebody tried to use Tasmota software to read the watermeter with the NPN sensor?

I did found this article using other software https://robothuis.nl/2020/05/01/waterme ... -domoticz/ (dutch)
This is using a wemos D1 mini (have lots of those) running ESP EASY

Was wondering if anyone figured this out with a standard tasmota firmware.. (would save me the hassle )

regards

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Posted: Thursday 29 April 2021 21:38
by Draakje
To all it may concern.

I have successfully connected the NPN sensor to a Sonoff Basic (R2).
NPN.jpg
NPN.jpg (285.86 KiB) Viewed 2247 times
Red wire is connected to 5v.
Blue wire is connected to GND
Yellow wire is connected to IO2

In the Tasmota software you have to select GPIO2 to counter.

I needed to compile Tasmota because I needed the condition function to have the updates sent immediately to domoticz.
This is done by a rule.

"Rule1":{"State":"ON","Once":"OFF","StopOnError":"OFF","Length":120,"Free":391,"Rules":"on Counter#C1 DO if (%var1% < %value%) publish domoticz/in {\\\"idx\\\":12,\\\"svalue\\\":\\\"%value%\\\"}; var1 %value% endif ENDON"}}

with this setup I reuse my existing Sonoff Basic (so it has 2 functions now) and have a neater solution.

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Posted: Saturday 15 May 2021 19:37
by fschaefer
pedemo wrote: Wednesday 08 May 2019 13:08 its true that in some cases the 5v of the Rpi is not sufficient. a different power supply might help.
when you use the battery, does domoticz get a signal?
Can anyone share which 5V Inductive Proximity Sensor works with a raspberry pi?

I tried the LJ12A3–4-Z/BX one and stumbled upon the same issue where the sensor's LED indicated a metal detection but the output can't actually be detected on a GPIO pin.

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Posted: Sunday 16 May 2021 22:18
by PvHeerebeek
I have added a very small 5V to 12V up converter to power the sensor from the Pi and a resistor voltage step down network to bring the output back to 3.3v

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Posted: Thursday 25 November 2021 18:19
by GeyerA
JeeGee wrote: Friday 24 January 2020 19:35 My PNP with the plugin was running fine, but this week I had 3 times the issue that the measuring went crazy.
Examples: in one hour it said I used 400 liters, out of the blue and even one time 15.000 liters.
Tried to place the PNP a it different, but so far it didn't help.

Anyone else seeing this? And could be the cause?
Hi, I now also installed a Zenner NPN and did run with ESP8266 (espeasy) without problems. I sent the count to a virtual sensor and did not encounter any issues.
I then thought lets hook up the sensor directly to PI running the plugin. I also get once or twice a day peak values of over 100 litres consumption although no water was used. I set-up the Telegram notification on "above" 100 litres consumption and that one even keeps on sending messages with the disabled plug-in (and disconnected sensor). I moved back to ESP8266 HTTP to Domoticz. In my opinion, this got nothing to do with sensor position since it works just fine on Espeasy, but same setup with plugin, twice a day peak values of over 100 litres. If somebody guides me to better logging I am happy to re-set up with plugin....maybe akamming is able to help.

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Posted: Saturday 27 November 2021 23:30
by Piacco
Last week i did a fresh install of Domoticz and updated to (build 138172). Now the watermeter-plugin gives an error

Code: Select all

 Error: (FindModule) Domoticz/DomoticzEx modules not found in interpreter.
I have read in this post that this probably has something to do with the new Python framework.

https://domoticz.com/forum/viewtopic.p ... 81#p279981

Or could this be the problem?

https://www.domoticz.com/wiki/Developi ... hon_plugin
Troubleshooting
Importing Modules Fails
...........
Broadly these are: the plugin directory, the existing Python path as picked up when Domoticz started and the 'site' directories for system level modules installed with pip3. Local 'site' directories are not searched by defaul although plugin developers can add any directory the like to the path inside the plugin. Pip3 installs packages in system directories when sudo is used on linux platforms.

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Posted: Thursday 09 December 2021 7:54
by GeyerA
GeyerA wrote: Thursday 25 November 2021 18:19
JeeGee wrote: Friday 24 January 2020 19:35 My PNP with the plugin was running fine, but this week I had 3 times the issue that the measuring went crazy.
Examples: in one hour it said I used 400 liters, out of the blue and even one time 15.000 liters.
Tried to place the PNP a it different, but so far it didn't help.

Anyone else seeing this? And could be the cause?
Hi, I now also installed a Zenner NPN and did run with ESP8266 (espeasy) without problems. I sent the count to a virtual sensor and did not encounter any issues.
I then thought lets hook up the sensor directly to PI running the plugin. I also get once or twice a day peak values of over 100 litres consumption although no water was used. I set-up the Telegram notification on "above" 100 litres consumption and that one even keeps on sending messages with the disabled plug-in (and disconnected sensor). I moved back to ESP8266 HTTP to Domoticz. In my opinion, this got nothing to do with sensor position since it works just fine on Espeasy, but same setup with plugin, twice a day peak values of over 100 litres. If somebody guides me to better logging I am happy to re-set up with plugin....maybe akamming is able to help.
Hi, need to withdraw my post. I have now added 1K resistors to my sensors (water pull-down / gas pull-up) and seems working fine. I will need to check on a longer period to confirm that no fake counts occur. I also added some pictures from my Zenner water and Actaris gas counter...thanks again to akamming for the plug-in.
BTW, using this reed sensor on the gas counter https://www.pic-gmbh.com/produkte/ms-332-3

UPDATE: Just got again a fake read of 250 liters water consumption; could it be that the pull up resistor is too small with 1K?
UPDATE2: Now changed to 10K resistor...
UPDATE3: Still had a fake reading on water sensor so displaced the raspberry away from any disturbances like NAS, power and network cables as well as network gigaswitches and managed to have one day without fake reading. I will keep you posted.
UPDATE4: Since the fake readings on water sensor did come back, I decided to move back to ESP8266 with ESPeasy. (pulse high on water and gas meter as well as 500ms debounce time).
UPDATE5: Fake readings still occurred also on ESP8266, seems the water sensor reading is the culprit. I have now connected the gas sensor on raspberry using the nice domoticz plugin and water on WeimosMini with espeasy. I also have to say that the external 1KOhm pull ups did not change anything so I am also using the internal pullups.
UPDATE6: It has now been working for nearly a week without fake reading. Seems, if I hook up both sensors on one device (regardless external 1kOhm pull-up or not), interferences will occur. Having it split, works fine.

Re: Watermeter lezen met raspberry en Inductieve NPN sensor

Posted: Wednesday 08 February 2023 23:27
by Benneton
Hi

Anyone updated the Python script to work with Python 3?
Needed to replace my PI, so took the opportunity to upgrade to PI 4 and latest version software.

When rebuilding config and installing the watermeter.py script again I got an error message:
Traceback (most recent call last):
File "/home/bernard/domoticz/scripts/watermeter.py", line 4, in <module>
import urllib2
ModuleNotFoundError: No module named 'urllib2'

Looked it up and Python 3 does not support urllib2
This is replaced by urllib.request, urllib.parse with different syntax to use. As I have no programming skills I tried this:
replace:
import urllib2

with:
import urllib.parse
import urllib.request

and replace:
req1 = urllib2.Request(url1)
response1 = urllib2.urlopen(req1)

with:
req1 = urllib.request(url1)
reponse1 = urlopen(req1)

would this be correct? it appears to work as I do not get an error on this, now I have:

Traceback (most recent call last):
File "/home/bernard/domoticz/scripts/watermeter.py", line 20, in <module>
f = file(fn, "r+")
NameError: name 'file' is not defined

Is that something that has changed when moving to Python 3?

When someone has updated the Python script when you share it will be much appreciated.

I have the feeling the debugging is above my programming knowledge

Thank you very much,
Bernard