Head scratcher

cant figure figure this one out. I have a template to show which bin collection it is. it work and changes from black to blue bin. I use this yaml code - i wanted it to actually have the icon and letters black and blue.

# Template sensor for bin collection schedule
- platform: template
  sensors:
    bin_collection:
      friendly_name: "Bin Collection"
      value_template: >
        {% set today = now().date() %}
        {% set black_bin_week = as_timestamp('2024-03-08') %}
        {% set days_since_black = (as_timestamp(today) - black_bin_week) / (60*60*24) %}
        {% set weeks_since_black = (days_since_black / 7) | round(0, 'floor') %}
        {% set day_after_collection = (days_since_black % 7) == 1 %}
        {% if day_after_collection %}
          {% if weeks_since_black % 2 == 0 %}
            Blue Bin
          {% else %}
            Black Bin
          {% endif %}
        {% elif weeks_since_black % 2 == 0 %}
          Blue Bin
        {% else %}
          Black Bin
        {% endif %}
      attribute_templates:
        icon_color: >
          {% if states('sensor.bin_collection') == 'Black Bin' %}
            "#000000"  # Hex color code for black
          {% elif states('sensor.bin_collection') == 'Blue Bin' %}
            "#0000FF"  # Hex color code for blue
          {% else %}
            "#FFFFFF"  # Default color (white) if no bin is scheduled
          {% endif %}
# Customization for the bin collection sensor
homeassistant:
  customize:
    sensor.bin_collection:
      templates:
        icon_color: "{{ state_attr('sensor.bin_collection', 'icon_color') }}"

I also have this in my card config

                                                                                                                                                 ``` 

type: entity
entity: sensor.bin_collection
name: Next Collection
icon: >
[[[
return state_attr(‘sensor.bin_collection’, ‘icon’);
]]]
state_color: true
styles:
card:
- background-color: >
[[[
return state_attr(‘sensor.bin_collection’, ‘icon_color’);
]]]
icon:
- color: >
[[[
return state_attr(‘sensor.bin_collection’, ‘icon_color’);
]]]```

long tory short it doesnt work when i change the card from attribute to icon colour i just get #x000f#

is there something glaring obvious ive done wrong . I did try something with the HACCS custom ui int- a quick fix? might just leave it saying black bin / blue bin in black as its already cost me 2 HA reboots :slight_smile:

This has a lot of the markers of a ChatGPT special…

Issues:

  • The customize entry makes no sense. It creates a new attribute named “templates” with a sub-attribute named “icon_color” whose value is defined by a template. Customize only allows static values, template support was removed years ago due to the issues it caused.
  • Entity cards do not allow templating, and if they did it wouldn’t likely be JS-style templating. There may be custom integrations or resources that adds JS templating to cards… if that is the case you need to tell us. Some custom cards like Custom Button Card do use JS-style templating.
  • That’s not how you comment in Jinja… those “comments” will be printed as part of the returned value.

Ooofff …

There was a bit of looking online…so yeh its probably all wrong :slight_smile: might be one i just leave be haha

# Template sensor for bin collection schedule
- platform: template
  sensors:
    bin_collection:
      friendly_name: "Bin Collection"
      value_template: >
        {%- set black_bin_week = ('2024-03-08')|as_datetime|as_local %}
        {%- set days_since_black = (today_at() - black_bin_week).days %}
        {%- set weeks_since_black = (days_since_black // 7) %}
        {%- set day_after_collection = (days_since_black % 7) == 1 %}
        {%- if day_after_collection and weeks_since_black is even %}
          Blue Bin
        {%- elif day_after_collection %}
          Black Bin
        {%- elif weeks_since_black is even %}
          Blue Bin
        {%- else %}
          Black Bin
        {%- endif %}
      attribute_templates:
        icon_color: >
          {% if this.state == 'Black Bin' %}
            #000000
          {% elif this.state == 'Blue Bin' %}
            #0000FF
          {% else %}
            #FFFFFF
          {% endif %}

Then you need to find a card that accepts some from of templating. Mushroom Cards and Custom Button Card are popular options and templating can be used with Tile cards if you add the Custom Service Call Tile Feature

2 Likes

Thanks ill have a look into it…really new to HA i use co pilot to try help me with code: but clearly didnt do well. Is there anything i cam read that would help me with the card and customer service tile feature?

That’s understandable, one of the best things about HA is how flexible and customizable it is. But the price of that flexibility is that the learning curve can be steep. Unfortunately, most of the LLM-based AIs are really bad at Home Assistant and very often produce configurations with issues that are hard for new users to recognize because they seem plausible.

The main thing is to read the documents, especially with custom integrations and cards since they may be configured very differently from standard cards.

All of the ones I linked above (and many more) are available in HACS. There are also threads here on the forum for Mushroom (1, 2) and Service Call Tile Feature (1).

Then give it a try, and ask more question if you need help.
No one here wants you to fail or give up.

Cheers mate appreciate it, everyone on here as been really helpful