Custom Hardware Communication using MQTT

Hi All, I am very new to HA, so I’m not sure if this is the correct forum / category for my questions. I have created some ESP32 hardware that is publishing to mqtt in raw text data, not using json. I have added a sensor card to the dashboard but cannot get any connection to the mqtt broker. I add a sensor in config.yaml as below

sensor: 
  - platform: mqtt 
  name: "Tank 2" 
  unique_id: tank_2_level 
  state_topic: "Bills_Mesh/master/tank2/level" 
  unit_of_measurement: "%" 
  value_template: "{{ state.value.level }}"

I suspect the value_template is not correct but can’t find any examples to know what to use, all examples I have found are using JSON formatted data.

Any help would be greatly appreciated

What is published to Bills_Mesh/master/tank2/level?

We would need to see an example of the data published to the topic to be able to help you.

It should be something like :
Bills_Mesh/master/tank2/level 75.0

 char tempString[8];
 dtostrf(incomingReadings.float1, 1, 2, tempString);
 reconnect_mqtt(); // Reconnect if we have lost connection to broker
 client.publish(incomingReadings.topic, tempString); // topic, payload

Last example on this page: MQTT Sensor - Home Assistant

Well that is not particularly helpful. we were after an example of an actual post to Bills_Mesh/master/tank2/level. But if it is a simple string, what @robertklep said!

Thanks Robertklep, I had seen that example before. Maybe I don’t need any value_template. But I can’t seem to get any data from the mqtt broker in HA. I get “unknown” for the sensor, or Entity is non-numeric".

Published to Bills_mesh/master/tank2/level : 0.00

Then you don’t need a value template. Or just use this is you want to.

value_template: "{{ value }}"

As the publishing happens within the publish command of pubsubclient the topic and payload are separated and so the payload is simply a float value converted to a string and the topic is “Bills_Mesh etc”. The mqtt explorer shows the value coming in fine. I don’t know exactly what to put into my config.yaml to have the sensor card recognize it.
Thanks again for the help

So if I don’t need a value template, then is it that this template simply parses my data if I need to transform it in some way, and my problem is somewhere else. I don’t seem to be getting the data from the mqtt broker.

Yes, exactly. Hopefully your config isn’t really indented like this:

Correct, I don’t yet know how to format the code in this forum, but it is indented correctly I believe. from name to value_template it is indented 2 more spaces

The tank level data is published to the mqtt as a string, “75.00” for example. Is it possible that for the sensor to recognize it I need to convert it back to a float or int, and if so, how?

Even with quotes this should register something.

sensor: 
  - platform: mqtt 
    name: "Tank 2" 
    unique_id: tank_2_level 
    state_topic: "Bills_Mesh/master/tank2/level" 
    unit_of_measurement: "%" 

thanks Tom , sorry I can’t get back to this until sunday, but i think i need to look at the sensor card that i added, maybe something is wrong there. i will post some info about that when i can get back to it. Thanks again.

Hi Tom and others. I managed to get one sensor display to work since yesterday, The available entities in the entities list when I go to add a sensor seem to be completely different to anything I have added in my config.yaml file. Below is the yaml file.

# Loads default set of integrations. Do not remove.
default_config:

# Text to speech
tts:
  - platform: google_translate

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml

switch:
  - platform: mqtt
    name: "Pump"
    unique_id: tank2_pump_state
    state_topic: "Bills_mesh/slave/tank2/pump"
    command_topic: "Bills_mesh/slave/tank2/pump"
    payload_on: "1"
    payload_off: "0"
    retain: false
    
sensor:
  - platform: mqtt
    name: "Tank 2"
    unique_id: tank_2_level
    state_topic: "Bills_mesh/master/tank2/level"
    unit_of_measurement: "%"
    value_template: "{{ states(sensor.level)|float(0) }}"
#    command_topic: "Bills_Mesh/slave/tank2/range"
#    payload: "50"
#    native_value: "50"
#    initial: 0
#    min: 0
#    max: 100
#    step: 1
#    retain: false
    qos: 1
# Example configuration.yaml entry
  - platform: mqtt
    name: "Tank 2 Level"
    unique_id: tank2_level
    state_topic: "Bills_mesh/master/tank2/level"
    unit_of_measurement: "%"
    value_template: "{{ value }}"
  - platform: mqtt
    name: "Temperature"
    unique_id: tank2_temperature
    state_topic: "Bills_Mesh/master/tank2/temperature"
    unit_of_measurement: "°C"
    value_template: "{{ value_json.temperature }}"
  - platform: mqtt
    name: "Humidity"
    unique_id: tank2_humidity
    state_topic: "Bills_mesh/master/tank2/humidity"
    unit_of_measurement: "%"
    value_template: "{{ value.humidity }}"

As you can see the unique_id is tank2_pump_state, tank_2_level, tank2_level, tank2_temperature and tank2_humidity.
The list of available entities in the drop down list when adding a new sensor card is sensor.tank_2, sensor.tank_2_2, sensor.temperature and sensor.humidity.
I have added sensors for these and the tank_2 is the only one that works, the tank_2_2 and humidity and temperature do not work. and I have added sensors for sensor.tank_2_level which also does not work.
There seems to be a discrepancy between the unique_id that I have given the sensors in the yaml file and the names available in the drop down list. I have no idea where it has come up with the names tank_2 and tank_2_2, but tank_2 is the only one that works.
Can anyone make some sense out of this and let me know how the system creates the list of available entities from my config.yaml file.


You can see that there are two sensor displays with level of 8%, both with sensor.tank_2 as the entity and all other are “unknown” or “not available”.
The pump control card is working.
BTW, I can control and monitor all mqtt topics in mqtt explorer and mqtt dash from my phone, and also sent and receive with the broker from my ESPnow wifi mesh hardware.

Thanks
Regards
Bill

Hi Again,
I have made some good progress. It didn’t seem to like the value_template, and it definitely didn’t like my incorrect spelling in the temperature topic. I still don’t know how to refer to the data value from the broker within the value_template, but that’s another question. Here is my config.yaml

switch:
  - platform: mqtt
    name: "Pump"
    unique_id: tank2_pump_state
    state_topic: "Bills_mesh/slave/tank2/pump"
    command_topic: "Bills_mesh/slave/tank2/pump"
    payload_on: "1"
    payload_off: "0"
    retain: false
    
sensor:
  - platform: mqtt
    name: "Tank 2 Level"
    unique_id: tank2_level
    state_topic: "Bills_mesh/master/tank2/level"
    unit_of_measurement: "%"
#    value_template: "{{ value }}"
  - platform: mqtt
    name: "Temperature"
    unique_id: tank2_temperature
    state_topic: "Bills_mesh/master/tank2/temperature"
    unit_of_measurement: "°C"
#    value_template: "{{ value_json.temperature }}"
  - platform: mqtt
    name: "Humidity"
    unique_id: tank2_humidity
    state_topic: "Bills_mesh/master/tank2/humidity"
    unit_of_measurement: "%"
#    value_template: "{{ value.humidity }}"

And the new dashboard :

Thanks for the help.
I have created a ESPnow wifi mesh that sends to my router with a ESP32 gateway. All working pretty well, devices auto-detect, Control and monitor, now ready for the next step. I am happy to share my stuff if anyone is interested. It is written in C on arduino with ESP8266 and ESP32.
Regards
Bill