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
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
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:
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, this post!
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!
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
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!
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 .
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!
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