Isolating single value from serial input fails

Hi,
I am receiving serial data from a Electricity meter (Landis & Gyr) via a direct serial link on my Rpi 3B+ running Home assistant 2021-08-06 on Hass OS 6.2. It took some effort to get the serial port to work, but serial data is arriving in the serial sensor.

sensor:
  - platform: serial
    name: Energy meter
    serial_port: /dev/ttyAMA0
    baudrate: 115200    
    bytesize: 8
    parity: N

From the log file:

2021-08-13 21:32:12 DEBUG (MainThread) [homeassistant.components.serial.sensor] Received: /XMX5LGBBFG1012664304
2021-08-13 21:32:12 DEBUG (MainThread) [homeassistant.components.serial.sensor] Received: 
2021-08-13 21:32:12 DEBUG (MainThread) [homeassistant.components.serial.sensor] Received: 1-3:0.2.8(42)
2021-08-13 21:32:12 DEBUG (MainThread) [homeassistant.components.serial.sensor] Received: 0-0:1.0.0(210813213203S)
2021-08-13 21:32:12 DEBUG (MainThread) [homeassistant.components.serial.sensor] Received: 0-0:96.1.1(4530303331303033373334343237393136)
2021-08-13 21:32:12 DEBUG (MainThread) [homeassistant.components.serial.sensor] Received: 1-0:1.8.1(017386.714*kWh)
2021-08-13 21:32:12 DEBUG (MainThread) [homeassistant.components.serial.sensor] Received: 1-0:1.8.2(016328.895*kWh)
2021-08-13 21:32:12 DEBUG (MainThread) [homeassistant.components.serial.sensor] Received: 1-0:2.8.1(000000.000*kWh)
2021-08-13 21:32:12 DEBUG (MainThread) [homeassistant.components.serial.sensor] Received: 1-0:2.8.2(000000.000*kWh)
2021-08-13 21:32:12 DEBUG (MainThread) [homeassistant.components.serial.sensor] Received: 0-0:96.14.0(0001)
2021-08-13 21:32:12 DEBUG (MainThread) [homeassistant.components.serial.sensor] Received: 1-0:1.7.0(00.507*kW)
2021-08-13 21:32:12 DEBUG (MainThread) [homeassistant.components.serial.sensor] Received: 1-0:2.7.0(00.000*kW)
2021-08-13 21:32:13 DEBUG (MainThread) [homeassistant.components.serial.sensor] Received: 0-0:96.7.21(00004)
2021-08-13 21:32:13 DEBUG (MainThread) [homeassistant.components.serial.sensor] Received: 0-0:96.7.9(00002)
2021-08-13 21:32:13 DEBUG (MainThread) [homeassistant.components.serial.sensor] Received: 1-0:99.97.0(2)(0-0:96.7.19)(180823090507S)(0000003479*s)(170212204313W)(0000000294*s)
2021-08-13 21:32:13 DEBUG (MainThread) [homeassistant.components.serial.sensor] Received: 1-0:32.32.0(00000)
2021-08-13 21:32:13 DEBUG (MainThread) [homeassistant.components.serial.sensor] Received: 1-0:32.36.0(00000)
2021-08-13 21:32:13 DEBUG (MainThread) [homeassistant.components.serial.sensor] Received: 0-0:96.13.1()
2021-08-13 21:32:13 DEBUG (MainThread) [homeassistant.components.serial.sensor] Received: 0-0:96.13.0()
2021-08-13 21:32:13 DEBUG (MainThread) [homeassistant.components.serial.sensor] Received: 1-0:31.7.0(002*A)
2021-08-13 21:32:13 DEBUG (MainThread) [homeassistant.components.serial.sensor] Received: 1-0:21.7.0(00.507*kW)
2021-08-13 21:32:13 DEBUG (MainThread) [homeassistant.components.serial.sensor] Received: 1-0:22.7.0(00.000*kW)
2021-08-13 21:32:13 DEBUG (MainThread) [homeassistant.components.serial.sensor] Received: 0-1:24.1.0(003)
2021-08-13 21:32:13 DEBUG (MainThread) [homeassistant.components.serial.sensor] Received: 0-1:96.1.0(4730303331303033333738373439353136)
2021-08-13 21:32:13 DEBUG (MainThread) [homeassistant.components.serial.sensor] Received: 0-1:24.2.1(210813210000S)(15068.413*m3)
2021-08-13 21:32:13 DEBUG (MainThread) [homeassistant.components.serial.sensor] Received: !B906

The meter is sending a block of multiple lines (all terminated CR/LF) with electricity consumption data, every couple of seconds. I am (for now) only interested in a single line, that starts with 1-0:1.8.1.
The full line reads: 1-0:1.8.1(017386.714*kWh)
The rest of the lines should be discarded. Later I need to create more sensors to cater for some of the other data (6 lines out of 20).
I created a template sensor for that, which takes the serial sensor as input. Problem is that the value always seems to be empty (not undefined, but an empty string). I think that every line is evaluated and results in setting of the state. When I experiment in Developer tools / templates, it seems that a value flashes by. I am new to this Jinja syntax, so it would not be a surprise if the code is wrong.

Please advise. Many thanks in advance.

Template sensor definitition:

template:
  - sensor:
      - name: "Energy current use"
        unit_of_measurement: "kWh"
        state: >
          {% if "1-0:1.8.1" in states('sensor.energy_meter') %}
          {{ states('sensor.energy_meter').split("(")[1].split("*")[0] | float }}
          {% endif %}

The problem here is that the sensor is updated each time a line is received on the serial, and if the line doesn’t match, the state is indeed set to nothing.

You can likely fix this with a triggered sensor template.

template:
  - trigger:
    - platform: template
      value_template: >
         {{ '1-0:1.8.1' in states('sensor.energy_meter') }}

    sensor:
      - name: "Energy current use"
        unit_of_measurement: "kWh"
        state: >
          {{ states('sensor.energy_meter').split("(")[1].split("*")[0] | float }}
1 Like

Hi Chris,
many thanks for your fast and appropriate reaction. It works now!

Marcel

For the record:
I have now a serial connection between Home assistant running on a Raspberry Pi and a Landis & Gyr electricity meter, using the P1 port / P1 protocol. Note that a simple inverter, involving a standard transistor and 2 resistors, is needed to invert the serial signal. It is also required to enable the serial port (under HASS OS, it is not enabled), requiring to edit the config.txt file to add the lines:

enable_uart = 1
dtoverlay=pi3-miniuart-bt

The second line is related to bluetooth and frankly I am not sure it is required here. I do not use bluetooth and the serial port works with this.

  • Marcel

I am also doing something similar and it works. I just have an additional requirement and I am not sure how to implement it.
I want to set the value of the sensor to be unavailable or at least 0 if no information is received via the serial port for say x minutes.
At the moment is just displays the current value if no new value is received.