MQTT Auto Configuration publish JSON payload for Number type

Hello,

I’m trying to create a auto-config via MQTT. I’m good on the sensor side (read only from device) but I’m struggling with the configuration of number inputs, that should trigger a publish the set value to a command topic. So far I have:

{
    "name": "Output Power Limit",
    "cmd_t": "iot/73bkTV/{{ device_id }}/properties",
    "cmd_tpl": "{'properties': {'outputLimit': value}",
    "stat_t": "~outputLimit",
    "uniq_id": "{{ device_id }}-outputLimit",
    "dev_cla": "power",
    "unit_of_meas": "W",
    "max": 800,
    "min": 0,
    "step": 1,
    "dev": {...},
    "~": "solarflow-hub/{{ device_id }}/telemetry/"
}

Upon change of the number input this should publish a message to the “cmd_t” with a JSON payload (cmd_tpl?) that looks like this:

{ "properties": {"outputLimit": <value of number input>} } 

How can I do this? Any ideas?
Thanks!

Post the entire script that you are using to publish this Discovery payload.

template.json

{
    "name": "Output Power Limit",
    "cmd_t": "iot/73bkTV/{{ device_id }}/properties/write",
    "cmd_tpl": "{\"properties\": {\"outputLimit\": value}}",
    "stat_t": "~outputLimit",
    "uniq_id": "{{ device_id }}-outputLimit",
    "dev_cla": "power",
    "unit_of_meas": "W",
    "max": 800,
    "min": 0,
    "step": 1,
    "dev": {
      "identifiers": ["{{ device_id }}"],
      "manufacturer": "Zendure",
      "model": "Solarflow",
      "name": "Solarflow Hub",
      "sw_version": "{{ fw_version }}"
    },
    "~": "solarflow-hub/{{ device_id }}/telemetry/"
}

Code to publish:

    def pushHomeassistantConfig(self):
        hatemplates = [f for f in pathlib.Path().glob("homeassistant/*.json")]
        environment = Environment(loader=FileSystemLoader("homeassistant/"), undefined=DebugUndefined)

        for hatemplate in hatemplates:
            template = environment.get_template(hatemplate.name)
            hacfg = template.render(device_id=self.deviceId, fw_version=self.fwVersion)
            cfg_type = hatemplate.name.split(".")[0]
            cfg_name = hatemplate.name.split(".")[1]
            self.client.publish(f'homeassistant/{cfg_type}/solarflow-hub-{self.deviceId}-{cfg_name}/config',hacfg)