Power consumption graph, GPIO readout on rPi3 connected to power meter

Can anyone point me to a good way to do the following (maybe even some existing code)?

I have a rPI3, which I connected to my electric meter that counts how much power my heatpump consumes. There are 1000 impules for every kWh that has been consumed. The wires are connected to two GPIO pins. I have been able to get a C program to work (found here: https://blog.webernetz.net/stromzahler-mit-s0-schnittstelle-vom-raspberry-pi-auswerten/) which “detects the impules” and uses a counter in a file /var/strom/stromcounter. For every impules received, the counter is incremented by 1. As soon as the C program is stopped, counters return to 0, so no kind of database is used.

What I’d like to have now is a graph / graphs that displays the power consumption. As I am quite the openHab beginner, I’m not sure what the best approach would be. I guess I need to use some kind of persistance? How do I get the data into openHab? I do have a MQTT broker running, but I guess I would need to connect to that MQTT broker via my C code, or use something else (like a Python script or so?), to publish the counter to openHab? Or is it easier to keep the counter file and read that somehow in openHab?

The best thing would be if the data would be stored, so I can keep track of power consumption over time (not sure how accurate that will be over time though).

Help would be greatly appreciated :slight_smile:

Do you know volkszaehler.org? This software is intended to track down any sort of consumption like water, electrical power, gas and so on. It’s easy to read the consumption through an item bound to http. If you don’t want to use the volkszaehler stuff, you could either read a file frequently to get the actual state or put the pulses to openHAB by using curl via openHAB RESTful API

I think I’d rather keep the functionality on my local machine (so it would for example keep working if the internet connection is down).

Would it not be better to use my already running MQTT instead of REST? How would you do it exactly, publish the data? And what to do with it afterwards?

I guess I might be able to just use the GPIO binding instead? Will have to look into that.

volkszaehler.org is completely local :wink: they just provide frontend/backend and middleware plus various howtos for different measurement hardware.

Of course you could use gpio binding to get the pulses to openHAB directly. I thought the raspberry for collecting pulses was not the openHAB machine…

Ok, I must admit, I didn’t even look at volkszaehler.org, I somehow automatically assumed it had to be some kind of cloud service.

I just read that the GPIO binding does not work with OH2 anymore. So I still have to find another way. MQTT, Rest, not sure…

I think it’s actually working with the GPIO binding.

Now for the calculation and graphing. I guess I will have to for example use a counter to count the interrupts, then use some kind of cron job to calculate the “current power consumption” for example every minute, store that in a number item and then store that value somewhere (RRD4J?) and also use that to get a graph of that data. Any more suggestions? :slight_smile:

Hi Mario, is your binding working now?
I´ve to Eltako s0-counters connected to GPIO-Ports. With FHEM it worked, but now I´d like to switch to OH.

I´m interested in:

  • how to configure GPIO binding to log the interupts send by s0-counter
  • build some graphs.

Hi Peter,

yea, it’s working.

This is my gpio.items file:

Contact GPIOPin3 "Pin3" { gpio="pin:3 force:yes" }

The rules file I use contains the following:

var Number gpio3InterruptCounter = 0
var Number tmp = 0

rule "GPIO Pin3 interrupt"
when
        Item GPIOPin3 received update CLOSED
then
        gpio3InterruptCounter = gpio3InterruptCounter + 1
end

rule "Cron job every minute"
when
    Time cron "0 0/1 * * * ?"
then
        var Number diff = gpio3InterruptCounter - tmp
        tmp = gpio3InterruptCounter

        postUpdate(Nibe_Tick_Counter_Electric_Meter, diff)
        diff = diff / 16.6667
        postUpdate(Nibe_Power_Consumption_Mean_Minute, diff)
end

Don’t ask me why I have to divide the interrupt counter by 16.66667… somehow that seems to be giving me the connect values.

I then use Grafana to display that item, which is “persisted” in a influxdb database (the yellow graph is the power meter of my heat pump):

I hope that helps.

Wow, that looks great. I´m new to OH and have to define “Heizung_Tick_Counter_Electric_Meter” - how do I do that?

Thank you very much!

Ok, I think i got it - but i don´t get any counts…

Not sure what could be the problem. What GPIO pins did you connect your meter to? I connected my cable to the pins (Pin#5 / GPIO03 and Pin 09 / Ground) in the screenshot below.

In FHEM i used this GPIOs:

PIN (GPIO 25)= Lüftung s0 + (Ventilation)
PIN (GND) = Lüftung s0 -
PIN (GPIO 17 )=WP s0 + (Vaillant geoTherm)
PIN (GND)=WP s0-

My gpio.items:

//Pin17=Wärmepumpe S0-Zähler +
//Pin25=Lüftungsanlage S0-Zähler +

Contact GPIOPin17 "Pin17" { gpio="pin:17 force:yes" }
Contact GPIOPin25 "Pin25" { gpio="pin:25 force:yes" }

my s0_counter.items:

Number  Heizung_Tick_Counter_Electric_Meter      "Heizung_Tick_Counter_Electric_Meter"
Number  Heizung_Power_Consumption_Mean_Minute      "Heizung_Power_Consumption_Mean_Minute"
Number  Lueftung_Tick_Counter_Electric_Meter      "Lueftung_Tick_Counter_Electric_Meter"
Number  Lueftung_Power_Consumption_Mean_Minute      "Lueftung_Power_Consumption_Mean_Minute"

my s0_counter_heizung.rules (same as for ventilation with PIN25)

var Number gpio17InterruptCounter = 0
var Number tmp = 0

rule "GPIO Pin17 interrupt"
when
        Item GPIOPin17 received update CLOSED
then
        gpio17InterruptCounter = gpio17InterruptCounter + 1
end

rule "Cron job every minute"
when
    Time cron "0 0/1 * * * ?"
then
        var Number diff = gpio17InterruptCounter - tmp
        tmp = gpio17InterruptCounter

        postUpdate(Heizung_Tick_Counter_Electric_Meter, diff)
        //diff = diff / 16.6667
        postUpdate(Heizung_Power_Consumption_Mean_Minute, diff)
end

and in sitemap:

Frame label="Wärmepumpe (ebus)" {
        Text item=Vorlauf label="Vorlauf: [%.0f Grad]" icon="none"
        Text item=Ruecklauf label="Rücklauf: [%.0f Grad]" icon="none"
      	Text item=Temperatur_Warmwasser label="Temperatur Warmwasser: [%.0f Grad]" icon="none"
        Text item=Druck_Heizkreis label="Druck Heizkreis: [%.0f bar]" icon="none"
        }

Result is that I don´t see anything.

Is the openhab user member of group gpio? Did you change /etc/default/openhab according to this: http://docs.openhab.org/addons/bindings/gpio1/readme.html by adding

JAVA_ARGS=-Djna.boot.library.path=/usr/lib/jni

?

Yes, both.

Not sure. I was happy that it just worked right away.

Have you tried logging in the “GPIO Pin17 interrupt” rule? Do you get any interrupts? Could you try using different pins, maybe the ones I am using?

How can i log a rule?

Use the following to add a line into the openhab log:

logInfo("test", "bla")

In the logfile there are some values:

2017-12-20 21:54:00.036 [ItemStateChangedEvent     ] - Lueftung_Tick_Counter_Electric_Meter changed from 0 to 1
2017-12-20 21:54:00.050 [ItemStateChangedEvent     ] - Lueftung_Power_Consumption_Mean_Minute changed from 0 to 1

and then, 1 minute later:

2017-12-20 21:55:00.024 [ItemStateChangedEvent     ] - Lueftung_Tick_Counter_Electric_Meter changed from 1 to 0
2017-12-20 21:55:00.040 [ItemStateChangedEvent     ] - Lueftung_Power_Consumption_Mean_Minute changed from 1 to 0

I think that this is the problem - perhaps not enough ticks per Minute?

Hi, i changed to PIN3. After Rebooting the Raspi i got following Message in the Log file:

2017-12-23 14:51:30.082 [ItemStateChangedEvent ] - GPIOPin3 changed from OPEN to CLOSED
2017-12-23 14:51:30.145 [ItemStateChangedEvent ] - GPIOPin3 changed from CLOSED to OPEN

My sitemap is showing 0 Watts - i think GPIO is working now?
Is my sitemap definition wrong (Post above)?