#My ARGB → color-name Jinja got out of hand. What's the proper way of doing this?

1 messages · Page 1 of 1 (latest)

chrome zealot
#

I’m trying to clean up some janky color detection logic for my Bambu AMS tray sensors in Home Assistant. Each tray exposes a color attribute that looks like an 8-digit hex value. I want to automatically convert that into a readable filament color name (Red, Blue, White, etc.)

Right now, I’m using a Template Entity Row in Lovelace to translate that hex into names using a ton of if/else checks. It does work… but it feels super brute-force, slow, and hard to maintain. Here’s the code for one tray if anyone wants to take a peek:

Is there a smarter way to do this? Maybe some sort of HACS integration or tool I haven't found out about yet?

#
type: custom:template-entity-row
entity: sensor.ams_tray_1
name: Spool 1
icon: >-
  {% set hex = state_attr('sensor.ams_tray_1', 'color')|string|upper|replace('#','') %}
  {% if hex == '00000000' %}
    mdi:cancel
  {% else %}
    mdi:album
  {% endif %}
state: >-
  {% set hex = state_attr('sensor.ams_tray_1', 'color')|string|upper|replace('#','') %}
  {% if hex == '00000000' %}
    Empty
  {% elif hex|length == 8 %}
    {% set r = hex[2:4]|int(0, 16) %}
    {% set g = hex[4:6]|int(0, 16) %}
    {% set b = hex[6:8]|int(0, 16) %}
    {% set max_val = [r,g,b]|max %}
    {% set min_val = [r,g,b]|min %}
    {% if max_val < 30 %}Black
    {% elif min_val > 225 %}White
    {% elif max_val < 100 %}Dark Gray
    {% elif min_val > 180 and max_val < 220 %}Light Gray
    {% elif r > g + 50 and r > b + 50 %}
      {% if r > 200 and g < 100 %}Red
      {% elif g > 100 %}Orange
      {% else %}Dark Red{% endif %}
    {% elif g > r + 30 and g > b + 30 %}
      {% if g > 200 %}Lime Green
      {% elif b > 100 %}Teal
      {% else %}Green{% endif %}
    {% elif b > r + 30 and b > g + 30 %}
      {% if b > 200 %}Blue
      {% else %}Dark Blue{% endif %}
    {% elif r > 180 and g > 180 and b < 100 %}Yellow
    {% elif r > 150 and b > 150 and g < 100 %}Purple
    {% elif r > 150 and g < 100 and b > 100 %}Magenta
    {% elif g > 150 and b > 150 and r < 100 %}Cyan
    {% elif r > 100 and g > 80 and b < 80 %}Brown
    {% elif r > 150 and g > 100 and b > 100 %}Pink
    {% else %}Mixed{% endif %}
    - {{ states('sensor.ams_tray_1') }}
  {% else %}
    {{ states('sensor.ams_tray_1') }}
  {% endif %}
#

Found this post on the forum, but unsure of how to set this up exactly. I don't see the folder/file referenced by the poster, do I need to create them myself? Any help is much appreciated!

olive relic
#

I read over your post and recognized the sensor entity name being a BambuLab AMS. I have two P1S printers and 4 AMS (1 original, 3 pros) and I'm running the BambuLab integration. I figured I'd try my luck to try to solve this. I came across the post that you mentioned above. LINK

#

First things first: let's get the custom_template added. I have the Samba Share add-on installed. With that, I used my Windows PC and navigated to my HA's IP address then the config folder. If you don't see the custom_templates folder, create it. (I can't remember if it is there by default.)

#

In the custom_templates folder, I created a new text document and gave it an appropriate name and ensured the extension was jinja. I opened the file with Notepad, pasted the code from the link above. Save & close. (I called mine hex2name.jinja.)

#

Next, go to Developer Tools > Actions. Find homeassistant.reload_custom_templates and click Perform Action.

#

In Developer Tools > Template, I used this for testing: yaml {% from 'hex2name.jinja' import hex2name %} {{ hex2name(state_attr('sensor.ams_bravo_1_tray_4','color')[:7]) }} (The [:7] strips the ending FF from the attribute because the template did not like the FF.) This returned: Black.