ESPhome data type

Dear experts,

I try to use ESPhome to publish a sensor value through MQTT. however, the sensor value data type is float, but MQTT publish can only accept string data type. Is there any way to convert the data type for ESPHOME configuration?

Errrā€¦ what?

MQTT only accept string data type as payload. Is there any way to convert float or int to string for ESPhome?

Iā€™m not really sure you have to. What does your ESPhome config look like?

Hi

My configuration as per below:

sensor:

  • platform: ultrasonic
    trigger_pin: D5
    echo_pin: D6
    name: ā€œUltrasonic Sensorā€
    update_interval: 1s
    id: sensor_ultrasonic
    on_value:
    then:
    • mqtt.publish:
      topic: ā€œsensor/esphome/ultrasonicā€
      payload: !lambda ā€œreturn id(sensor_ultrasonic).state;ā€

And with error message of:
src\main.cpp:563:33: error: could not convert ā€˜sensor_ultrasonic->esphome::ultrasonic::UltrasonicSensorComponent::.esphome::sensor::Sensor::stateā€™ from ā€˜floatā€™ to ā€˜std::string {aka std::basic_string}ā€™

Please edit your post and format it correctly. See point 11 here: How to help us help you - or How to ask a good question

Hi,

it is auto formatted without my control.

Yes. This is why I told you to read the link. It has instructions on how to overcome this.

Also I see your problem. Put some effort into learning how to use the forum and Iā€™ll tell you the answer. Youā€™ve been a member for 5 months and still not learned this. I donā€™t mean to be too much of a hard-arse but it helps us help you as we can not only see your config correctly but we can quickly copy and paste corrections for you as well.

Hi Tom,

Thanks very much for your comments. Sorry that I donā€™t have any programming background, and tried wrong block comment ways until it is correct now.

Here is my configuration code, but get error message as below.

sensor:
  - platform: ultrasonic
    trigger_pin: D5
    echo_pin: D6
    name: "Ultrasonic Sensor"
    update_interval: 1s
    id: sensor_ultrasonic
    on_value:
      then:
      - mqtt.publish:
          topic: "sensor/esphome/ultrasonic"
          payload: !lambda "return id(sensor_ultrasonic).state;"

src\main.cpp: In lambda function:
src\main.cpp:573:33: error: could not convert ā€˜sensor_ultrasonic->esphome::ultrasonic::UltrasonicSensorComponent::.esphome::sensor::Sensor::stateā€™ from ā€˜floatā€™ to ā€˜std::string {aka std::basic_string}ā€™
return sensor_ultrasonic->state;
^
src\main.cpp:574:3: warning: control reaches end of non-void function [-Wreturn-type]
});
^
*** [.pioenvs\esp_slb\src\main.cpp.o] Error 1

Awesome. Thanks for fixing the code block formatting. It makes it a lot easier for us to help you.

Have you tried this:

sensor:
  - platform: ultrasonic
    trigger_pin: D5
    echo_pin: D6
    name: "Ultrasonic Sensor"
    update_interval: 1s
    id: sensor_ultrasonic

i.e. leaving the mqtt publishing bit out. The reason is that as soon as the sensor updates ESPhome will publish the value for you. No extra configuration required.

All you have to do is define your mqtt broker.

and not the api.

it will publish to the topic:

[device_name]/ultrasonic_sensor/state

Hi Tom,

Thanks very much.

I re-tried, and it works.

BTW, to feedback some findings.

Homeassistant is ok to auto discover the mqtt topic if it is configured with ā€œdiscovery_prefix: homeassistantā€.

My esphome configured device name is ā€œesp8266_testā€. I tried to subscribe mqtt topic ā€œesp8266_test/ultrasonic_sensor/stateā€, but it doesnā€™t work.

I tried to use ā€œesphome esp8266_test.yaml clean-mqttā€ command to clean the mqtt topics try to restart, I happened to find out that the terminal displays all the mqtt topics in full sting.

The full mqtt topic is ā€œhomeassistant/sensor/esp8266_test/ultrasonic_sensor/configā€. By subscribe the topic, i can see the full mqtt message detail:

{
  "unit_of_measurement": "m",
  "icon": "mdi:arrow-expand-vertical",
  "name": "Ultrasonic Sensor",
  "state_topic": "esp8266_test/sensor/ultrasonic_sensor/state",
  "availability_topic": "esp8266_test/status",
  "unique_id": "ESPsensorultrasonic_sensor",
  "device": {
    "identifiers": "cc50e3cdff94",
    "name": "esp8266_test",
    "sw_version": "esphome v1.13.6 Oct 28 2019, 17:36:42",
    "model": "PLATFORMIO_NODEMCUV2",
    "manufacturer": "espressif"
  }
}

From inside, I find the actual sensor topic is ā€œesp8266_test/sensor/ultrasonic_sensor/stateā€, and subscription of this topic, it works.

1 Like

The only other issue I see is that a 1 second update interval is quite fast. Do you really need updates that quickly for your application?

If not you can keep the update interval the same and add an averaging filter. The average of ten samples will smooth any noise in the results considerably, give you a 10 second update to home assistant (which may still be overkill depending on your application), with the bonus of reducing the size of your home assistant database and network traffic.

Thanks for your recommendation.

Yes, 1 second is over kill. I put 1 second is mainly due to i am new to the system, and doing some testing so see the response and to be family with it.

1 Like