Customize Templates not working?

Hi!
I’m having problems using templates while customizing entities.

This is how configuration.yaml looks. Neither of the customizations is working, but when i open the card for a sensor for example, it returns the template in clear text under the history for the component. Do i have to enable anything for this to work?

  customize:
    switch.fan_bathroom:
      friendly_name: Bathroom Fan
      icon: mdi:fan
      templates:
        icon: "if (state == 'on') return 'mdi:fan'; else return 'mdi:fan-off';"


  customize_glob:

    sensor.*_temperature:
      templates:
        rgb_color: >
          if (state > 25) {
            return [0, 128, 0];
          } else if (state >= 18 ) {
            return [223, 76, 30];
          } else {
            return [35, 76, 99];
          }

36

need 3 equal signs.

That looks like it should work. The brackets are unnecessary but that shouldn’t affect the outcome. maybe needs to be icon_color inside glob? EDIT: doesn’t look like it needs to be icon color…

 customize_glob:
   sensor.*_temperature:
     templates:
       rgb_color: >
         if (state > 25)
           return [0, 128, 0];
         else if (state >= 18)
           return [223, 76, 30];
         else
           return [35, 76, 99];

Yeah, I originally tried with 3 equal signs, but tried with 2 and forgot to change it back.

It should work, as I copied the last bit off of this forum. So it’s rather odd that it’s not working for me. Other customizations works fine, but they are not made with templating.

I can’t get any customizations with templating to work either. Did you ever find a solution?

try:

  templates:
    icon: >
      if (state === 'on') return 'mdi:music';
      return 'mdi:music-off';
    icon_color: >
      if (state === 'on') return 'rgb(251, 210, 41)';
      return 'rgb(54, 95, 140)';

and/or:

  templates:
    icon_color: >
      if (state > 1500) return 'rgb( 192, 39, 30 )';
      if (state > 150) return 'rgb( 244, 101, 35 )';
      if (state > 70) return 'rgb( 255, 194, 15 )';
      if (state > 0) return 'rgb(251, 210, 41)';
      return 'rgb(54, 95, 140)';

use icon_color instead of rgb_color

1 Like

I am having the same problem. No customization inside a template works.

example:

# Customizations
  customize:
    sensor.snmp_wan_in_volume:
      icon: mdi:download
    sensor.snmp_wan_out_volume:
      icon: mdi:upload
    sensor.ha_db_file_size:
      icon: mdi:file
    input_boolean.coffee_override_switch:
      templates:
        icon: >
          if (state === 'on') return 'mdi:run-fast';
          return 'mdi:eye-off';
        icon_color: >
          if (state === 'on') return 'rgb(251, 210, 41)';
          return 'rgb(54, 95, 140)';


input_boolean:
  coffee_override_switch:             
    name: Override Automation (once)  
    initial: off

I don’t get it. The icon is not set, and the color doesn’t change.

The config gives no errors.

Could this be an HA bug? Anyone?

Note to self.

Templating inside ‘customize’ requires some sort of ‘add on’ called CustomUI

… which adds this feature.

So: if all you were trying to do is change the color of an input_boolean based on state and toggle some other element visibility, consider using a switch.template element instead.

1 Like

Has anyone found a solution? I’m still struggling with this. None of my icons or colors get adjusted and yes, I have CustomUI installed. Thanks

works just fine, give us your code and we can help

Thanks. I tried pretty much everything, but nothing works. All icons and colors remain unchanged.
Here are some of the versions I tried:

sensor.broadlink_s1c_living_front:
  friendly_name: Living Front Door
  show_last_changed: true
  icon: mdi:door-open
  templates:
    rgb_color: if (state === 'closed') return [51, 153, 255]; else return [255, 0,0];
    icon_template: '{{ ''mdi:door-open'' if is_state(''sensor.broadlink_s1c_living_front'',''open'') else ''mdi:glassdoor'' }}'
      
sensor.broadlink_s1c_living_side:
  friendly_name: Living Side Door
  icon: mdi:door-open
  show_last_changed: true
  templates:
    icon_color: "if (state === 'closed') return 'rgb(51, 153, 255)'; else return 'rgb(255, 0, 0)';"
    icon_template: "{{ 'mdi:door-open' if is_state('sensor.broadlink_s1c_living_side', 'open') else 'mdi:door-open' }}"
    
sensor.broadlink_s1c_office_motion:
  friendly_name: Office Motion
  show_last_changed: true
  icon: mdi:walk
  templates:
    rgb_color: "if (state === 'no_motion') return [#5294E2]; else return [#E45E65];"
    icon_template: '{{ ''mdi:run-fast'' if is_state(''sensor.broadlink_s1c_office_motion'', ''motion'') else ''mdi:walk'' }}'
      
sensor.broadlink_s1c_office_front:
  friendly_name: Office Front Door
  show_last_changed: true
  icon: mdi:door-closed
  templates:
    icon_template: >
      if (state === 'open') return 'mdi:door-open';
      return 'mdi:door-closed';
    icon_color: >
      if (state === 'open') return 'red';
      return 'green';  
      
