Serial data template - help!

I have a serial sensor configured in my configuration. I’m having a hard time parsing the data with a template because the data gets sent as “state”. But a lot more data gets sent back that changes the state. Below is an example of what data I want. Is there anyway to parse this into separate values?

1 is the sensor #
TEST DEVICE is the name of the sensor
8178 is gallons
71.35 is inches
1.2 is inches
1550 is gallons

{
    "event_type": "state_changed",
    "data": {
        "entity_id": "sensor.serial_sensor",
        "old_state": {
            "entity_id": "sensor.serial_sensor",
            "state": "1   TEST DEVICE                8178   71.35     1.2   68.9     1550",
            "attributes": {
                "friendly_name": "Serial Sensor"
            },

edit: making things easier to understand

Yes, you can use a regular expression (regex) to extract the six values from the received data. To compose and test the regex pattern, regex101.com provides a handy online tool.

Thank you for the reply!
I am having so many issues trying to get this to work. My google history is just about in the hundreds trying to find an example similar to what I’m trying to do. Seems simple enough, but every example searches the sensor attributes and instead it is the sensor state that I am trying to parse.

I also think some of the problem that makes it more difficult for me is that when the device sends the data to the serial port, it sends more than one thing, making the state change multiple times. So I need to capture it during a specific state. God this is so confusing lol

use template that your friend

That worked! Well kinda. The data just flashes across the screen very fast. Each line of data from the device shows as a different state change so I need to filter it?.

When I manually request data from my device, using “Developer Tools - Events” I can see the data being sent in 9 different events, I need to parse the data from just one of the state change events. Below is the log of all the events that were heard when I manually requested data from my device. Thanks again for your help!

edit:
I’ve made some progress with filtering the data on an entity-filter card using:

state_filter:
  - operator: regex
    value: 1   TEST TANK

But that doesn’t help me at all.

Tried adding this to my configuration:

sensor:
  - platform: serial
    serial_port: /dev/ttyUSB0
    baudrate: 9600
    stopbits: 1
    name: rs232device
    
  - platform: template
    sensors:
      tank1:
        friendly_name: T1
        value_template: "{{ states('sensor.rs232device').value.split(' ') }}{{states('sensor.rs232device').value.split(' ')[4] }}"

Doesn’t do anything.

Here is the data received from serial port:


Event 9 fired 5:37 PM:

{
    "event_type": "state_changed",
    "data": {
        "entity_id": "sensor.rs232device",
        "old_state": {
            "entity_id": "sensor.rs232device",
            "state": "1   TEST TANK                8178   71.35     1.2   68.3     1550",
            "attributes": {
                "friendly_name": "rs232device"
            },
            "last_changed": "2021-01-26T00:37:36.870591+00:00",
            "last_updated": "2021-01-26T00:37:36.870591+00:00",
            "context": {
                "id": "bcfdb623fda2ac5facd0a1eb6e6a3760",
                "parent_id": null,
                "user_id": null
            }
        },
        "new_state": {
            "entity_id": "sensor.rs232device",
            "state": "",
            "attributes": {
                "friendly_name": "rs232device"
            },
            "last_changed": "2021-01-26T00:37:36.871574+00:00",
            "last_updated": "2021-01-26T00:37:36.871574+00:00",
            "context": {
                "id": "15410fd2788b0110a5685e8efff41980",
                "parent_id": null,
                "user_id": null
            }
        }
    },
    "origin": "LOCAL",
    "time_fired": "2021-01-26T00:37:36.871574+00:00",
    "context": {
        "id": "15410fd2788b0110a5685e8efff41980",
        "parent_id": null,
        "user_id": null
    }
}

Event 8 fired 5:37 PM:

{
    "event_type": "state_changed",
    "data": {
        "entity_id": "sensor.rs232device",
        "old_state": {
            "entity_id": "sensor.rs232device",
            "state": "",
            "attributes": {
                "friendly_name": "rs232device"
            },
            "last_changed": "2021-01-26T00:37:36.791012+00:00",
            "last_updated": "2021-01-26T00:37:36.791012+00:00",
            "context": {
                "id": "d4c99884008387ed34463a8864fe8c22",
                "parent_id": null,
                "user_id": null
            }
        },
        "new_state": {
            "entity_id": "sensor.rs232device",
            "state": "1   TEST TANK                8178   71.35     1.2   68.3     1550",
            "attributes": {
                "friendly_name": "rs232device"
            },
            "last_changed": "2021-01-26T00:37:36.870591+00:00",
            "last_updated": "2021-01-26T00:37:36.870591+00:00",
            "context": {
                "id": "bcfdb623fda2ac5facd0a1eb6e6a3760",
                "parent_id": null,
                "user_id": null
            }
        }
    },
    "origin": "LOCAL",
    "time_fired": "2021-01-26T00:37:36.870591+00:00",
    "context": {
        "id": "bcfdb623fda2ac5facd0a1eb6e6a3760",
        "parent_id": null,
        "user_id": null
    }
}

Event 7 fired 5:37 PM:

{
    "event_type": "state_changed",
    "data": {
        "entity_id": "sensor.rs232device",
        "old_state": {
            "entity_id": "sensor.rs232device",
            "state": "TANK  PRODUCT               GALLONS  INCHES   WATER  DEG F   ULLAGE",
            "attributes": {
                "friendly_name": "rs232device"
            },
            "last_changed": "2021-01-26T00:37:36.790041+00:00",
            "last_updated": "2021-01-26T00:37:36.790041+00:00",
            "context": {
                "id": "11ba175da5accfe5883d10e4f3f2ca17",
                "parent_id": null,
                "user_id": null
            }
        },
        "new_state": {
            "entity_id": "sensor.rs232device",
            "state": "",
            "attributes": {
                "friendly_name": "rs232device"
            },
            "last_changed": "2021-01-26T00:37:36.791012+00:00",
            "last_updated": "2021-01-26T00:37:36.791012+00:00",
            "context": {
                "id": "d4c99884008387ed34463a8864fe8c22",
                "parent_id": null,
                "user_id": null
            }
        }
    },
    "origin": "LOCAL",
    "time_fired": "2021-01-26T00:37:36.791012+00:00",
    "context": {
        "id": "d4c99884008387ed34463a8864fe8c22",
        "parent_id": null,
        "user_id": null
    }
}

Event 6 fired 5:37 PM:

{
    "event_type": "state_changed",
    "data": {
        "entity_id": "sensor.rs232device",
        "old_state": {
            "entity_id": "sensor.rs232device",
            "state": "",
            "attributes": {
                "friendly_name": "rs232device"
            },
            "last_changed": "2021-01-26T00:37:36.726777+00:00",
            "last_updated": "2021-01-26T00:37:36.726777+00:00",
            "context": {
                "id": "e05b7442cd113273dd9bacebcbbc865a",
                "parent_id": null,
                "user_id": null
            }
        },
        "new_state": {
            "entity_id": "sensor.rs232device",
            "state": "TANK  PRODUCT               GALLONS  INCHES   WATER  DEG F   ULLAGE",
            "attributes": {
                "friendly_name": "rs232device"
            },
            "last_changed": "2021-01-26T00:37:36.790041+00:00",
            "last_updated": "2021-01-26T00:37:36.790041+00:00",
            "context": {
                "id": "11ba175da5accfe5883d10e4f3f2ca17",
                "parent_id": null,
                "user_id": null
            }
        }
    },
    "origin": "LOCAL",
    "time_fired": "2021-01-26T00:37:36.790041+00:00",
    "context": {
        "id": "11ba175da5accfe5883d10e4f3f2ca17",
        "parent_id": null,
        "user_id": null
    }
}

Event 5 fired 5:37 PM:

{
    "event_type": "state_changed",
    "data": {
        "entity_id": "sensor.rs232device",
        "old_state": {
            "entity_id": "sensor.rs232device",
            "state": "01-23-21 17:38",
            "attributes": {
                "friendly_name": "rs232device"
            },
            "last_changed": "2021-01-26T00:37:36.725798+00:00",
            "last_updated": "2021-01-26T00:37:36.725798+00:00",
            "context": {
                "id": "cb71bdfc257ff972b5e903faa862ede5",
                "parent_id": null,
                "user_id": null
            }
        },
        "new_state": {
            "entity_id": "sensor.rs232device",
            "state": "",
            "attributes": {
                "friendly_name": "rs232device"
            },
            "last_changed": "2021-01-26T00:37:36.726777+00:00",
            "last_updated": "2021-01-26T00:37:36.726777+00:00",
            "context": {
                "id": "e05b7442cd113273dd9bacebcbbc865a",
                "parent_id": null,
                "user_id": null
            }
        }
    },
    "origin": "LOCAL",
    "time_fired": "2021-01-26T00:37:36.726777+00:00",
    "context": {
        "id": "e05b7442cd113273dd9bacebcbbc865a",
        "parent_id": null,
        "user_id": null
    }
}

Event 4 fired 5:37 PM:

{
    "event_type": "state_changed",
    "data": {
        "entity_id": "sensor.rs232device",
        "old_state": {
            "entity_id": "sensor.rs232device",
            "state": "",
            "attributes": {
                "friendly_name": "rs232device"
            },
            "last_changed": "2021-01-26T00:37:36.695225+00:00",
            "last_updated": "2021-01-26T00:37:36.695225+00:00",
            "context": {
                "id": "a818f87f917dbd2fa9f6cf159e2cff87",
                "parent_id": null,
                "user_id": null
            }
        },
        "new_state": {
            "entity_id": "sensor.rs232device",
            "state": "01-23-21 17:38",
            "attributes": {
                "friendly_name": "rs232device"
            },
            "last_changed": "2021-01-26T00:37:36.725798+00:00",
            "last_updated": "2021-01-26T00:37:36.725798+00:00",
            "context": {
                "id": "cb71bdfc257ff972b5e903faa862ede5",
                "parent_id": null,
                "user_id": null
            }
        }
    },
    "origin": "LOCAL",
    "time_fired": "2021-01-26T00:37:36.725798+00:00",
    "context": {
        "id": "cb71bdfc257ff972b5e903faa862ede5",
        "parent_id": null,
        "user_id": null
    }
}

Event 3 fired 5:37 PM:

{
    "event_type": "state_changed",
    "data": {
        "entity_id": "sensor.rs232device",
        "old_state": {
            "entity_id": "sensor.rs232device",
            "state": "MY TEST STATION",
            "attributes": {
                "friendly_name": "rs232device"
            },
            "last_changed": "2021-01-26T00:37:36.694253+00:00",
            "last_updated": "2021-01-26T00:37:36.694253+00:00",
            "context": {
                "id": "8bd897c309b24f040346ffdf8610afc2",
                "parent_id": null,
                "user_id": null
            }
        },
        "new_state": {
            "entity_id": "sensor.rs232device",
            "state": "",
            "attributes": {
                "friendly_name": "rs232device"
            },
            "last_changed": "2021-01-26T00:37:36.695225+00:00",
            "last_updated": "2021-01-26T00:37:36.695225+00:00",
            "context": {
                "id": "a818f87f917dbd2fa9f6cf159e2cff87",
                "parent_id": null,
                "user_id": null
            }
        }
    },
    "origin": "LOCAL",
    "time_fired": "2021-01-26T00:37:36.695225+00:00",
    "context": {
        "id": "a818f87f917dbd2fa9f6cf159e2cff87",
        "parent_id": null,
        "user_id": null
    }
}

Event 2 fired 5:37 PM:

{
    "event_type": "state_changed",
    "data": {
        "entity_id": "sensor.rs232device",
        "old_state": {
            "entity_id": "sensor.rs232device",
            "state": "201",
            "attributes": {
                "friendly_name": "rs232device"
            },
            "last_changed": "2021-01-26T00:37:36.583569+00:00",
            "last_updated": "2021-01-26T00:37:36.583569+00:00",
            "context": {
                "id": "07d4aaa59b58e297e19b1ac65a534221",
                "parent_id": null,
                "user_id": null
            }
        },
        "new_state": {
            "entity_id": "sensor.rs232device",
            "state": "MHP TEST STATION",
            "attributes": {
                "friendly_name": "rs232device"
            },
            "last_changed": "2021-01-26T00:37:36.694253+00:00",
            "last_updated": "2021-01-26T00:37:36.694253+00:00",
            "context": {
                "id": "8bd897c309b24f040346ffdf8610afc2",
                "parent_id": null,
                "user_id": null
            }
        }
    },
    "origin": "LOCAL",
    "time_fired": "2021-01-26T00:37:36.694253+00:00",
    "context": {
        "id": "8bd897c309b24f040346ffdf8610afc2",
        "parent_id": null,
        "user_id": null
    }
}

Event 1 fired 5:37 PM:

{
    "event_type": "state_changed",
    "data": {
        "entity_id": "sensor.rs232device",
        "old_state": {
            "entity_id": "sensor.rs232device",
            "state": "\u0003\u0001",
            "attributes": {
                "friendly_name": "rs232device"
            },
            "last_changed": "2021-01-26T00:37:36.582581+00:00",
            "last_updated": "2021-01-26T00:37:36.582581+00:00",
            "context": {
                "id": "93cf619ec59bcd299ed3fbdf862bc1b5",
                "parent_id": null,
                "user_id": null
            }
        },
        "new_state": {
            "entity_id": "sensor.rs232device",
            "state": "201",
            "attributes": {
                "friendly_name": "rs232device"
            },
            "last_changed": "2021-01-26T00:37:36.583569+00:00",
            "last_updated": "2021-01-26T00:37:36.583569+00:00",
            "context": {
                "id": "07d4aaa59b58e297e19b1ac65a534221",
                "parent_id": null,
                "user_id": null
            }
        }
    },
    "origin": "LOCAL",
    "time_fired": "2021-01-26T00:37:36.583569+00:00",
    "context": {
        "id": "07d4aaa59b58e297e19b1ac65a534221",
        "parent_id": null,
        "user_id": null
    }
}

Event 0 fired 5:37 PM:

{
    "event_type": "state_changed",
    "data": {
        "entity_id": "sensor.rs232device",
        "old_state": {
            "entity_id": "sensor.rs232device",
            "state": "",
            "attributes": {
                "friendly_name": "rs232device"
            },
            "last_changed": "2021-01-25T23:58:21.427304+00:00",
            "last_updated": "2021-01-25T23:58:21.427304+00:00",
            "context": {
                "id": "41a8bf6c4033ad068e6b9e0773db6931",
                "parent_id": null,
                "user_id": null
            }
        },
        "new_state": {
            "entity_id": "sensor.rs232device",
            "state": "\u0003\u0001",
            "attributes": {
                "friendly_name": "rs232device"
            },
            "last_changed": "2021-01-26T00:37:36.582581+00:00",
            "last_updated": "2021-01-26T00:37:36.582581+00:00",
            "context": {
                "id": "93cf619ec59bcd299ed3fbdf862bc1b5",
                "parent_id": null,
                "user_id": null
            }
        }
    },
    "origin": "LOCAL",
    "time_fired": "2021-01-26T00:37:36.582581+00:00",
    "context": {
        "id": "93cf619ec59bcd299ed3fbdf862bc1b5",
        "parent_id": null,
        "user_id": null
    }
}