Convert JSON String from UART to object

Using the custom UART text sensor example (exactly the same as this - Custom UART Text Sensor — ESPHome), I have managed to read a JSON string from the UART.

'uart_readline': Received new state {"MSG":1057",Vrms":0.00,"P1":0,"P2":0,"P3":0,"P4":0,"P5":0,"P6":0,"E1":0,"E2":0,"E3":0,"E4":0,"E5":0,"E6":0,"pulse":0}

I’d like to convert this to a JSON Object to split out the different returned values. I can see there is a json_utils file that other components make use of, but I cannot work out how to use this (or if I can). I also saw this brief note about using JSON as well ESPHome 2022.1.0 - 19th January 2022 — ESPHome

I’d like to be able to configure the sensors to be returned in the YAML as not all the data returned will be valid. Is it possible to make the object available and extract what is required like the MQTT component does? (MQTT Client Component — ESPHome)

My other option is to use strtok (on an alternative key:value pair format I can get) to split up the string.

C++ isn’t really my language so I’m struggling here and any help would be appreciated :frowning:

1 Like

Did you manage how to do it?
If yes, please attached the solution.

No I didn’t :frowning:

From the help I got on Discord, here is the solution for future reference.

esphome:
  name: roof-temperatures
  friendly_name: Roof-temperatures
  includes:
    - uart_read_line_sensor.h

esp8266:
  board: esp01_1m

# Enable logging
logger:
uart:
  id: uart_bus
  rx_pin: 3
  baud_rate: 9600

# Enable Home Assistant API
api:
  encryption:
    key: "xxxxx"

ota:
  password: "xxxx"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Roof-Temperatures"
    password: "kP30jrjNUc1C"

captive_portal:
## Global variables
## ---------------------------------------------------
globals:
  - id: my_custom_sensor
    type: std::string
    restore_value: no
    initial_value: '""'
json:

sensor:
  - platform: dht
    model: DHT22
    pin: GPIO0
    temperature:
      name: "Roof_inside_Temperature"
      id: roof_inside_temperature
    humidity:
      name: "Roof_inside__Humidity"
      id: roof_inside_humidity
    update_interval: 60s
  - platform: template
    name: "Tboiler"
    id: tboiler
  - platform: template
    name: "Tout"
    id: tout
  - platform: template
    name: "Tin"
    id: tin
  - platform: template
    name: "Tinavg"
    id: tinavg
  - platform: template
    name: "Toutavg"
    id: toutavg
  - platform: template
    name: "Diff"
    id: diff

text_sensor:
- platform: custom
  lambda: |-
    auto my_custom_sensor = new UartReadLineSensor(id(uart_bus));
    App.register_component(my_custom_sensor);
    return {my_custom_sensor};
  text_sensors:
    id: "uart_readline"
    on_value:
      then:
        - lambda: |-      
            json::parse_json(x, [](JsonObject root) {
              id(tboiler).publish_state(root["Tboiler"]);
              id(tout).publish_state(root["Tout"]);
              id(tin).publish_state(root["Tin"]);
              id(tinavg).publish_state(root["Tinavg"]);
              id(toutavg).publish_state(root["Toutavg"]);
              id(diff).publish_state(root["Diff"]);
            });








# Example:
# Tboiler=30.00Tout=28.88Tin=29.62Tinavg=34.06Toutavg=33.42Diff=0.65
# {"Tboiler":29.25,"Tout":29.71051,"Tin":30.17538,"Tinavg":70.06898,"Toutavg":70.03862,"Diff":0.030357}
1 Like

Excellent. Trouble with Discord, it just depends who happens to read the message.

1 Like

Correct.
It is pitty that there is no good search within the discord.
I think that it should be part of the esphome example, but I don’t know anyone who can put that into the help.

2 Likes

I suggest adding in device_class, state_class and unit_of_measurement to the template sensors as well.

Did you ask about it on the Discord Documentation channel? Can’t say I know how doc updates work other than noticing there’s a channel.

I agree the Discord search isn’t amazing, but it’s there.

It’s better for more interactive/real time comms than knowledge storage I reckon.

There’s definitely a more advanced pool of users over there, and I find I usually get a much quicker answer. Typically from ssieb :wink:

1 Like

if you are reffering to dev_documentation channel (Discord),
I understood that this channel is for development only, so I didn’t use that channel.
BTW, can you please let me know how you search within the discord?
Is there something like a “solution” (same as over here)?

My assumption is that it’s for documentation development. Which I think would include any examples on the ESPHome site. I’m not sure, but you could always ask. I believe they are always looking for documentation contributors (since the devs are so busy).

I usually just do a few keyword searches and scroll through results. Coming up with the right keywords is usually the hard bit (same with Google though right).

Hi,
I meant to use the webpage Search — ESPHome
It will show up only in the discord, but I don’t know how long it lasets.

1 Like

Yeah can’t say I used the ESPHOME page search much.

Usually I just use Google and include “ESPHome” in the search.

Anyway I agree the ESPHOME search experience isn’t fantastic overall…

1 Like