What is wrong with this code?

Please check my code and why the cell colour does not change when cell=min_cell or max_cell.

type: grid
        cards:
          - type: grid
            cards:
              - type: markdown
                content: |2-
                            <center><font color="#18f50c"><font size=3>01.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {% if
                            states('sensor.accu_bms0_max_cell_voltage') == '1' %}  <font
                            color="#3090C7">{{ states('sensor.accu_bms0_cell_1') }}
                            V</font> {% elif states('sensor.accu_bms0_min_cell_voltage') ==
                            '1' %}  <font color="orange">{{
                            states('sensor.accu_bms0_cell_1') }} V</font> {% else %}
                            {{ states('sensor.accu_bms0_cell_1') }} V {% endif %}    
                            <br>
                            02.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {% if
                            states('sensor..accu_bms0_max_cell_voltage') == '2' %}  <font
                            color="#3090C7">{{ states('sensor.accu_bms0_cell_2') }}
                            V</font> {% elif states('sensor.jk_bms_min_voltage_cell') ==
                            '2' %} <font color="orange">{{
                            states('sensor.accu_bms0_cell_2') }} V</font> {% else %}
                            {{ states('sensor.accu_bms0_cell_2') }} V {% endif %}<br>
                            03.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {% if
                            states('sensor..accu_bms0_max_cell_voltage') == '3' %}  <font
                            color="#3090C7">{{ states('sensor.accu_bms0_cell_3') }}
                            V</font> {% elif states('sensor..accu_bms0_min_cell_voltage') ==
                            '3' %} <font color="orange">{{
                            states('sensor.accu_bms0_cell_3') }} V</font> {% else %}
                            {{ states('sensor.accu_bms0_cell_3') }} V {% endif %}
                            <br>
                            04.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {% if
                            states('sensor..accu_bms0_max_cell_voltage') == '4' %}  <font
                            color="#3090C7">{{ states('sensor.accu_bms0_cell_4') }}
                            V</font> {% elif states('sensor..accu_bms0_min_cell_voltage') ==
                            '4' %} <font color="orange">{{
                            states('sensor.accu_bms0_cell_4') }} V</font> {% else %}
                            {{ states('sensor.accu_bms0_cell_4') }} V {% endif %}
                            <br>
                            05.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {% if
                            states('sensor..accu_bms0_max_cell_voltage') == '5' %}  <font
                            color="#3090C7">{{ states('sensor.accu_bms0_cell_5') }}
                            V</font> {% elif states('sensor..accu_bms0_min_cell_voltage') ==
                            '5' %} <font color="orange">{{
                            states('sensor.accu_bms0_cell_5') }} V</font> {% else %}
                            {{ states('sensor.accu_bms0_cell_5') }} V {% endif %}
                            <br>
                            06.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {% if
                            states('sensor..accu_bms0_max_cell_voltage') == '6' %}  <font
                            color="#3090C7">{{ states('sensor.accu_bms0_cell_6') }}
                            V</font> {% elif states('sensor..accu_bms0_min_cell_voltage') ==
                            '6' %} <font color="orange">{{
                            states('sensor.accu_bms0_cell_6') }} V</font> {% else %}
                            {{ states('sensor.accu_bms0_cell_6') }} V {% endif %}
                            <br>
                card_mod:
                  style: |
                    ha-card {
                       box-shadow: none;
                       background: rgba(0,0,0,0);
                       border: white solid 1px !important;
                    }

Typo?
BTW, this question is not related to card-mod, that why is was not supposed to be asked in card-mod repo.

Also, I suggest to convert to float for comparing.
Like “states(‘sensor.xxx’)|float(default=0) == 6”

Also, using “color” property in Markdown not the best way… it may not work.
“Color” property is deprecated according to internet.

I think you’re mistaking the 16 color words being deprecated, not the prop itself.

Could you please try this on your configuration?

I will retest it again in a week. Some time ago tried it and failed.

I cannot. Away from PC.

Could you try my code?

Why don’t you test with simplified version? Leave only a small meaningful amount and test. If your were able to write a large amount of code - you shoud be able to test it as well. Posting a large sophisticated code and asking “what is wrong here? Could you test?” is not a good way. If you are asking for a help - please simplify a work for people, simplify the code. Also, there is a big probability that you will sort out things by yourself while simplifying.

1 Like

This code is checking the state 16% which should be 3.330.
Where does 16% stand for?

type: grid
        cards:
          - type: markdown
            content: >- 
                            <center><font color="yeloow"><font size=3>01.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {% if
                            states('sensor.accu_bms0_max_cell_voltage') == '16' %}  <font
                            color= "green">{{ states('sensor.accu_bms0_cell_16') }}
                            V</font> {% elif states('sensor.accu_bms0_min_cell_voltage') ==
                            '16' %}  <font color="yellow">{{
                            states('sensor.accu_bms0_cell_16') }} V</font> {% else %}
                            {{ states('sensor.accu_bms0_cell_16') }} V {% endif %}    

Sorry, unclear.
If you suspect that something is wrong with “if else” code - try testing this simplified:


type: grid
        cards:
          - type: markdown
            content: >- 
              {% if states('sensor.accu_bms0_max_cell_voltage') == '16' -%}
              xx
              {%- elif states('sensor.accu_bms0_min_cell_voltage') == '16' -%}
              yyy
              {%- else -%}
              zzz
              {%- endif %}

And check ones again my suggestion about converting to float (or int if more appropriate for “==“ check) which you missed.

It gives zz

I don’t want value of min/max but one of the 16 cells having the same value as min/max

Will try your suggestion.

Is this code to display the JK-BMS (I guess) values somewhere stored on Github or so? I really like this way of presenting the values. It would be great of I use this code as well in my HA setup. Therefore I would like to download the latest/working version? Thanks.

Another question for @broekieman How do you get the JK-BMS data into Home Assistant? Is this through the CAN to Cerbo cable connection? Or are you using a different mechanism?

I use an esphome wifi board connected to the rs485 port.

1 Like

I am still trying to get the min/max cells in colour. Will share it when it works.

1 Like

Not that simple…

type: vertical-stack
cards:
  - type: markdown
    content: <font color=red>xxxx</font>
  - type: markdown
    content: <font color=var(--red-color)>xxxx</font>
  - type: markdown
    content: <font color=#ff0000>xxxx</font>
  - type: markdown
    content: <font color=rgb(255,0,0)>xxxx</font>

So, the property “color” for a “font” has issues.

As for defining a “color” inside a “style”:

How to get my color in state?

{# Retrieve minimum and maximum values from sensors #}
{% set bms0_max = states('sensor.bms0_max_cell_voltage') | float | round(2) %}
{% set bms0_min = states('sensor.bms0_min_cell_voltage') | float | round(2) %}

{# Make a list of all cell sensor entities #}
{% set bms0_cells = states.sensor
    | selectattr('entity_id', 'match', 'bms0_cell_')
    | map(attribute='entity_id')
    | list
%}

{# Open paragraph and center the text #}
<p style="text-align: center">
 {# Loop through the list we made above #}
    {% for cell in bms0_cells %}
 {# Store sensor's state (voltage) in variable #}
        {% set voltage = states(cell) | float | round(2) %}
 
 {# Set colour based on min/max/default #}
        {% if voltage >= bms0_max %}
            {% set colour = "#3090C7" %}
        {% elif voltage <= bms0_min %}
            {% set colour = "orange" %}
        {% else %}
            {% set colour = "var(--primary-text-color)" %}
        {% endif %}
 
 {# Output #}
        <span style="color: {{ colour }}">
            {{ voltage }} V
        </span><br />
    {% endfor %}
</p>