(mqtt) sensor reporting distance in mm showing as string

I have this (lora-wan) sensor reporting a value in mm (that’s millimeter for the yanks in here), but when I look at the sensor in my dashboard the history graph shows it as string-values. How can I force it so see them as “integers”? I know mqtt only works with strings, but other sensors work…

add a unit_of_measurement to your mqtt sensor config. Or if it is discovered use Customize to add it.

      distance:
        unit_of_meas: "mm"
        state_class: measurement

Should be:

unit_of_measurement: "mm"

I think abbreviations are allowed, as shown in the documentation: MQTT - Home Assistant also it works fine for all the other sensors. I’ll try it to make sure, but I’m pretty sure that’s not it.

I did not know that.

It’s something else then.

Show your full sensor config.

It doesn’t show the mm in the dialog, so somehow it didn’t get registered. Or can you also use customize to remove it and is that what is happening here?

1 Like

It does say “190 mm” in the sensor view (see “Water Level”), it’s only when I click on it and look at the history it’s shown as strings and not int values. I guess is, it was created “wrong” and it stays that way even after I added the unit_of_meas: “mm” and state_class: measurement ?

I tried deleting the sensor, but then when it’s created again… it still uses the old values stored in the history db? I have not found a way to delete a single sensor in the (history) db yet.

     if sensor_not_exists(uniq_dev_id):
                payload = {
                    "platform": "mqtt",
                    "unique_id": uniq_dev_id,
                    "name": f"{dev_id} {key}",
                    "state_topic": f"ttn/{dev_id}/state",
                    "value_template": f"{{{{ value_json.{key} }}}}",
                    "qos": 0,
                    "retain": True,
                    "force_update": True,
                    "device": {
                        "identifiers": dev_id,
                        "manufacturer": "Dragino",
                        "model": dev_cfg['device'],
                        "name": dev_cfg['name']}
                }
                if get_dev_cla(key) is not None:
                    payload['dev_cla'] = get_dev_cla(key)
                if get_unit_of_meas(key) is not None:
                    payload['unit_of_meas'] = get_unit_of_meas(key)

                topic = f"homeassistant/sensor/ttn/{dev_id}_{key}/config"
                mqtt.publish(
                    topic=topic,
                    payload=dumps(payload),
                    retain=True
                )

Thanks @tom_l and @Edwin_D I think I found it!

“distance” != “Distance” :man_facepalming:

1 Like