MQTT Discovery and icon_template

Hey community!

I’m a bit stuck. Or sleep deprived. Or something.

I’m trying to get discovery via MQTT working, but I want to be able to publish the icon_template value. It works fine with a normal icon. I believe it’s possible on a normal sensor, although the documentation suggests it only working for a binary sensor?

Here’s the error I get when I rebooted the server (you know, in case: turn it off and on again):

2021-01-11 22:28:33 ERROR (MainThread) [homeassistant.util.logging] Exception in async_discover_sensor when dispatching ‘mqtt_discovery_new_sensor_mqtt’: ({‘name’: ‘Battery State of Charge (GE)’, ‘state_topic’: ‘givenergy/cloud/summary’, ‘availability_topic’: ‘givenergy/cloud/status’, ‘payload_available’: ‘Online’, ‘payload_not_available’: ‘Offline’, ‘json_attributes_topic’: ‘givenergy/cloud/summary’, ‘unit_of_measurement’: ‘%’, ‘value_template’: “{{value_json[‘soc’]}}”, ‘icon_template’: “{%- if value_json[‘soc’] <= ‘100’ and value_json[‘soc’] > ‘96’ and states.sensor.battery_power_2.state >= ‘0’ %} mdi:battery {% elif value_json[‘soc’] <= ‘95’ and value_json[‘soc’] > ‘90’ and states.sensor.battery_power_2.state >= ‘0’ %} mdi:battery-90 {% elif value_json[‘soc’] <= ‘89’ and value_json[‘soc’] > ‘80’ and states.sensor.battery_power_2.state >= ‘0’ %} mdI:battery80 {% elif value_json[‘soc’] <= ‘79’ and value_json[‘soc’] > ‘70’ and states.sensor.battery_power_2.state >= ‘0’ %} mdi:battery70 {% elif value_json[‘soc’] <= ‘69’ and value_json[‘soc’] > ‘60’ and states.sensor.battery_power_2.state >= ‘0’ %} mdi:battery60 {% elif value_json[‘soc’] <= ‘59’ and value_json[‘soc’] > ‘50’ and states.sensor.battery_power_2.state >= ‘0’ %} mdi:battery50 {% elif value_json[‘soc’] <= ‘49’ and value_json[‘soc’] > ‘40’ and states.sensor.battery_power_2.state >= ‘0’ %} mdi:battery40 {% elif value_json[‘soc’] <= ‘39’ and value_json[‘soc’] > ‘30’ and states.sensor.battery_power_2.state >= ‘0’ %} mdi:battery30 {% elif value_json[‘soc’] <= ‘29’ and value_json[‘soc’] > ‘20’ and states.sensor.battery_power_2.state >= ‘0’ %} mdi:battery20 {% elif value_json[‘soc’] <= ‘19’ and value_json[‘soc’] > ‘10’ and states.sensor.battery_power_2.state >= ‘0’ %} mdi:battery10 {% elif value_json[‘soc’] <= ‘09’ and value_json[‘soc’] > ‘06’ and states.sensor.battery_power_2.state >= ‘0’ %} mdi:battery10 {% elif value_json[‘soc’] <= ‘5’ and value_json[‘soc’] > ‘0’ and states.sensor.battery_power_2.state >= ‘0’ %} mdi:batteryoutline {% elif value_json[‘soc’] <= ‘100’ and value_json[‘soc’] > ‘96’ and states.sensor.battery_power_2.state < ‘0’ %} mdi:flash:battery-charging-100 {% elif value_json[‘soc’] <= ‘95’ and value_json[‘soc’] > ‘90’ and states.sensor.battery_power_2.state < ‘0’ %} mdi:battery-charging-90 {% elif value_json[‘soc’] <= ‘89’ and value_json[‘soc’] > ‘80’ and states.sensor.battery_power_2.state < ‘0’ %} mdi:battery-charging-80 {% elif value_json[‘soc’] <= ‘79’ and value_json[‘soc’] > ‘70’ and states.sensor.battery_power_2.state < ‘0’ %} mdi:battery-charging-70 {% elif value_json[‘soc’] <= ‘69’ and value_json[‘soc’] > ‘60’ and states.sensor.battery_power_2.state < ‘0’ %} mdi:battery-charging-60 {% elif value_json[‘soc’] <= ‘59’ and value_json[‘soc’] > ‘50’ and states.sensor.battery_power_2.state < ‘0’ %} mdi:battery-charging-50 {% elif value_json[‘soc’] <= ‘49’ and value_json[‘soc’] > ‘40’ and states.sensor.battery_power_2.state < ‘0’ %} mdi:battery-charging-40 {% elif value_json[‘soc’] <= ‘39’ and value_json[‘soc’] > ‘30’ and states.sensor.battery_power_2.state < ‘0’ %} mdi:battery-charging-30 {% elif value_json[‘soc’] <= ‘29’ and value_json[‘soc’] > ‘20’ and states.sensor.battery_power_2.state < ‘0’ %} mdi:battery-charging-20 {% elif value_json[‘soc’] <= ‘19’ and value_json[‘soc’] > ‘10’ and states.sensor.battery_power_2.state < ‘0’ %} mdi:battery-charging-10 {% elif value_json[‘soc’] <= ‘09’ and value_json[‘soc’] > ‘06’ and states.sensor.battery_power_2.state < ‘0’ %} mdi:battery-charging-10 {% elif value_json[‘soc’] <= ‘5’ and value_json[‘soc’] > ‘0’ and states.sensor.battery_power_2.state < ‘0’ %} mdi:battery-charging-outline {%- endif %}”, ‘unique_id’: ‘G345-I-BattSoC-1’, ‘device_class’: ‘power’, ‘device’: {‘identifiers’: [‘G345-I-BattSoC_1’], ‘manufacturer’: ‘GivEnergy’}, ‘platform’: ‘mqtt’},)

