Add icon_template for cover when using command_line

So it would be very nice if the “cover” option in command_line, like the other versions like sensor and switch, get the ability to set the icon for the cover (and also, when I am at it, device_class would be nice addition). Since icon seems to be available for switch and sensor, I hope it is a rather easy implementation? And in the end I could add (icon or icon_template maybe?):

    icon: >
        {%- if value[21:25] == 'Open' -%} mdi:curtains
        {%- elif value[21:25] == 'Clos' -%} mdi:curtains-closed
        {%- elif value[21:25] == 'MyPo' -%} mdi:star
        {%- else -%} mdi:cloud-off-outline
        {%- endif -%}

to the en of my current config in configuration.yaml:

command_line:
  - cover:
      name: Gardin_Utsikten_Inner
      unique_id: utsikten_innergardin
      command_open: "/usr/bin/curl http://10.168.10.23/open"
      command_close: "/usr/bin/curl http://10.168.10.23/close"
      command_stop: "/usr/bin/curl http://10.168.10.23/myposition"
      command_state:  "/usr/bin/curl http://10.168.10.23/status"
      value_template: >
        {%- if value[21:25] == 'Open' -%} 100
        {%- elif value[21:25] == 'Clos' -%} 0
        {%- elif value[21:25] == 'MyPo' -%} 50
        {%- else -%} Offline
        {%- endif -%}
      scan_interval: 60

This won’t change your request however you could use customize to help you do this today.
Customizing entities - Home Assistant.

I have actually tried that, and I did not get it to work. Can’t recall 100% what was the issue more than something maybe like “not supported” (which can easily be me trying to do something that simply is not supported, or more likely some sort of incorrect syntax maybe).

I am also thinkin about template cover Template cover - Home Assistant - Although I have not tried that yet.

My request is to get it more into the base layer so to speak. There is always (?) work arounds, but I would like to keep it simple?!

Manage not to trigger any errors with Customizing Entities, but I can not see any changed icon… And no visible change in device class either. So maybe I do something wrong anyway?

homeassistant:
  customize:
    # Add an entry for each entity that you want to overwrite.
    cover.utsikten_innergardin:
      device_class: curtain
      icon: >
        {%- if value[21:25] == 'Open' -%} mdi:curtains
        {%- elif value[21:25] == 'Clos' -%} mdi:curtains-closed
        {%- elif value[21:25] == 'MyPo' -%} mdi:star
        {%- else -%} mdi:cloud-off-outline
        {%- endif -%}

Is that a valid variable? Doesn’t look right.

Use the Link to Developer tools: templates – My Home Assistant to test things and see if it’s valid.

No it is not - it is “copy & pasted” from an my REST definition of the same:

  - platform: rest
    resource: http://10.168.10.23/status
    name: Gardin_Utsikt_Inner
    value_template: >
        {%- if value[21:25] == 'Open' -%} Open
        {%- elif value[21:25] == 'Clos' -%} Closed
        {%- elif value[21:25] == 'MyPo' -%} My Position
        {%- else -%} Offline
        {%- endif -%}
    icon: >
        {%- if value[21:25] == 'Open' -%} mdi:curtains
        {%- elif value[21:25] == 'Clos' -%} mdi:curtains-closed
        {%- elif value[21:25] == 'MyPo' -%} mdi:star
        {%- else -%} mdi:cloud-off-outline
        {%- endif -%}

That works on the REST - however, that version of control of my Glydea-ESp32C6 implementation does not show the cover controls - it just shows the status…

So far I have tried a few different variants of this thru customize settings in configuration.yaml - none seems to do what I would like, nor do they present any error. To cut it short, here are the four versions I tested:

homeassistant:
  customize:

    cover.gardin_utsikten_inner:
      device_class: curtain
      icon: >
        {%- if value[21:25] == 'Open' -%} mdi:curtains
        {%- elif value[21:25] == 'Clos' -%} mdi:curtains-closed
        {%- elif value[21:25] == 'MyPo' -%} mdi:star
        {%- else -%} mdi:cloud-off-outline
        {%- endif -%}

    cover.gardin_matplats_inner:
      device_class: curtain
      templates:
        icon: >
          {%- if value[21:25] == 'Open' -%} mdi:curtains
          {%- elif value[21:25] == 'Clos' -%} mdi:curtains-closed
          {%- elif value[21:25] == 'MyPo' -%} mdi:star
          {%- else -%} mdi:cloud-off-outline
          {%- endif -%}

    cover.gardin_sovrum_inner:
      device_class: curtain
      templates:
        icon: >
          {% if cover.gardin_sovrum_inner.attributes.current_position == 100 %} mdi:curtains
          {% elif cover.gardin_sovrum_inner.attributes.current_position == 0 %} mdi:curtains-closed
          {% elif cover.gardin_sovrum_inner.attributes.current_position == 50 %} mdi:star
          {% else %} mdi:cloud-off-outline
          {% endif %}
          
    cover.gardin_matplats_ytter:
      device_class: curtain
      templates:
        icon: >
          {%- if value[21:25] == 'Open' -%} mdi:curtains
          {%- elif value[21:25] == 'Clos' -%} mdi:curtains-closed
          {%- elif value[21:25] == 'MyPo' -%} mdi:star
          {%- else -%} mdi:cloud-off-outline
          {%- endif -%}

