MQTT to UART text sensor

Hi all!!
I am using ESPHOME and trying to send a mqtt value to uart text senor. How can I transmit a mqtt received value to uart text sensor??? I have already succeeded to receive mqtt values. I have already succeeded to write to uart text sensor an ASCII character.

Any advise will be appreciated.

I have the following error:

/config/esphome/test.yaml: In lambda function:
/config/esphome/test.yaml:115:25: error: conversion from ‘esphome::mqtt_subscribe::MQTTSubscribeSensor*’ to non-scalar type ‘std::__cxx11::string’ {aka ‘std::__cxx11::basic_string’} requested

my yaml file has the following code among other:
mqtt:
broker: …
username: …
password: …
birth_message:
topic: sensor/$devicename/status
payload: online
will_message:
topic: sensor/$devicename/status
payload: offline

sensor:

  • platform: mqtt_subscribe
    name: “temp”
    id: temp
    topic: Measurements/temp

logger:
level: VERBOSE #makes uart stream available in esphome logstream
baud_rate: 115200

uart:
id: uart_bus
tx_pin: 17
rx_pin: 16
baud_rate: 115200
debug:
direction: BOTH
dummy_receiver: false
after:
delimiter: “\n”
sequence:
- lambda: UARTDebug::log_string(direction, bytes);

interval:

  • interval: 2s
    then:
    • uart.write:
      data: !lambda |-
      std::size_t pos;
      std::string str = id(temp);
      while ((pos = str.find("\r")) != std::string::npos)
      str.replace(pos, 2, “\r”);
      std::vector<uint8_t> vec(str.begin(), str.end());
      return vec;

First of all, please format your message, eg enclose your code in back ticks (= `) cause how it’s shown now is just hard to read.

Now your question, how to copy the text value received from MQTT to a text sensor:

text_sensor:
  - platform: template
    id: uart_sensor

mqtt:
  # ...
  on_message:
    - topic: Measurements/temp
      then:
        - text_sensor.template.publish:
            id: uart_sensor
            state: !lambda "return x;"

I see that with text sensor you mean writing text to the uart.
In that case the following should work:

mqtt:
  ...
  on_message:
    - topic: Measurements/temp
      then:
        - uart.write: !lambda
            const std::vector<unsigned char> vec(x.begin(), x.end()); 
            return vec;

Thank you for your answer. I will check it, and I let you know.

I am writing what I am trying to achieve.
I want a esp32 to subscribe to some mqtt topics. The received data will be transmitted via serial communication to a Texas Instruments TMS320F2837D device.
At the other hand the Texas Instruments TMS320F2837D device will be transmitting other data, through serial communication, back to esp32, and then the esp32 will transmit them to specific MQTT topics in the same MQTT broker.
The above communication is already working in my esp32 and I want to translate my code to work in home assistant through esphome.
So I have already manage to subscribe to mqtt topics and now I am trying to send them to serial.