UART debug sensor to Home Assistant

I added UART debug logging to my device

and the information perfectly shows in the webinterface.
however I would like to have this send to HA as well so I can analyze the data later.
Is this possible?

usually when my device shows an error I don’t have time to immediately examine,
I would be great if I can use history to see the previous statusses

You could publish the messages to a text sensor.

    - lambda: |-
          UARTDebug::log_string(direction, bytes);  //Still log the data
          
          int sensorID=0;
          float sensorTEMP=0; 
          
          //Example to convert uart text to string
          std::string str(bytes.begin(), bytes.end());

          //watch for potential problems with non printable or special characters in string
          id(rawString).publish_state(str.c_str());

When I try this my esphome device crashes,
I think it’s because I’m reading hex data instead of string.

Can you show your logs and yaml?

You can probably adapt the code from the hex logger into your lamda.

Eg instead of ESP_LOGD do a publish_state.

https://esphome.io/api/uart__debugger_8cpp_source

void UARTDebug::log_hex(UARTDirection direction, std::vector<uint8_t> bytes, uint8_t separator) {
   std::string res;
   if (direction == UART_DIRECTION_RX) {
     res += "<<< ";
   } else {
     res += ">>> ";
   }
   size_t len = bytes.size();
   char buf[5];
   for (size_t i = 0; i < len; i++) {
     if (i > 0) {
       res += separator;
     }
     sprintf(buf, "%02X", bytes[i]);
     res += buf;
   }
   ESP_LOGD(TAG, "%s", res.c_str());
   delay(10);
 }

Alternatively you can do it the custom sensor way.

really really cool,
that works

    debug:
      direction: BOTH
      dummy_receiver: true
      after:
        delimiter: "\n"
      sequence:
        #https://community.home-assistant.io/t/uart-debug-sensor-to-home-assistant/722853
        - lambda: |-
            static const char *const TAG = "uart_debug";
            uint8_t separator = ' ';
            std::string res;
            if (direction == UART_DIRECTION_RX) {
              res += "<<< ";
            } else {
              res += ">>> ";
            }
            size_t len = bytes.size();
            char buf[5];
            for (size_t i = 0; i < len; i++) {
              if (i > 0) {
                res += separator;
              }
              sprintf(buf, "%02X", bytes[i]);
              res += buf;
            }
            ESP_LOGD(TAG, "%s", res.c_str());
            id(UartStatus).publish_state(res.c_str());
            delay(10);

then luckily HA now has the export function since the value updates really rapidly.

not sure what you want to see in the log,
it’s a bunch of hex messages to control my coffee maker.

21:10:07	[D]	[uart_debug:058]	

>>> 01 02 00 02 00 00 00 11 36 D5 55 00 01 02 00 02 00 00 00 11 36 D5 55 00 01 02 00 02 00 00 00 11

21:10:07	[D]	[uart_debug:058]	

>>> 00 00 00 11 36 05 24 24 04 14 D5 55 00 01 02 00 02 00 00 00 11 36 D5 55 00 01 02 00 02 00 00 00

21:10:07	[D]	[uart_debug:058]	

<<< D5 55 00 07 07 07 07 00 00 00 00 00 38 00 00 00 00 37 28

21:10:07	[D]	[uart_debug:058]	

>>> 01 02 00 02 00 00 00 11 36 D5 55 00 01 02 00 02 00 00 00 11 36 D5 55 00 01 02 00 02 00 00 00 11

21:10:07	[D]	[uart_debug:058]	

>>> 36 D5 55 00 01 02 00 02 00 00 A0 64 FE D5 55 00 01 02 00 02 00 00 00 11 9B

but I’m glad it works,
the next time my machine shows an error I can write down the time and then later check the data of byte[15]

[edit:] unfortunately about after 10 minutes the ESP8266 still crashes,
I think maybe the amount of logging is just to much to handle for the ESP.
so for now I keep it at weblogging.

1 Like