MQTT Number Entity Failing Auto Discovery

Hi, I’m having issues with MQTT discovery of a custom device. I’ve been able to discover custom sensor entities, but when I tried publishing number entities, they aren’t being discovered by Home Assistant.

When looking at the MQTT logs, it appears that I am encountering some exception likely to do with how I’m defining the config topic. This is from:

tail -f config/home-assistant.log | grep -i mqtt

2022-12-21 21:23:47.112 ERROR (MainThread) [homeassistant.util.logging] Exception in async_discover when dispatching 'mqtt_discovery_new_number_mqtt': ({'device_class': 'temperature', 'state_topic': 'homeassistant/number/Magtag-c7fd1aa11bab/state', 'step': 1.0, 'unique_id': 'Magtag-c7fd1aa11bab_Magtag_CO2_Temp_Offset', 'unit_of_measurement': '°C', 'device': {'model': 'Adafruit MagTag with ESP32S2', 'identifiers': 'c7fd1aa11bab', 'name': 'Magtag_CO2', 'sw_version': '7.3.3 on 2022-08-29', 'manufacturer': 'Espressif'}, 'name': 'Magtag_CO2_Temp_Offset', 'min': 0, 'max': 100, 'value_template': '{{ value_json.Magtag_CO2_Temp_Offset | round(0) }}', 'platform': 'mqtt'},)
  File "/usr/src/homeassistant/homeassistant/components/mqtt/mixins.py", line 328, in async_discover

Here is the config topic itself:

homeassistant/number/Magtag-c7fd1aa11bab_Magtag_CO2_Temp_Offset/config

Here’s the payload I’m sending on the config topic:

{
  "device_class": "temperature",
  "stat_t": "~/state",
  "step": 1,
  "uniq_id": "Magtag-c7fd1aa11bab_Magtag_CO2_Temp_Offset",
  "unit_of_meas": "°C",
  "dev": {
    "mdl": "Adafruit MagTag with ESP32S2",
    "ids": "c7fd1aa11bab",
    "name": "Magtag_CO2",
    "sw": "7.3.3 on 2022-08-29",
    "mf": "Espressif"
  },
  "name": "Magtag_CO2_Temp_Offset",
  "min": 0,
  "max": 100,
  "~": "homeassistant/number/Magtag-c7fd1aa11bab",
  "val_tpl": "{{ value_json.Magtag_CO2_Temp_Offset | round(0) }}"
}

Anyone know what I have wrong in my config topic payload??

What does the config topic itself look like?

Ah, I should have included that. Here is what the config topic looks like:

homeassistant/number/Magtag-c7fd1aa11bab_Magtag_CO2_Temp_Offset/config

I don’t don’t know for sure but I found (from others) to use {% raw %} to encapsulate templates. Here is an example:

"value_template": {% raw %}"{{ (value_json['message'] | from_json)['state'] }}"{% endraw %},

Thanks for the suggestion, I’ll give that a try. However, I was able to successfully discover a sensor using the same kind of value template. For example, here is the config topic and payload for a sensor that is auto discovered by MQTT. I just can’t figure out why a similar topic and payload won’t work for a number.

Topic:

homeassistant/sensor/Magtag-c7fd1aa11bab_Magtag_CO2_SCD30_CO2/config

Payload:

{
  "device_class": "carbon_dioxide",
  "stat_t": "~/state",
  "cmd_t": "~/cmd",
  "~": "homeassistant/sensor/Magtag-c7fd1aa11bab",
  "unit_of_meas": "ppm",
  "uniq_id": "Magtag-c7fd1aa11bab_Magtag_CO2_SCD30_CO2",
  "stat_cla": "measurement",
  "dev": {
    "mdl": "Adafruit MagTag with ESP32S2",
    "ids": "c7fd1aa11bab",
    "name": "Magtag_CO2",
    "sw": "7.3.3 on 2022-08-29",
    "mf": "Espressif"
  },
  "name": "Magtag_CO2_SCD30_CO2",
  "cmd_tpl": "{ 'co2_cal': {{ value }} }",
  "val_tpl": "{{ value_json.Magtag_CO2_SCD30_CO2 | round(0) }}"
}

Ahh, I found the issue. Looks like for a Number entity, the command topic is required:

After adding that to the config payload, discovery works fine.