sensor.broadlink_s1c_office_back:
  friendly_name: Office Back Door
  show_last_changed: true
  icon: mdi:door-closed
  templates:
    rgb_color: "{{ [255, 0, 0] if is_state('sensor.broadlink_s1c_office_back', 'open') else [51, 153, 255] }};"
    icon_template: "{{ 'mdi:door-open' if is_state('sensor.broadlink_s1c_office_back', 'open') else 'mdi:door-closed' }};"

first of all, rgb_color isn’t supported any longer, so you can take that out.
2nd: have you installed the custom-ui?
3d: dont use icon: and a template for the icon in the sensor. You have that in all of the above examples…

if you have a template sensor, you set the icon_template in the declaration of the sensor itself. If you use another entity, you set the icon template in customize:

  templates:
    icon: >
      if (state === 'on') return 'mdi:music';
      return 'mdi:music-off';
    icon_color: >
      if (state === 'on') return 'rgb(251, 210, 41)';
      return 'rgb(54, 95, 140)';

Thanks Marius.
In my examples, I have just used all the different versions of users above, but none of them works.
2nd: yes, I have installed it. Is there a way to check if the installation was successful?
3rd: if I understand correctly, I should not use icon: and template: together, ie. either or?

So I have adjusted my customize.yaml to this, but still no joy:

sensor.broadlink_s1c_office_front:
  friendly_name: Office Front Door
  show_last_changed: true
#  icon: mdi:door-closed
  templates:
    icon: >
      if (state === 'open') return 'mdi:door-open';
      return 'mdi:door-closed';
    icon_color: >
      if (state === 'open') return 'red';
      return 'green';

i think this should work…
that being said, why version HA are you on? Right now, Ive updated to HA 0.81.2, and many customizations are off.

only thing I could say from here is you have to check if the states are really ‘open’ . Then again, if they wouldn’t be ‘open’ the else would have to check, which would be the ‘green’ and ‘door-closed’.

this customization is perfect, so if its not working, something else in the system/ setup isnt correct, or a bug is going on.

I’m on Hassio 0.81.2
I’m starting to wonder if CustomUI is properly installed. Nothing shows up on System although I followed the manual installation instructions perfectly. How could I check and how could I fix the installation?

you mean this:

27 which is shown when you click

15 ?

if not then you should indeed check. do you have the .update.sh in your config directory? you know how to run it?

still, I fear the lates HA version has some issues with customize, because in both my installs customizations have flukes. And the truly annoying thing is they are not identical…while my customizations are.

Hi again Marius,

Yes when I click on the “i” sign it doesn’t show me that CustomUI is installed, so I guess it doesn’t run.
I followed the instructions here: https://github.com/andrey-git/home-assistant-custom-ui/blob/master/docs/installing.md and tried both manual installation and running update.sh
I added customizer: custom_ui: hosted to my config and everything else. I can’t figure out why it wouldn’t run.
I have now also updated to 0.81.5 but no change. So weird.

does it matter that I have another custom component running (Broadlink)?

I test like this and it works. You need to check the state from its entity_id whether ‘on, off’ or ‘open, close’

homeassistant:
  customize:  
    binary_sensor.office_motion_sensor:
      friendly_name: Office Motion
      show_last_changed: true
      templates:
        icon: >
          if (state === 'on') return 'mdi:run-fast';
          return 'mdi:walk';
        icon_color: >
          if (state === 'on') return 'rgb(255, 0, 0)';
          return 'rgb(0, 255, 0)';
    binary_sensor.office_door_sensor:
      friendly_name: Office Door
      show_last_changed: true
      templates:
        icon: >
          if (state === 'on') return 'mdi:door-open';
          return 'mdi:door';
        icon_color: >
          if (state === 'on') return 'rgb(255, 0, 0)';
          return 'rgb(255, 255, 0)';

I do have another custom_component running Broadlink which is climate component.

no it doesnt.

Ive been using it for so long, I forgot how to install…
first, do the automatic instal via .update.sh (you have to create the file first and make it executable)

that would place all necessary files in the correct folders.

that isn’t enough though, as I found out setting up a spanking fresh install.

one also needs the frontend to load the custom card via:

frontend:
  extra_html_url:
    - /local/custom_ui/state-card-custom-ui.html

I do that in a frontend.yaml file which is called i the configuration.yaml via

frontend: !include frontend.yaml

as a final step, to make thing easy and forget, one can set the custom-ui to be used globally in HA, and never have to specify it again via this setting in customize_glob.yaml

'*.*':
  custom_ui_state_card: state-card-custom-ui

again, I call this customize_glob.yaml in configuration.yaml:

homeassistant:
  # Name of the location where Home Assistant is running
  name: Home
  latitude: !secret latitude
  longitude: !secret longitude
  elevation: !secret elevation
  unit_system: metric
  time_zone: !secret time_zone
  customize: !include_dir_merge_named customize
  customize_glob: !include customize_glob.yaml
  packages: !include_dir_named packages


customizer: !include customizer.yaml
frontend: !include frontend.yaml

Hope this helps!