Trying to change icon depending on (cover) position - but seems to stumble on unknowns...?

So I posted a request for change, and was referred over here to get some help :slight_smile:
https://community.home-assistant.io/t/add-icon-template-for-cover-when-using-command-line/711634

So in short, I have 8 curtains (cover’s) that I can control over curl http://xxx.xxx.xxx.‘{{gardin}}’/open or /close or /myposition or to get /status. Over REST everything just works, and I can get the icon I like thru a “sensor” like this:

  - platform: rest
    resource: http://xxx/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 -%}

It just does not support the control buttons for my curtains (cover).

So I added command_line cover version of the same and now I can control the curtains with the buttons for open/stop/close which comes with that definition:

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

But now I lost the control of the icon. Now these are Somfy Glydea motors, and they have a built in “store favorite position” called “My Position”. Now the command to get to that “My Position” is the same as sending “stop” to the device. So if the curtain is moving, it will stop, and if it is in a stopped position (anyware on the track so to speak) it will go to “My Position”. Very hand, for me and my wife anyway.

However, it would be awfully nice to get the ocon to “mdi:star” when the curtain is in “My Position”. Currently I can only get it to be either “open” or “closed”, and “open” works for anything that is NOT closed, so 1% to 100% = open. Nice :slight_smile:

ehh… yea about that now: “My Position” is 80% for some of the curtains, or 20% for others - open, well yes partly open. However if I could get this to be an “mdi:star” icon, it would be much nicer - you know: happy wife - happy life :sunglasses:

So I tried, as a newbie I might have to add, to add in configuration.yaml a few cuztom lines to figure that icon changing thingi out - so far I have no idea what I am doing I guess:

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
      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_sovrum_ytter:
      device_class: curtain
      templates:
        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 %}

All 4 curtains above are working in the same way. And just to give an idea what I have to “play” with, this is what I get under development tools for that command_line cover definition above:

current_position: 50
device_class: curtain
friendly_name: Gardin_Sovrum_Ytter
supported_features: 15
templates:
  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 %}

So any reference to “value” in my code above, that is the value returned from the /status command. Any reference to “current_position” is to try to reference that attribute on the device (entiti?).

So the reason I added that request was that command_line has that support for other types except cover… Switch and Sensor got support for setting icon depending on “something”. It would be nice if that is implemented some day, it will for sure help me or any other newbie.

But until then I am trying to figure this Cuztom thingi out, and see if I can get it to work.

And I just can not get this to work. Either I get NO icon, or I get the Original icon from the command_line definition (which I have no control over).

Any ideas are welcome :slight_smile:

I have come to one conclusion: The below code works perfectly under Development tools where one can test code…

        {% 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 %}

But it does NOT work under customize, and templates or not does not seem to matter:

homeassistant:
  customize:

    cover.gardin_sovrum_ytter:
      device_class: curtain
      templates:
        icon: >
          {% 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 %}

I just got to be missing something obviously?

I recall trying this out under customize several years ago and couldn’t get it to work either.

If all else fails, here is a Community Forum thread

that uses a python script called set_state.py. There are actually several variants of this script in that thread. Most people use the script to change the state of an entity, but the one I specifically referenced allows you to change the icon.
So you would use an automation that depended on the level of the cover and would call the script to change the icon.

There seems to be a few challenges with cuztom setup yes. I have tried a lot of combos and well it will not even detect misspelled code - so no cuztom is not up for this yet.

I did a request that seems to been approved and about to be released:

When that get’s out I will implement that - I can easily wait a few weeks…

None of the customize variable options accept templates. See Customizing entities - Home Assistant

Yes I know what it says - and as I wrote: Does not matter. I tried templates just to see if I could get any kind of reaction - there are no reaction. Only ICON without template just leaves the ICON empty. At all times. There is no way to control which icon to present. And as I also wrote somewhere, there is no syntax checking either.