ESPHome UART Parsing

My ESPHome configuration file is as below

esphome:
  name: flow_temp_reader
  friendly_name: Flow Temp Reader
  comment: Reads FlowTempSetpoint from serial and publishes to Home Assistant.
  compile_process_limit: 2

esp8266:
  board: nodemcuv2

# Disable serial logging so UART RX can be used
logger:
  baud_rate: 0

api:

ota:
  platform: esphome

wifi:
  ssid: xxxxxx
  password: xxxxx
  use_address: xxxx

captive_portal:

# UART hardware setup
uart:
  id: uart_bus
  rx_pin: GPIO3          # connect to TX of Arduino/Wemos
  baud_rate: 115200
  rx_buffer_size: 1024
  debug:
    direction: RX
    dummy_receiver: true
    after:
      delimiter: "\n"
    sequence:
      - lambda: UARTDebug::log_string(direction, bytes); 


text:
  - platform: template
    name: "Number type in"
    optimistic: true
    min_length: 0
    max_length: 16
    mode: text
    on_value:
      then:
        - sensor.template.publish:
            id: num_from_text
            state: !lambda |-
              auto n = parse_number<float>(x);
              return n.has_value() ? n.value() : NAN;

sensor:
  - platform: template
    id: num_from_text
    name: "Number from text"

yields a UART debug stream of data as below

[22:04:05.351][D][uart_debug:158]: <<< "ReadData 3 0 0<- ReadAck 3 0 F4\r\n"
[22:04:05.360][D][uart_debug:158]: <<< "\r\n"
[22:04:05.505][D][uart_debug:158]: <<< "\r\n"
[22:04:05.521][D][uart_debug:158]: <<< "Kamertemp: 20.00\r\n"
[22:04:05.527][D][uart_debug:158]: <<< "FlowTempSetpoint: 10.00\r\n"
[22:04:05.540][D][uart_debug:158]: <<< "DHWSetpoint: 0.00\r\n"
[22:04:05.555][D][uart_debug:158]: <<< "MaxRelModLevelSetting: 0.00\r\n"
[22:04:05.564][D][uart_debug:158]: <<< "\r\n"

I am trying to parse this UART stream of data in order to extract the value for the “FlowTempSetpoint” but have drawn a blank. Can anybody give me a nudge in the correct direction please?

This seems to have gotten a lot of attention here on the forum, re UART parsing in ESPHome:

Thanks @tim.plas yes from what I have researched I like the route of parsing the dummy receiver. My challenge at the moment is generating suitable lambda script that filters the stream text data. Still getting my head around the syntax, will try and find something close to my use case as a starting point.

If that’s all you receive, you could verify that the length is 25 bytes (“FlowTempSetpoint: 10.00\r\n”).
Then extract bytes 19-23. That would be dirty simple approach if the value is always double-digit decimal xx.xx

I have only ever parsed HEX and my skills with strings are disaster, but it could be something like this:

    sequence:
      - lambda: |-
          if (bytes.size() == 25) {
            // Extract bytes 19..23 
            char buf[6];  // 5 chars + null terminator
            for (int i = 0; i < 5; i++) {
              buf[i] = bytes[19 + i];
            }
            buf[5] = '\0';

            float value = atof(buf);
            ESP_LOGD("uart", "Extracted value %.2f", value);
          }

There are better approaches though…

Thanks @Karosm; it compiled (which is a great start for me :laughing: ) and whilst it is dirty it does extract the value I was looking for…would have to stress test it to see how resilient it is to fault conditions on the TX device. How would I pass this value to a HA entity? I am on baby steps but bit by bit I am managing to walk. I can see it is something to do with sensor.template.publish but can’t quite get the syntax correct to link up with the value parsed in the UART… thanks in advance

Make a plain template sensor

sensor:
  - platform: template
    id: flowts
    name: "Flow Temp Setpoint"
    unit_of_measurement: "°C"
    accuracy_decimals: 2

and update it from that lambda:

id(flowts).publish_state(value);

Amazing thankyou. So simple and worked a treat. Can be like playing with lego when you find the right blocks to build with but so infuriating when you are not sure were to look. Any good points of reference for building up some good foundation knowledge on ESPhome / yaml apart from trial and error!? Thanks again

Esphome documentation of course. It’s the only place where you find updated and valid information. This forum is good source as well, but esphome is evolving so fast, that lot of info gets outdated quickly.

And when you need help, open new topic instead of posting to some (years) old one.

1 Like

Thanks again :+1:

You are welcome.