Data transmission via UART (string to number)

Hi, guys!

I have an Arduino UNO to which an ORP sensor is connected. I am transmitting data via UART to ESP32 (ESP Home). To do this, I use a Custom UART Text Sensor. Everything is displayed well in the Home Assistant. But I can’t figure out how to replace the code in order to get a numeric value.
I would be grateful if someone could help me…

YAML

esphome:
  name: uart-orp
  includes:
    - uart_orp.h
esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:
  level: VERBOSE #makes uart stream available in esphome logstream
  baud_rate: 0 #disable logging over uart

uart:
  id: uart_bus
  tx_pin: GPIO17
  rx_pin: GPIO16
  baud_rate: 9600
# Enable Home Assistant API
api:
  encryption:
    key: ""

ota:
  password: ""

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

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Uart-Orp Fallback Hotspot"
    password: "bGKBD3U2Neox"

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"
    name: "ORP Sensor"
    

uart_orp.h

#include "esphome.h"

class UartReadLineSensor : public Component, public UARTDevice, public TextSensor {
 public:
  UartReadLineSensor(UARTComponent *parent) : UARTDevice(parent) {}

  void setup() override {
    // nothing to do here
  }

  int readline(int readch, char *buffer, int len)
  {
    static int pos = 0;
    int rpos;

    if (readch > 0) {
      switch (readch) {
        case '\n': // Ignore new-lines
          break;
        case '\r': // Return on CR
          rpos = pos;
          pos = 0;  // Reset position index ready for next time
          return rpos;
        default:
          if (pos < len-1) {
            buffer[pos++] = readch;
            buffer[pos] = 0;
          }
      }
    }
    // No end of line has been found, so return -1.
    return -1;
  }

  void loop() override {
    const int max_line_length = 80;
    static char buffer[max_line_length];
    while (available()) {
      if(readline(read(), buffer, max_line_length) > 0) {
        publish_state(buffer);
      }
    }
  }
};

I am pretty sure all states in HA are text.

What are you seeing in Home Assistant? Screenshot of the states screen for that sensor please.

It may just need a unit_of_measurement added in order to render as a graph, if that’s what you want.

2022-10-28_14-05-32
2022-10-28_14-11-52

As I know, [unit_of_measurement] is an invalid option for text_sensors

You can add it by customizing the entity in Home Assistant: Customizing entities - Home Assistant

I didn’t really understand how Customizing entities can help in solving my problem. Could you give an example, please

2022-10-28_18-19-51
Now I use the another card type Sensor, but I want change it to History Graph (but it works fine only with numbers)

I found a solution. In Custom Sensor I replaced publish_state(buffer) with publish_state(atof(buffer)), and in the yaml file I made a numeric sensor instead of a text one

1 Like