Mqtt data is type string - how to use in Influx?

Really struggling here. I have 2 sensors being read and would like to record data into influx and use in Grafana etc however I cannot. I suspect this is due to it being string rather than a float. Any ideas on how to convert to a float?

Regards

image

That’s not it. All home assistant states are strings.

Shows how much I know then haha

Can you see why I’d have no value in these 2. Ive chosen one as value and one as state. I know data is coming in as have plotted the sensor using an Apex charts card

value is the one you want in the query. What is the dashboard time set to?

I think it defaults to 1h (top right of the graph area).

Can you show the state in Home Assistant Developer Tools → States?

When did it last change?

Yea seems 1 hr. Just discovered if i choose ‘Flux’ instead of ‘InfluxQL’ up the top, I can see stuff. Maybe my configs are wrong somewhere…

Oh, that has the unit in the state. We need to remove that. Can you please post this as formatted text rather than a screenshot:

image

sensor:
- platform: mqtt
  state_topic: "home/tank/level"
  name: "Tank Level"
  unit_of_measurement: "L"
  
- platform: mqtt
  state_topic: "home/tank/percent"
  name: "Tank Percent"
  unit_of_measurement: "%"

Ohhh. My Esp32 is sending the ‘%’ and ‘L’ with the value

  // MQTT can only transmit strings, convert
  String ps = String((float)percent) + " % ";

  // PUBLISH to the MQTT Broker (topic = percent_topic, defined at the beginning)
  if (client.publish(percent_topic, String(ps).c_str())) {
    Serial.println("Percentage sent!");
  }

I was about to suggest adding device_class: to the MQTT sensor definition, but there’s not really a “level” type in the list available.

You posted your MQTT data after I replied, but I suggest you need a value template to transform from JSON into a number with a decimal point.

My favourite MQTT analyser to investigate JSON formatting issues is
https://mqtt-explorer.com/ .

If this helps, :heart: this post!

1 Like

Sorry! Did I just delete your message!?

Thanks. I did look into templates but couldn’t get it to work or get my head around it. I’’ try removing the extra symbols and go from there

Can you remove that from your ESP32?

Otherwise it’s easy enough to remove in Home Assistant with a value template as Derrick suggested.

Yep! Late here. I’ll keep you posted over the next few days. Thanks friend

It’s best if you stop your ESP from sending the unit in the state but if you cant then this will do it in Home Assistant:

sensor:
- platform: mqtt
  state_topic: "home/tank/level"
  name: "Tank Level"
  unit_of_measurement: "L"
  value_template: "{{ value|replace('L','') }}"
  
- platform: mqtt
  state_topic: "home/tank/percent"
  name: "Tank Percent"
  unit_of_measurement: "%"
  value_template: "{{ value|replace('%','') }}"

Sorry - I was researching links for my reply, and Tom replied first making my original draft a bit obsolete! :slight_smile:

Great that several folk are all helping at once!

Thanks again. Such quick responses! Will look into this. Out for a couple days but back into it after :grinning:

I went the other way in a custom Python MQTT watering controller - publishing HASS autodiscovery topics to MQTT so that once started, the sensors and actuators “just appear” in HASS. Yes, it took a bit more work than a value template! :slight_smile:

I reformatted my published sensor data (simple numbers are easier than JSON) and manually sent discovery topics via mqtt_pub until HASS “just worked” with units and device_class. Quite similar to defining the same in configuration.yaml.

A useful Python example is Thom Dietrick’s Flora daemon as this includes both implementation and links to several different standards docs.

Wow this is really cool. I’m picking this is similar to say installing tasmota onto an Esp? It just ‘appears’ within HA, saying it has discovered a new device. Similarly you publish the discovery topic using the tool within HA and from that point on it talks to your sensor as it is sending the messages in correct format?

Is you’re watering controller an arduino or Esp type device? If you don’t mind, I’d love to check out your code that end just to get my head around it :relaxed:.

You’ve got it in one - same HASS discovery as Tasmota (well, pre v-12 SetOption19 0 that is).

The hardware is based on a RPi Zero and a Pimoroni Grow hat running Python.
Note - due to the shortage of RPi, Pimoroni have produced a new version based on the new RPi PicoW uP. Not looked if the uP has microPython Paho MQTT support.

The code is a fork of Pimoroni’s example code with MQTT support added for both sensor publish and motor control using the Paho MQTT client. It should really be up in GitHub, but it’s not yet!

The Python data marshalling code isn’t all that readable, so instead, here’s a dump of one channel MQTT discovery:

homeassistant/sensor/glz-rpi-grow_chan1/config
{
  "name": "glz-rpi-grow Sunlight",
  "state_topic": "tele/glz-rpi-grow/SENSOR",
  "value_template": "{{value_json.Lux}}",
  "availability_topic": "tele/glz-rpi-grow/LWT",
  "payload_available": "Online",
  "payload_not_available": "Offline",
  "unique_id": "glz-rpi-grow_lux",
  "device": {
    "identifiers": [
      "glz-rpi-grow"
    ],
    "name": "glz-rpi-grow",
    "manufacturer": "Pimoroni",
    "model": "Grow",
    "sw_version": "0.2",
    "suggested_area": "Conservatory"
  },
  "icon": "mdi:weather-sunny",
  "device_class": "illuminance",
  "unit_of_measurement": "lux",
  "expire_after": "1200",
  "force_update": "true"
}

This looks complex, but it’s really just the same parameters needed for a manual MQTT sensor in an ordered dictionary. As you say, this is something Tasmota has been doing for years on ESP12F!

It was tested using command line MQTT commands (different sensor for variety):

mosquitto_pub -d -h broker.local --username 'user' --pw "password" -t 'homeassistant/sensor/grow-1_chan1/config' --retain -m '{"name":"Grow-1 Channel 1", "stat_t":"tele/grow-1/SENSOR", "val_tpl":"{{value_json.Channel1}}", "avty_t":"tele/grow-1/LWT", "pl_avail":"Online", "pl_not_avail":"Offline", "uniq_id":"grow-1_chan1", "dev": {"ids":["grow-1"], "name":"Grow 1", "mf":"Pimoroni", "mdl":"Grow", "sw":"0.2", "sa":"Conservatory"}, "icon":"mdi:sprout", "dev_cla":"humidity", "unit_of_meas":"%", "expire_after":"1200", "frc_upd": true}'

Basically, tweak the config on the command line, publish, and the endpoint appears in HASS - magic! :mage:t2: :slight_smile:

Publish similar for all sensors and switches and it “just works” with endpoints appearing INSTANTLY (really took about a week to test).

And links to the documentation used:

Thanks again. Very interesting.

Also to anyone interested. Removing the ‘L’ and ‘%’ when publishing allowed me to see the info within influx :blush: