Howto write float sensor state to uart

Hi

I need to write sensor values to my serial port (for an usecase where not wifi is possible).

I can write test using

- uart.write: 'Hello World'

but

- uart.write: !lambda
                  return id(temperature).state;

wont work:

src/main.cpp:140:27: error: could not convert ‘temperature->esphome::sensor::Sensor::state’ from ‘float’ to ‘std::vector’

Where can I find a sprintf function or something similar to just write the float to uart.


My whole configuration:

esphome:
  name: bluetooth_tracker
  platform: ESP32
  board: nodemcu-32s

esp32_ble_tracker:
    
sensor:
  - platform: xiaomi_cgg1
    mac_address: xxxx
    temperature:
      name: "Xiaomi CGG1 Temperature"
      id: temperature
      on_value:
        - uart.write: 'Hello World'
        - uart.write: !lambda  
                          return id(temperature).state;
        - uart.write: '\r\n'
    humidity:
      name: "Xiaomi CGG1 Humidity"
    battery_level:
      name: "Xiaomi CGG1 Battery Level"
 
logger:

uart:
  baud_rate: 115200 
  tx_pin: GPIO1
  rx_pin: GPIO3

I found a solution myself

  on_value:
    - uart.write: !lambda  
                      char buf[128];
                      sprintf(buf, "Temperature: %.1f degrees \n", id(temperature).state);
                      std::string s = buf;
                      return std::vector<unsigned char>( s.begin(), s.end() );
3 Likes

@BenjaminS - thank you for this!

Helped me in a project :slight_smile: