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

Also thanks form my side as I recently bought this 5-kanaals s0 pulse meter.

@dsoveen : is this setup still working? In my case the sensor-part is giving the requested string output, but the ā€˜state’-statement in the template is giving only errors , whatever I tried. I’m wondering whether an update of HomeAssistant is causing this as I’m using the exact coding you used, slightly adapted.

Basically the error is triggered with part of the state-line: {{ states(ā€˜sensor.s0pcm1’).split(ā€˜:’)[6] | float}}, invoking an error at the ā€˜|’ sign

Hi RenĆ©, do you have the 5-channel S0 Pulse Meter with USB working in HA? How did you do it? Following the previous messages, I’ve tried a few things, but I can’t get it to work in HA. I’m a bit of a newbie. I’m trying to switch from Domoticz to HA, but I can’t get it to work. The supplier isn’t very cooperative.

Hi Miro010, indeed i do have the 5-channel s0 pulse meter working in HA. However, it took me some time to install it as i use HA in Docker, not HA OS. And having read your request, i had to look into it again as the s0pcm-reader.log gave the Error ā€˜Serial port connection failed’. Vandaag opgelost met ChatGPT - assume you’re also Dutch?

Hallo RenƩ, ja ik ben Nederlander.
Kan je nog achterhalen hoe je de 5 kanaals S0 indertijd in HA werkend hebt gekregen?

Dag Miro, ik ben er net weer een paar dagen mee bezig geweest en eea is weer aardig terug gekomen. Waar loop je vast? Allereerst: werk je ook in een docker setup of gewoon met HA OS?

Ik werk met HA OS op RPI3B.
ik had in configuration.yaml onderstaande gezet en HA herstart. Ik zie geen entiteiten verschijnen die hier mee te maken kunnen hebben.

- platform: serial
    - name: s0pcm1
      serial_port: /dev/ttyACM0
      baudrate: 9600
      parity: E
      bytesize: 7
      stopbits: 1
template:
    - sensor:
        - name: Total s0 power usage
          unique_id: s0pcm1_m1_total
          unit_of_measurement: kWh
          device_class: energy
          state_class: total
          state: ā€œ{{ (states(ā€˜input_number.m1_offset’)| float + states(ā€˜sensor.s0pcm1’).split(ā€˜:’)[6] | float / 1000 ) | round(2) }}ā€

In Docker gaat dit dus anders, ik heb geen ervaring met HA OS. Waar ik wel tegenaan liep, is dat de serial_port niet werd herkend. Kun je dat apart testen? En vandaag was ik bezig om een HACS add on werkend te krijgen: ChatGPT is een grote hulp als je precieze vragen stelt - maar zeker niet perfect (telkens krijg ik instructies die alleen onder HA OS werken en moet ik ChatGPT ā€˜kapittelen’ :joy:). Succes ermee, ik kan je niet op een gemakkelijke manier verder helpen maar hoor wel graag of het gelukt is.