String Manipulation from a serial data feed

Evening,
I’m looking for some tutorage with my Home Assistant setup. I have been stalking around in the the background for quite a while. My Current HA is setup for lighting and power control and I’m very happy with it.

I want to bring in my weather station in to HA (It is a Vaisala WXT530) data. Mainly just as a place to record the historical data. It’s setup to stream 3 or 4 different Serial strings. I am looking for someone to help point the way to stripping the values out of them. :sunglasses: I’m done plenty of programming in older languages, but struggling with the syntex of the HA system (not used many “moden” languages).

The Raw data looks like these messages;

1R5,Vs=11.5V,Id=Hanover
1R1,Dn=150D,Dm=150D,Dx=150D,Sn=0.3K,Sm=0.5K,Sx=0.7K
1R2,Ta=24.9C,Ua=43.8P,Pa=1005.3H

Ta is current temp, Pa is pressure etc

I understand i need a template to my Configuration.yaml setup, but looking for help to find the correct commends and formats for searching and stripping the values out

  - platform: serial
    serial_port: /dev/ttyUSB0
    name: WXT530_data
    baudrate: 19200

Assuming that first string in each message is a unique identifier of the message type, here are a couple of examples to try:

template:
  - sensor:
      - name: Vs # or whatever you want to call it
        state_class: measurement
        device_class: voltage
        unit_of_measurement: V
        state: >
          {% if states('sensor.wxt530_data').split(',')[0] == '1R5' %}
            {{ states('sensor.wxt530_data').split(',')[1]|replace('Vs=','')|replace('V','') }}
          {% else %}
            {{ this.state }}
          {% endif %}

      - name: Ta # or whatever you want to call it
        state_class: measurement
        device_class: temperature
        unit_of_measurement: '°C' # or F
        state: >
          {% if states('sensor.wxt530_data').split(',')[0] == '1R2' %}
            {{ states('sensor.wxt530_data').split(',')[1]|replace('Ta=','')|replace('C','') }}
          {% else %}
            {{ this.state }}
          {% endif %}

You can find a list of device classes and units for your other sensors here: https://www.home-assistant.io/integrations/sensor/#device-class

The way these template sensors work is first grabbing the state of your serial sensor, e.g.

states('sensor.wxt530_data')'1R5,Vs=11.5V,Id=Hanover'

Then splitting this into a list at the commas, e.g.

['1R5','Vs=11.5V','Id=Hanover']

Then checking what the first element [0] of this list is (counting left to right starts at 0). If it matches the message we are looking for then extract the value we want from one of the other positions (e.g. [1] is the second element) in this list replacing everything except the number using the replace() function.

('Vs=11.5V')|replace('Vs=','')|replace('V','') → 11.5

If the first element does not match the string we want then keep the previous state {{ this.state }} of the template sensor.

Sorry for going AWOL, family Xmas time and all… :sunglasses:

This is awsome. Thank you for adding comments in the sections. I never know where i can mess with and what needs to stay :sunglasses: also for heh class link.

Sitting down to run the numbers now! wish me luck lol I’ll come back with my mess, so you can have a giggle :wink:

1 Like