Custom Icon

Any ideas why this is not displaying the icon?

I have a switch and if switch is on I want it to display one icon and if it’s off, I want it to display a different icon. I created the icons and they are in the right folder (I think) This is from my customize.yaml

switch.toothbrush:
  templates:
    entity_picture: "if (state === 'on' return '/local/icons/icons/toothbrush-on.png'; else return '/local/icons/icons/toothbrush-off.png';"

If I use:
entity_picture: /local/icons/icons/toothbrush.png it works.

The first one is just showing a lightning bolt.

Don’t if statements have to be enclosed in curly brackets? {% if... %}

OK look… it’s a little bit cryptic so I thought I’d post what I got working…

First changes to configuration.yaml

switch:
  - platform: mqtt
    name: "Toothbrush1"
    command_topic: "cmnd/sonoff1/power"
    state_topic: "stat/sonoff1/POWER"
    qos: 1
    payload_on: "ON"
    payload_off: "OFF"
    retain: true
  - platform: template
    switches:
      toothbrush:
        value_template: "{{ is_state('switch.toothbrush1', 'on') }}"
        turn_on:
          service: switch.turn_on
          data:
            entity_id: switch.toothbrush1
        turn_off:
          service: switch.turn_off
          data:
            entity_id: switch.toothbrush1
        entity_picture_template: >-
          {% if is_state('switch.toothbrush1', 'on') %}
            /local/icons/icons/toothbrush-on.png
          {% else %}
            /local/icons/icons/toothbrush-off.png
          {% endif %}