When pushing JSON icon, I’ve tried the HA sensor state data, my most recent attempt has been the value_json node, and I’ve tried escape characters, without escape characters, swapping the ’ for a " …

Here’s the JSON string I’m passing in MQTT/config:

{
    "name": "Battery State of Charge (GE)",
    "stat_t": "givenergy/cloud/summary",
    "avty_t": "givenergy/cloud/status",
    "pl_avail": "Online",
    "pl_not_avail": "Offline",
    "json_attr_t": "givenergy/cloud/summary",
    "unit_of_meas": "%",
    "val_tpl": "{{value_json['soc']}}",
    "icon_template": "{%- if value_json[\"soc\"] <= \"100\" and value_json[\"soc\"] > \"96\" and states.sensor.battery_power_2.state >= \"0\" %} mdi:battery {% elif value_json[\"soc\"] <= \"95\" and value_json[\"soc\"] > \"90\" and states.sensor.battery_power_2.state >= \"0\" %} mdi:battery-90 {% elif value_json[\"soc\"] <= \"89\" and value_json[\"soc\"] > \"80\" and states.sensor.battery_power_2.state >= \"0\" %} mdI:battery80 {% elif value_json[\"soc\"] <= \"79\" and value_json[\"soc\"] > \"70\" and states.sensor.battery_power_2.state >= \"0\" %} mdi:battery70 {% elif value_json[\"soc\"] <= \"69\" and value_json[\"soc\"] > \"60\" and states.sensor.battery_power_2.state >= \"0\" %} mdi:battery60 {% elif value_json[\"soc\"] <= \"59\" and value_json[\"soc\"] > \"50\" and states.sensor.battery_power_2.state >= \"0\" %} mdi:battery50 {% elif value_json[\"soc\"] <= \"49\" and value_json[\"soc\"] > \"40\" and states.sensor.battery_power_2.state >= \"0\" %} mdi:battery40 {% elif value_json[\"soc\"] <= \"39\" and value_json[\"soc\"] > \"30\" and states.sensor.battery_power_2.state >= \"0\" %} mdi:battery30 {% elif value_json[\"soc\"] <= \"29\" and value_json[\"soc\"] > \"20\" and states.sensor.battery_power_2.state >= \"0\" %} mdi:battery20 {% elif value_json[\"soc\"] <= \"19\" and value_json[\"soc\"] > \"10\" and states.sensor.battery_power_2.state >= \"0\" %} mdi:battery10 {% elif value_json[\"soc\"] <= \"09\" and value_json[\"soc\"] > \"06\" and states.sensor.battery_power_2.state >= \"0\" %} mdi:battery10 {% elif value_json[\"soc\"] <= \"5\" and value_json[\"soc\"] > \"0\" and states.sensor.battery_power_2.state >= \"0\" %} mdi:batteryoutline {% elif value_json[\"soc\"] <= \"100\" and value_json[\"soc\"] > \"96\" and states.sensor.battery_power_2.state < \"0\" %} mdi:flash:battery-charging-100 {% elif value_json[\"soc\"] <= \"95\" and value_json[\"soc\"] > \"90\" and states.sensor.battery_power_2.state < \"0\" %} mdi:battery-charging-90 {% elif value_json[\"soc\"] <= \"89\" and value_json[\"soc\"] > \"80\" and states.sensor.battery_power_2.state < \"0\" %} mdi:battery-charging-80 {% elif value_json[\"soc\"] <= \"79\" and value_json[\"soc\"] > \"70\" and states.sensor.battery_power_2.state < \"0\" %} mdi:battery-charging-70 {% elif value_json[\"soc\"] <= \"69\" and value_json[\"soc\"] > \"60\" and states.sensor.battery_power_2.state < \"0\" %} mdi:battery-charging-60 {% elif value_json[\"soc\"] <= \"59\" and value_json[\"soc\"] > \"50\" and states.sensor.battery_power_2.state < \"0\" %} mdi:battery-charging-50 {% elif value_json[\"soc\"] <= \"49\" and value_json[\"soc\"] > \"40\" and states.sensor.battery_power_2.state < \"0\" %} mdi:battery-charging-40 {% elif value_json[\"soc\"] <= \"39\" and value_json[\"soc\"] > \"30\" and states.sensor.battery_power_2.state < \"0\" %} mdi:battery-charging-30 {% elif value_json[\"soc\"] <= \"29\" and value_json[\"soc\"] > \"20\" and states.sensor.battery_power_2.state < \"0\" %} mdi:battery-charging-20 {% elif value_json[\"soc\"] <= \"19\" and value_json[\"soc\"] > \"10\" and states.sensor.battery_power_2.state < \"0\" %} mdi:battery-charging-10 {% elif value_json[\"soc\"] <= \"09\" and value_json[\"soc\"] > \"06\" and states.sensor.battery_power_2.state < \"0\" %} mdi:battery-charging-10 {% elif value_json[\"soc\"] <= \"5\" and value_json[\"soc\"] > \"0\" and states.sensor.battery_power_2.state < \"0\" %} mdi:battery-charging-outline {%- endif %}",
    "uniq_id": "I-BattSoC-1",
    "device_class": "battery",
    "dev": {
        "ids": [
            "I-BattSoC_1"
        ],
        "mf": "GivEnergy"
    }
}

Hoping someone can tell me what I’m doing wrong, or if it’s unsupported (in which case this post is a lie).

He Bergals,

I am trying to do a similar thing and found your post. MQTT discovery is limited in what it can do, and it does not support all fields that could be configured in YAML (as was done in the linked post).

If you check the official documentation https://www.home-assistant.io/integrations/binary_sensor.mqtt/ you will not find ‘icon’ nor ‘icon_template’ there…

I don’t think it would be a big endeavor to have it added, just somebody needs to find time to do it :slight_smile:

1 Like

Well, I can only hope. It’d make it’d be a cheery on the cake!

MQTT Discovery supports “icon” in the JSON payload, it just does not support icon templates.
You have the list of supported fields there: MQTT Discovery - Home Assistant (home-assistant.io)

What you could do is creating a template sensor based on the MQTT one.

1 Like

device_class battery handles the icon changing based on percentages. So even if icon_template was available (its not), it still wouldn’t be needed.

I assume that if you actually force the icon, it would deactivate the automatic one, would it?
So in this case, the best approach would be to not specify the icon at all.

yes, that’s correct