[SOLVED] How measure electricity and water consumption with pulse counters? (2000i/KWh and 20i/liter)

just starting from a own (scripts based) solution to openHAB.
At first I add weather and a door contact in myopenHAB “house”.

In my house I allready have installed a electrical consumption counter which send 2000 signals per kWh and a water counter with 20 signales per liter. They are connected to two GPIO ports from my Raspberry 3+.

With my old script solution on Linux I “stop” the time betwen electrical signals to measure actuall electrical consumption, and for total consumption I count the signals for electricity and water.

Maybe there is a binding or action I can use for these consumption counters I have, but I had not found them for openHAB. Maybe I search wrong or in wrong places or with wrong keywords.

Can you give me hint how to add these in openHAB (with actuall consumption, total daily consumption, total weekly consumption) ?

How does your old script work? Python?

Yes, counting-parts was in Python.
(the (big) main part (calculate graphs, create html, …) was in Shell-Script, only the counting part from these two counters was in Python!)

But I’m not a Python programer, I get the “code” to run but understand the code not completely :frowning:
Was with using interupts to see the short signal correct (before I try with a simple loop)

GPIO.add_event_detect(9, GPIO.FALLING, callback=my_callback, bouncetime=300)

Ok, so you have a way to publish the values.
Don’t break what is working, use it.

You could use the shell script to publish the values on the REST api to openHAB

OK, if you think so …
I thought, maybe there is a “correct” and standart way to use these counters into openHAB …

What do you mean with “REST api” ?

Actuall my Python script starts a shell script which uses the measues the Python (including using interrupts) has measured and calculated, see:

subprocess.call("/var/log/wbin/menergy.sh %.3f %.3f %.3f %.3f %1.0f" % (KwhAct, KwhMinAvg, KwhActPrev, KwhMinAvgPrev, NewMinute), shell=True)

Where or how should the data send to openHAB?
Or can I write these data into a file and openHAB is montioring this file if it changed?

PS: Thx vzorglub!

Ok, Good

What does the call to /var/log/wbin/menergy.sh %.3f %.3f %.3f %.3f %1.0f return FROM THE SHELL?

Because OH can call executeCommandLine and then process the response
Depending on the response then, I can guide you on how to get these values in OH

/var/log/wbin/menergy.sh -script
did not send values, it get these values from the Python script (menergy-get-signal.sh) from where it is called.

Content from menergy-get-signal.sh:
#!/usr/bin/env python2.7
# script by Alex Eames http://RasPi.tv
# http://RasPi.tv/how-to-use-interrupts-with-python-on-the-raspberry-pi-and-rpi-gpio-part-3
import RPi.GPIO as GPIO
import sys
import time
import subprocess


subprocess.call("/var/log/wbin/menergy.sh %.3f %.3f %.3f %.3f %1.0f" % (E_KwhAct, …

The menergy.sh script is “big” and use the transmitted values (parameters) ("…%.3f %.3f %.3f %.3f %1.0f").
At the end the menergy.sh -script is creating (simple) html-out which can be displayed on a tablet.

Can you get a script to resond ON THE SHELL a json string like this:

    KwhAct: 251.25,
    KwhMinAvg: 1245.12,
    KwhActPrev: 32154.21,
    KwhMinAvgPrev: 987987.12,
    NewMinute: 12

Or like this(better)…

{ KwhAct: 251.25, KwhMinAvg: 1245.12, KwhActPrev: 32154.21, KwhMinAvgPrev: 987987.12, NewMinute: 12 }

you can do an http call to the html page published by your script and retrieve the values from there

You mean, to create shell script which repond these values in a line if it is called?
" { KwhAct: 251.25, KwhMinAvg: 1245.12, KwhActPrev: 32154.21, KwhMinAvgPrev: 987987.12, … }"

Shure, this is not my favourite way, but if openHAB have no bether direct “way”, Ok …

Because I thought (or bether: a dream) to say:
“openHAB; look on GPIO 23, there is the electrical consumption, 2000 signals = 1kwH. Please check GPIO freqently and use the data and provide these data …” But if I understand correctly, this is not so “easy”, correct?

You can but if you have something that works already, use it. No need to re-invent the wheel.
And not it’s won’t be so easy. I don’t know if GPIO binding will accept so may updates so quickly and then you’ll need counters…

It can have 2 signals per second, but normally only every 10s one signal.
Is 2i/sec to much?
So I need a separate script to measure (count) it and give the info periodically to openHAB. Correct?

Than, what will bet correct or better:
Only count (stupid) and provide the signals count and openHAB will calculate the actual amount (Kwh) (based on time range between the two values openHAB knows (i.E. 15s):
Previous value: 145256
Actual value: 145279
Difference: 23 signals in 15 seconds => 2.434kwh

Or should the script deliver a by script calculated actual consumption (2.434 Kwh for last 15s)) and and total count (145279).

However, the old scripts must be totally new written, also they have only few lines, n.p. !

You mentioned earlier that your existing script publishes an html page
openHAB can read that.

Can you post an example of that page?

No, sorry. the Raspi with this solution is broken, I only have some scripts saved …
But if needed I can write the values in a file (instead in html-file), thats not a problem. When this is a “good” solution, OK.
So if I understand correct, the best will be if openHAB simple get all wanted data by a foreign script.
But what about the consumption from one day? Actuall I have this not running.
For this case, it it possible to “deliver” openHAB every 24 hours (at 3 in the morning i.e.) the actuall count, and openHAB will calculate the consumption for last day (based on the difference from 24hours before)?
Or should/must be this also calculated “extern” and delivered into openHAB?

You can do both.

The simplest way will be to get your script to respond with a json string. OH can then extract all the data from that very easily.

than I will try add a script which will write every 15 second following data into a file:

  • actuall consumption (based of last 15s)
  • last minute average
  • value from actuall counter (1 count for every 0.5W consumption (if I’m right : 2000i/Kwh))

{ KwhAvg15s: 251.25, KwhAvg60s: 1245.12, ElectricityCounter: 32154433 }

Next steps :wink:

Just write a script that echo that string when call from the command line

for example:

server:~$ ./myscript.sh
server:~$ { KwhAvg15s: 251.25, KwhAvg60s: 1245.12, ElectricityCounter: 32154433 }

I have (name is: menegy_show_data.sh):


{ KwhAct: 0.238, KwhMinAvg: 1.359, ElecCounter: 10019 }



rule "Update data"
    Time cron "0 * * ? * * *" // Every minute
    val jsonString = executeCommandLine("./patchtoscript/menergy_show_data.sh", 1000)
    KwhAct.postUpdate("JSONPATH", "$.KwhAct", jsonString)
    KwhMinAvg.postUpdate("JSONPATH", "$.KwhMinAvg", jsonString)
    ElecCounter.postUpdate("JSONPATH"), "$.ElecCounter", jsonString)

This assume that you have 3 Number items named: KwhAct, KwhMinAvg and ElecCounter

seems to work, but in the line with “ElecCounter–_” you have a closing “)” after JSONPATH which is wrong.
But I found it :wink:

Every minute I get now:

Error during the execution of rule ‘Update data’: The name ‘KwhAct’ cannot be resolved to an item or type; line 6, column 5, length 6

Now I need an item, correct?
What must be the content that the KwhMinAvg is shown in existing Group “Floor”?
(maybe the last question here …)