Mqtt publish_json how to convert sensor.state in string

Help for beginner

i need to send the state of a sensor in string

#define sensor
sensor:
    # used as mqtt publisher
  - platform: uptime
    id: upti
    name: "${device_id} Uptime"
    update_interval: ${publish_interval}
    on_value:
      - mqtt.publish_json:
          topic: domoticz/in
          payload: |-
            root["command"] = "addlogmessage";
            root["message"] = "Connexion MQTT depuis ESPHOME";
      - logger.log: MQTT log domoticz published
    
  #---dallas sensors
  - platform: dallas
    id: temp_piece 
    name: "temperature-chaufferie"
    address: 0xAF012033B79C9F28
    on_value:
    #---Publisg sensors  content to domoticz via mqtt
      - mqtt.publish_json:
          topic: domoticz/in
          payload: |-
            root["idx"] = 45;
            root["nvalue"] = 0;
            root["svalue"] = id(temp_piece).state;
      - logger.log: MQTT Temp piece published
      
 

Give:
{“command”:“udevice”,“idx”:45,“nvalue”:0,“svalue”:26.75}

Domoticz need sensor value between quotes:
{“command”:“udevice”,“idx”:45,“nvalue”:0,“svalue”:“26.75”}

Thanks for Help
Philippe

Welcome to the forum. Please edit your post and format your pasted code correctly as per point 11 here.

first : in fact mqtt.publish_json make the conversion from float to string

but quotes around the values are missing

so, I find a solution, i am not sure that it is the smarter one !!

i change the way of ,publishing,
instead of using mqtt.publish_json , i use mqtt.publish,
and i construct the payload by myself

  # Dallas sensors
  - platform: dallas
    id: temp_piece 
    name: "temperature-chaufferie"
    address: 0xAF012033B79C9F28
    on_value:
    #---Publisg sensors  content to domoticz via mqtt
      - mqtt.publish:
          topic: domoticz/in
          payload: !lambda |-
            std::string messageO;
            char str[16];
            snprintf(str, sizeof(str), "%.2f", x);
            messageO += "{\"command\":\"udevice\",\"idx\":45,\"nvalue\":0,\"svalue\":\"";
            messageO += str;
            messageO += "\"}";
            return messageO;
      - logger.log: MQTT Temp piece published

and this give
sensor value between quotes:

{“command”:“udevice”,“idx”:45,“nvalue”:0,“svalue”:“26.75” }