Result of the above:
1st - No icon what so ever
2nd, 3rd and 4th - original icon, nothing changed

I have tested some variants of this, and fibbled back and forward with syntax. Actually no syntax error so far, but no luck with controlling icon either…

Just to show what I can see in development tools:

device_class: curtain
friendly_name: Gardin_Sovrum_Inner
supported_features: 15
icon: >
  {% if current_position == 100 %} mdi:curtains {% elif current_position == 0 %}
  mdi:curtains-closed {% elif current_position == 50 %} mdi:star {% else %}
  mdi:cloud-off-outline {% endif %}
current_position: 50

I am trying to find a way here - but I know I am mixing and randomly trying to fix what might be clear for some, but not for me. Just a thing like “{%” vs {%-" - what with that minus sign? When and why?

And to answer your question, “value[21:25]” refers to the value returned from the ESP32C6 “status” question which will return one of the following text messages:

  1. Open
  2. Closed
  3. MyPosition

The 21:25 gives the 4 first characters of status string that is returned when calling the “status” function on the web server running on the ESP32C6.

You have to define that. Currently it is likely equal to null or something, not giving errors, but not changing either.

The minus sign removes spaces from the template, not going to change what you have going on.

You have to get the value from the cover into your template in the customize statement.

This is your current problem.
I would put a topic in Configuration - Home Assistant Community and add a ‘template’ tag asking the question why won’t my icon change and see if one of the smarter people can help you get it going the hard way.

I created a PR for this that was accepted and merged. I wouldn’t expect to see it in tomorrow’s release, but it should show up before too long (no later than 2024.5, I would guess).

To get your example to work, I think the icon yaml will look like:

    icon: >
        {%- if this.attributes.current_position == 100 -%} mdi:curtains
        {%- elif this.attributes.current_position == 0 -%} mdi:curtains-closed
        {%- elif this.attributes.current_position == 50 -%} mdi:star
        {%- else -%} mdi:cloud-off-outline
        {%- endif -%}

Thanks for the help with the PR!! I’ll happily wait for it :slight_smile:

Regarding the yaml code: Well it does not produce an error or anything, but it does not work. I have tested with and without template code word - the only difference is that if I do not use “templates:” I get no icon at all - with “templates:” I get original icons and not the the one in the code.

I have also come to the conclusion that any errors in syntax in the Cuztom section will never generate any error log. It just suppresses it. For example, based on the code you suggested, I changed to an x in current - no error generated at all:

    cover.gardin_sovrum_inner:
      device_class: curtain
      templates:
        icon: >
          {%- if this.attributes.curxent_position == 100 -%} mdi:curtains
          {%- elif this.attributes.current_position == 0 -%} mdi:curtains-closed
          {%- elif this.attributes.current_position == 50 -%} mdi:star
          {%- else -%} mdi:cloud-off-outline
          {%- endif -%}

this.attributes.curxent_position is obviously misspelled - no error generated or anything - it just swallows it… Strange.

For reference (if someone else encounters anything like this) I have also tested cover template, this seems to work (although building a cover for a cover to get it to work just for icon seems a bit over the top for me - but it does work):

cover:
  - platform: template
    covers:
      gardin_sovrum_ytter:
        device_class: curtain
        unique_id: sovrum_yttergardin_new
        friendly_name: "Testa Gardin igen"
        position_template: "{{ state_attr('cover.gardin_sovrum_ytter', 'current_position') }}"
        open_cover:
          service: cover.open_cover
          target:
            entity_id: cover.gardin_utsikten_inner
        close_cover:
          service: cover.close_cover
          target:
            entity_id: cover.gardin_utsikten_inner
        stop_cover:
          service: cover.stop_cover
          target:
            entity_id: cover.gardin_utsikten_inner
        icon_template: >-
          {% set xyz = state_attr('cover.gardin_sovrum_ytter', 'current_position') %}
          {% if xyz == 100 %} mdi:curtains
          {% elif xyz == 0 %} mdi:curtains-closed
          {% elif xyz == 50 %} mdi:star
          {% else %} mdi:cloud-off-outline
          {% endif %}

Sorry I wasn’t clearer. I meant your original example of icon being a config parameter for the command_line cover. Your initial example used value just like in the value_template, but that won’t be available when the icon template is being parsed, so in my example I just referenced the computed current_position attribute which would be available.

I did an error report on cuztom, and the end result is: It is not supported. Icon is supported but not dynamic (jinja or template - or is jinja and template the same thing with different names?).

Jinja templates are not supported in “customize” / “customize_glob”.
You may try using Custom UI on your own risk since it has it’s issues & questionable maintenance.

I just updated to 2024.05 and YES it works!

Very large Thanks!

1 Like