So this is a MQTT Sonoff Switch. I changed the name to toothbrush1 and added a tenplate switch toothbrush. (It’s a copy type switch and I added the icons as an entity_picture template.

Next, I edited customize.yaml:

switch.toothbrush1:
  hidden: true
switch.toothbrush:
  friendly_name: Toothbrush

Also, I didn’t need to edit groups.yaml but here is my switch section from that as well so all my switches show on one card in the frontend:

  Switch:
    name: Switch
    control: hidden
    entities:
      - switch.coffee_maker
      - switch.toothbrush
      - switch.wemo_switch

Hope this helps someone else
Also as a postscript… I set the toothbrush1 entity to be hidden from Google Assistant. See you make ONE tiny change and that changes 4 files!

1 Like

Yeah, sorry I wasn’t more helpful but I’m only just starting to get a hang of this jinja stuff and rather than give you a possibly incorrect example I thought I’d just point you in the right direction. Glad you got it sorted.

No worries, thanks for trying. I’d tried about a million different combinations before I posted and nothing worked. Frustrating a bit. Thought I could do it in customize but turns out you can’t.

Thanks anyway.

I have all my switches customized like this:

switch.sw_audio_auditorium_template:
  templates:
    icon: >
      if (state === 'on') return 'mdi:music';
      return 'mdi:music-off';

maybe its the png causing issues (have that in several settings also), you could try with an mdi icon first, just to see if the logic is correct.
if that is fine, then solving the issue of the png is the next challenge…

Ive set up all customizations to use the : > and next line definitions to get rid of possible issue using the double and single quotes.

(btw, don’t be distracted by the _template in the switch name, it has nothing to do with this customizing template, it is a template switch (combining several switching and state techniques) (have a switch.sw_audio_auditorium_cl also) just to make it clear for me which is which :wink:

That’s almost identical to my original post (missing the else) But I’m not using an MDI icon, I’m using a png and also tried a jpg.

Yep, i merely suggested the mdi, to test the logic, sometimes the templates don’t work because the used entity isn’t in the state you’d expect it to be…

if its any help, this does work:

sensor.trash_today:
  friendly_name: 'Trash today'
  templates:
    entity_picture: >
     if (state === 'papier') return '/local/mijnafvalwijzer/papier.png';
     if (state === 'gft' ) return '/local/mijnafvalwijzer/gft.png';
     if (state === 'plastic verpakkingsafval') return '/local/mijnafvalwijzer/plastic.png';
     if (state === 'restafval') return '/local/mijnafvalwijzer/restafval.png';
     return '/local/mijnafvalwijzer/kliko.png';
    _stateDisplay: >
     if (state === 'papier') return 'Papier';
     if (state === 'gft' ) return 'Gft';
     if (state === 'plastic verpakkingsafval') return 'Plastic';
     if (state === 'restafval') return 'Restafval';
     return 'Geen';

then again, its a sensor, and not a switch.
about the correct folder: /local is in fact /www … at least when on Hassio.

in the original switch definition you have the a picture template. what happens when You change that into:

  - platform: template
    switches:
      toothbrush:
        value_template: "{{ is_state('switch.toothbrush1', 'on') }}"
        turn_on:
          service: switch.turn_on
          data:
            entity_id: switch.toothbrush1
        turn_off:
          service: switch.turn_off
          data:
            entity_id: switch.toothbrush1
        entity_picture: >
          {% if is_state('switch.toothbrush1', 'on') %}
            /local/icons/icons/toothbrush-on.png
          {% else %}
            /local/icons/icons/toothbrush-off.png
          {% endif %}

I actually tried in the template checker in dev tools to see if the template was working and it was returning the correct png/jpg file. Honestly I tried so many things and restarted HA so many times… it’s hard to remember what I did and didn’t try.

In any case, what I posted above solved the problem and it’s working for me now.

Only problem is the icons are bigger than the MDI ones so I edited them to make them 25x25 like MDI but it seems that the system scales them up again anyway. LOL. So I think some people are using bigger icons with lots of white space (and I cropped mine back sigh)

Anyway, I don’t think I’ll bother about the icons unless there’s an easy fix.

1 Like

I’m on my iPad so I’m not seeing what’s different about your suggested changes compared to my working version?

Ah I’m using entity_picture_template and you say entity_picture

I got mine straight from the docs.

remove the 3 equal signs and place 2:

entity_picture: "if (state == 'on' return '/local/icons/icons/toothbrush-on.png'; else return '/local/icons/icons/toothbrush-off.png';"

EDIT: Unless that is normal for this code language??? I’ve noticed that 3 equal signs are everywhere. What language is this? That seems odd. Ignore this suggestion if that’s normal code…

I tried 2 but the DOCS show 3.

Here is what I followed BTW (that I got working as per above)

You’re missing a close bracket after ‘on’

1 Like

Yeah I’m sure I noticed that in the 1000 things I tried.

I have it working as per post 3 above.

I appreciate the help but unless anyone can either improve upon post 3 or better yet give me a way to just use customize to do this without using a platform template like I was originally wanting to do…

1 Like

HELP: Saw your post concerning ICONs. Noticed you experimented with .jpg and .png formats (any luck with .gif) I need to generate my own icons but have been unable to find anyone who has done that (until I saw your post). Communicated with OttoWinter (discordapp) but no luck. My internet service is so bad, ATT will guarantee NOTHING and I go down frequently (way out at the end of the line–yet another reason NOT to use the ‘Cloud’ for anything), I can’t depend on icons provided via the net (sometimes they arrive from MDI, sometimes from FAS and sometimes not). I have generated my own icons for HA Floorplan and hoped I could do the same for HA general use. (Running HA 0.86.3, ESPHome/Lovelace on a Rasp PI 3 B+). Any suggestions would be greatly appreciated.

did you try any of the picture templates in the post? Start there, second example.

Thank you for the rapid response. Please excuse my ignorance, haven’t written any code since Excell Macros in the early 1990’s and programming (Basic, Dos/Batch files and Autocad Lisp) since the late 1980’s. A bit slow now. Just started with HA, mid December-18. If I understand the post, I could use something like…icon: /local//.png ?? Or could I direct it to my Floorplan directory where I store my Floorplan icons?
Thanks again

First, they aren’t icons. They are images. The icon option in home assistant is always referring to the mdi icons which you are trying to avoid. You are trying to use an image file as an icon. So, from here on out you should be looking to replace the picture or image.

Second, are you using floorplan? If so, are you using CustomUI along with it?

My HA crashed yesterday so I took the opportunity for a clean HA, ESPHome/Lovelace install…NO MQTT. I did not reload my Floorplan however; I am recreating it using the ‘Picture Elements’ card in Lovelace.

Ok, so you aren’t using custom UI. So this ability may or may not exist in the current structure. It depends on what card you are using and what entity you are trying to display. Pretty much everything in this thread is for the old ui aside from the picture_entity_templates on template sensors.