Non-JSON MQTT payload

Hi! I have since a few years some esp8266/32 devices running, sending sensor values etc to a custom home automation backend using MQTT. As I don’t have time to continue development of that system, I’m considering switching to Home Assistant.

Problem is that my devices send a compact payload like this:

sp=294.22,pv=297.21,rt=149

First, values are in Kelvin, so I need to subtract 273.15 from them to display values in Celsius.
Second, it’s not JSON, so it’s harder to parse using value_template, if I understand it correctly.

What would you suggest? Is there a way to get this working without too much trouble, or should I just resign and update the sensors to send data in JSON?

It would be easier to parse if the data were in JSON format but it can still be done using the current format.

You can use use split to convert the string to a list, by first splitting on commas and then on the equal sign.

{{ value.split(',')[0].split('=')[1] }}

Alternately, use regex_findall_index to search for a pattern that includes sp= followed by numbers and then return those numbers.

{{ value | regex_findall_index("sp=(\d+\.?\d+)") }}

Use either one in the MQTT Sensor’s value_template, convert to a floating-point number, and then subtract 273.15.

# sensor:
- platform: mqtt
  name: "Your Temperature Sensor"
  state_topic: sensor/whatever
  value_template:  >
    {{ (value | regex_findall_index("sp=(\d+\.?\d+)") | float - 273.15) | round(2) }}
1 Like