S0 pulse counter on Raspberry Pi

Hi, I have HA running on a PI and I would like to monitor power consumption of a Kwh meter with S0 a pulse interface.

I have seen examples of how to do this on an ESP board but I would like to do it directly on my PI as it is sitting directly next to the Kwh meter and it seems this would be a simpler setup. In the documentation of the Binary Sensors I cannot see anything about counting pulses on a GPIO port, is this even possible?

I’m actually looking for the same thing. I also have a kWh meter with S0 pulse output which I would like to connect to my Raspberry Pi running Home Assistant. And I’m quite handy, but not an electronics expert.

What I’ve learned so far is that my power meter delivers 12~27V at ≤27mA. The pulse takes 90ms max. The meter will ultimately handle 60VDC, max. 50mA.

According to https://www.raspberrypi.org/forums/viewtopic.php?p=529340 it seems not advisable to directly connect the S0 output to the Pi, but use an opto-isolator instead. But I couldn’t figure that one out just yet.
However, it seems possible to get it working without opto-isolator on a Pi Zero, according to OpenHAB user: https://community.openhab.org/t/s0-power-meter-readout-using-raspberry-pi-zero/22779
So I’m a bit in doubt here.

Another solution could be to buy this S0 Pulse counter module: http://www.smartmeterdashboard.nl/webshop. But to me this seems a bit ‘overkill’ just to register a pulse on te Pi? And then I’m still not sure how I’m going to link this counter to a Home Assistant sensor.

Some help on this subject would be welcome.

Hi, did you get S0 pulse counting on raspberry working in mean time?
i found this usb S0 pulse counter 5-kanaals S0 Pulse Meter op USB

Have you tried this one already? I’m buying one tonight.

no have not purchased one yet

Did you manage to connect that device to your raspberry?

Yes, it’s connected and working.
Connected it through a usb-hub because of the lack of inputs and it’s working fine. If you want to know the setup let me know.

1 Like

Can you tell me how you did integrate this? I’ve got the same hardware and previous used it with Domoticz. Wat integration do you use?

Hi timkrull,

I, too, would be interested in your solution. Would be very nice to be able to connect the S0 via USB right to a raspberry pi and ultimately somehow integrate it into HA.

Hello,

So I’ve used the S0 5 channel pulse meter from a dutch website: 5-kanaals S0 Pulse Meter op USB

Installation was pretty easy. Just connect the two wires to the S0 pulse reader the right way. And the reader to my home assistant device. (USB)
Once connected you’re able to check the hardware connected. (settings->system->hardware, find the 3 dots on the top right and you’ll find all the hardware)
In my case the device path was “/dev/ttyACM0”. It’s recommenden to use the device ID but I couldn’t get it to work right away so used the device path.

When connected I added the following on my config file:

sensor:

  • platform: serial
    name: s0pcm5
    serial_port: /dev/ttyACM0
    baudrate: 9600
    parity: E
    bytesize: 7
    stopbits: 1
    (old sensor templating Template - Home Assistant)
    These settings were in the box of the pulse meter.

Every few secconds the pulse meter will send the readed information to home assistant. Next you need to do is read out this information the right way. The information is splitted with the : sign. And will look something like this: ID:x:00:01:M1:03:12:M2:c:d:M3:e:f:M4:g:h:M5:i:j

Little explanation
ID:x:I:y:M1:a:b:M2:c:d:M3:e:f:M4:g:h:M5:i:j

x = ID S0 meter
y = Pulse Count (standard 10 sec)
M1, M2, etc the connection point on you S0 pulse meter. (always start with M1)
a,c,e,g,i = Pulses since last interval
b,d,f,h,j = Total pulses since startup

To read these out the right way I added the following in my config file (I still got some old coding in my config, so check what you want to use):
’ ’

  • platform: template
    sensors:
    s0pcm5_m1_total_watt:
    unique_id: s0pcm5_m1_total_watt
    friendly_name: Total s0 power usage
    unit_of_measurement: “W”
    value_template: “{{ states(‘sensor.s0pcm5’).split(’:’)[6] | float }}”
  • platform: template
    sensors:
    s0pcm5_m1_total:
    unique_id: s0pcm5_m1_total
    friendly_name: Total s0 power usage
    unit_of_measurement: kWh
    device_class: energy
    value_template: “{{ (states(‘input_number.m1_offset’)| float + states(‘sensor.s0pcm5’).split(’:’)[6] | float / 1000 ) | round(2) }}”
    (old sensor templating Template - Home Assistant)
    ’ ’
    or
    ’ ’
    template:
  • sensor:
    unique_id: s0pcm5_m1_total
    name: Total s0 power usage
    unit_of_measurement: kWh
    device_class: energy
    state_class: total
    state: “{{ (states(‘input_number.m1_offset’)| float + states(‘sensor.s0pcm5’).split(’:’)[6] | float / 1000 ) | round(2) }}”
  • sensor:
    name: Total ESP power usage
    unit_of_measurement: kWh
    device_class: energy
    state_class: total
    state: “{{ (states(‘sensor.energy_consumed_tariff_1’)| float + states(‘sensor.energy_consumed_tariff_2’) | float )}}”
    ’ ’
    (new sensor templating Template - Home Assistant)

The value given by the S0 pulse meter is splitted at the 6th : where it will read it’s value from.

After this I guess you’ll know what to do. I’ve copied some of my codes from other forums and other users.

I need to note, the pulse meter isn’t really accurate though. Since it’s a pulse meter it doesn’t (offcourse) read out your meter, it just checks the pulses.
I wanted it more accurate so switched to a modbus reader, modbus it installed simular and really reads out the device.

Hope I helped you on your way.

1 Like
template:
  - sensor:
      - name: Total s0 power usage
        unique_id: s0pcm2_m1_total
        unit_of_measurement: kWh
        device_class: energy
        state_class: total
        state: "{{ (states('input_number.m1_offset')| float + states('sensor.s0pcm2').split(':')[6] | float / 1000 ) | round(2) }}"

This is how I got it working. Indentation, dashes,… New to HA, but formatting these yaml files is a nightmare for me.

Don’t forget to add input_number.m1_offset as a Helper under settings->devices&services->Helpers

Thanks for the push in the right direction!

Thanks Tim and dsoveen.
The code of dsoveen works provided that “sensor.s0pcm2” is modified to “sensor.pcm5” in order to correspond to Tim’s serial platform.

1 Like