Format value for sending in JSON HTTP POST

I’m trying to send some info from my ESP to a display using json via http post, but I’m having trouble formatting the value before sending.

The value is currently a float with 6 decimal places and needs to be converted to either a float with only one decimal place or a formatted string.

This is what I have so far, but it won’t compile (error: cannot convert ‘float’ to ‘char*’)

Here’s what I have so far (and tempFormat is set as a std::string global variable)

interval:
  - interval: 30s
    then:
    - lambda: |-
        tempFormat = sprintf ( id(aht20_temp).state, "%.1f");

  - interval: 30s
    then:
    - http_request.post:
        url: !secret magicmirror_main
        headers:
          Content-Type: application/json
        json: |-
          root["temp"] = tempFormat;
          root["sensorId"] = id(sensorID);
        verify_ssl: false

I also tried this with no success.

tempFormat = std::to_string( round ( id(aht20_temp).state * 10.0 ) / 10.0);

I figured out a solution… or at least a partial one. You can round the value using a filter on the sensor end.

It looses the .0 for whole numbers, which I would like to have. So I’d still welcome any other suggestion to fix the string code above.

sensor:
  - platform: aht10
    temperature:
      name: "${device_code} AHT20 Temperature"
      id: aht20_temp
      accuracy_decimals: 1
      filters:
      - lambda: return round( x * 10.0 ) / 10.0;
    update_interval: 30s

...

interval:
  - interval: 30s
    then:
    - http_request.post:
        url: !secret magicmirror_main
        headers:
          Content-Type: application/json
        json: |-
          root["temp"] = id(aht20_temp).state;
          root["sensorId"] = id(sensorID);
        verify_ssl: false