Mariusthvdb / custom-ui

Add templates and icon_color to Home Assistant UI
161 stars 30 forks source link

MQTT sensors and dynamic icon and icon color #92

Closed jjvelar closed 1 year ago

jjvelar commented 1 year ago

Hi,

I am trying to add dynamic icon and icon color to an MQTT sensor but it shows an error when verifying the configuration.

Here you've got the code and the error. How can I fix it? Thanks!

   - name: inverter_bat_status_description
      state_topic: Inverter/Bat_Status_Description
      unique_id: inverter_bat_status_description_id
      icon: >
        {{'mdi:check-bold' if this.state === 'Running' else 'mdi:alert-circle'}}
      attributes:
        icon_color: >
          {{'green' if this.state === 'Running' else 'red'}}

Invalid config for [mqtt]: [attributes] is an invalid option for [mqtt]. Check: mqtt->mqtt->sensor->31->attributes.

Mariusthvdb commented 1 year ago

this is an MQTT sensor issue. read the error: it says the attributes: option is not allowed for MQTT sensor.

you can however use the

homeassistent:
  customize:
    sensor.inverter_bat_status_description:
      templates:
        icon_color:

version and use a correct js template

jjvelar commented 1 year ago

Thanks for your quick reply. You guys provide the best customer support.

Tried this but icon is not showing. And if I remove the icon piece, color is also not turning into green or red.:

  customize:
    sensor.inverter_bat_status_description:
      templates:
        icon: >
          {{'mdi:check-bold' if states('sensor.inverter_bat_status_description') == 'Running' else 'mdi:alert-circle'}}
        icon_color: >
          {{'green' if states('sensor.inverter_bat_status_description') == 'Running' else 'red'}}

Any help will be highly appreciated. Thanks in advance.

Mariusthvdb commented 1 year ago

check the docs... as said, you need js templates not the Jinja template we use in HA template sensors.

homeassistant:
  customize:
    sensor.inverter_bat_status_description:
      templates:
        icon: >
          return (state === 'Running') ? 'mdi:check-bold' : 'mdi:alert-circle';
        icon_color: >
          return (state === 'Running') ? 'green' : 'red';
jjvelar commented 1 year ago

That worked. Thanks a lot, Marius.