Battery Entities On Dashboard

To start with you need to format your code correctly so that it is readable with all spaces otherwise it is very difficult to see what, if anything, is wrong. Use </> option to include your code. Read no. 11 here:

1 Like

…and mats beat me to it.

Yeah Dag, we cant read that on our end (especially on a phone) if its not formatted right.

Sorry for the newbie mistake, I was wondering how to format the code. Very easy now that it was pointed out.

type: custom:auto-entities
card:
type: grid
title: Battery Status
columns: 4
square: false
card_param: cards
filter:
template: |-
{% for state in states.sensor -%}
{%- if (state.entity_id | regex_match(“sensor.battery”, ignorecase=True)) -%}
{{
{
‘type’ : “gauge”,
‘entity’ : state.entity_id,
‘name’: state.attributes.friendly_name|regex_replace(find=‘(:|)(Batterie|)(Battery|)(level|numeric|)’, replace=‘’, ignorecase=True),
‘card_mod’ : {
‘style’: “ha-card > div {\n font-size: 10px !important;\n}”
},
‘options’ : {
‘type: gauge’
‘min: 0’
‘max: 100’
‘severity’: [
‘green: 50’
‘yellow: 30’
‘red: 15’
],
‘entities’: [
state.entity_id,
],
}
}
}},
{%- endif -%}
{%- endfor %}
exclude:
- entity_id: sensor.total_battery_energy_charged
- entity_id: sensor.total_battery_energy_discharged
- entity_id: sensor.battery_power_charging
- entity_id: sensor.battery_power_discharging
- device_manufacturer: BYD
sort:
method: state
numeric: true

1 Like

Wooboy - you got a BAD indentation problem.

Indentations are critical - they matter in YAML.

:slight_smile: Good start but copy and paste the code you have in your yaml not from your post above then it is easier to see if anything is wrong.

1 Like

Let’s try this again.

type: custom:auto-entities
card:
  type: grid
  title: Battery Status
  columns: 4
  square: false
card_param: cards
filter:
  template: |-
    {% for state in states.sensor -%}
      {%- if (state.entity_id | regex_match("sensor.*battery*", ignorecase=True)) -%}
        {{
          {
            'type' : "gauge",
            'entity' : state.entity_id,
            'name': state.attributes.friendly_name|regex_replace(find='(:|)(Batterie|)(Battery|)(level|numeric|)', replace='', ignorecase=True),
            'card_mod' : {
              'style': "ha-card > div {\n   font-size: 10px !important;\n}"
            },
            'options' : {
              'type: gauge'
              'min: 0'
              'max: 100'
              'severity': [  
                'green: 50'
                'yellow: 30'
                'red: 15'
              ],  
              'entities': [
                state.entity_id,
              ], 
            }
          }
        }},
      {%- endif -%}
    {%- endfor %}
  exclude:
    - entity_id: sensor.total_battery_energy_charged
    - entity_id: sensor.total_battery_energy_discharged
    - entity_id: sensor.battery_power_charging
    - entity_id: sensor.battery_power_discharging
    - device_manufacturer: BYD
sort:
  method: state
  numeric: true
3 Likes

K so we’re talking about a standard gauge card wrapped in an auto entity. pulling all your batteries.

Do you have the sample gauge without the wrapping? (Basically, did you prototype this before you stuffed it into the auto entitles?) at first glance your severity clause LOOKS OK (similar to my dryer - the only thing is the values are reversed. you’re going red on low.) but…

Just out of curiosity - what happens if you revers your numbers? (Yes I know it’s not right, but I think you get where I’m going?)

OK -

When you expand your template in the template editor it looks like this:

  {
    "type": "gauge",
    "entity": "sensor.thing",
    "name": " Thing",
    "card_mod": {
      "style": "ha-card > div {\n   font-size: 10px !important;\n}"
    },
    "options": {
      "type: gaugemin: 0max: 100severity": [
        "green: 15yellow: 30red: 50"
      ],
      "entities": [
        "sensor.thing"
      ]
    }
  },

Do you see it? (hint - look how lines 9/10 are rendering)

I think you got lucky on blue, 0-100 because those are defaults.

1 Like

I can see where things don’t render properly but I’m not sure how to correct it. I don’t understand where braces and square brackets become necessary. Most of the code I just copied and tried to add tidbits here and there.

My latest changes don’t produce anything at all, not sure how close I am:

type: custom:auto-entities
card:
  type: grid
  title: Battery Status
  columns: 4
  square: false
card_param: cards
filter:
  template: |-
    {% for state in states.sensor -%}
      {%- if (state.entity_id | regex_match("sensor.*battery*", ignorecase=True)) -%}
        {{
          {
            'type' : "gauge",
            'entity' : state.entity_id,
            'name': state.attributes.friendly_name|regex_replace(find='(:|)(Batterie|)(Battery|)(level|numeric|)', replace='', ignorecase=True),
            'card_mod' : {
              'style': "ha-card > div {\n   font-size: 10px !important;\n}"
            },
            'options' : {
              'type': "gauge"
              'min': "0"
              'max': "100"
            'severity':   
              'green' : "15"
              'yellow' : "30"
              'red' : "50"
              ,  
              'entities': [
                state.entity_id,
              ], 
            }
          }},
      {%- endif -%}
    {%- endfor %}
  exclude:
    - entity_id: sensor.total_battery_energy_charged
    - entity_id: sensor.total_battery_energy_discharged
    - entity_id: sensor.battery_power_charging
    - entity_id: sensor.battery_power_discharging
    - device_manufacturer: BYD
sort:
  method: state
  numeric: true
1 Like

No worries. Ok how to help yourself time:
first in your install go to: developer-tools/template - template editor if you’ve never seen it.

What Im doing is copying EVERYTHING in your template:

    {% for state in states.sensor -%}
      {%- if (state.entity_id | regex_match("sensor.*battery*", ignorecase=True)) -%}
        {{
          {
            'type' : "gauge",
            'entity' : state.entity_id,
            'name': state.attributes.friendly_name|regex_replace(find='(:|)(Batterie|)(Battery|)(level|numeric|)', replace='', ignorecase=True),
            'card_mod' : {
              'style': "ha-card > div {\n   font-size: 10px !important;\n}"
            },
            'options' : {
              'type': "gauge"
              'min': "0"
              'max': "100"
            'severity':   
              'green' : "15"
              'yellow' : "30"
              'red' : "50"
              ,  
              'entities': [
                state.entity_id,
              ], 
            }
          }},
      {%- endif -%}
    {%- endfor %}

And dropping it in the editor The current iteration renders as:

TemplateSyntaxError: expected token ',', got ':'

Yes this is all about syntax, comma, how you’re escaping (this quote v that quote)

I havent found it yet but admittedly I’m doing 4 things at once. Where’s Marius when you need him,

Also don’t get discouraged - templating inside auto entities is one of the hardest things to do… You’re close.

I will give that a try. I didn’t know it is available. It’s always nice when you have an editor that points out your errors.

Thanks for your help.

1 Like

No worries that’s how we help each other. I’ll keep looking but I have to run to an appointment now that you know what you’re looking for and how to read it you will get there.

Ill check in when I get back.

Here is the page I use.

      - type: custom:auto-entities
        card:
          type: custom:bar-card
          title_position: inside
          height: 38
          card_mod: null
          positions:
            icon: inside
            indicator: inside
            name: inside
            value: inside
          show_icon: true
          align: split
          columns: "2"
          max: 100
          unit_of_measurement: "%"
          severity:
            - color: "#d11e1e"
              from: 0
              to: 5
              icon: mdi:battery-outline
            - color: "#cf2d11"
              from: 6
              to: 10
              icon: mdi:battery-10
            - color: "#cc3900"
              from: 11
              to: 15
              icon: mdi:battery-10
            - color: "#c84400"
              from: 16
              to: 20
              icon: mdi:battery-20
            - color: "#c44d00"
              from: 21
              to: 25
              icon: mdi:battery-20
            - color: "#bf5600"
              from: 26
              to: 30
              icon: mdi:battery-30
            - color: "#b95f00"
              from: 31
              to: 35
              icon: mdi:battery-30
            - color: "#b36600"
              from: 36
              to: 40
              icon: mdi:battery-40
            - color: "#ac6e00"
              from: 41
              to: 45
              icon: mdi:battery-40
            - color: "#a57500"
              from: 46
              to: 50
              icon: mdi:battery-50
            - color: "#9d7b00"
              from: 51
              to: 55
              icon: mdi:battery-50
            - color: "#948100"
              from: 56
              to: 60
              icon: mdi:battery-60
            - color: "#8b8700"
              from: 61
              to: 65
              icon: mdi:battery-60
            - color: "#818d00"
              from: 66
              to: 70
              icon: mdi:battery-70
            - color: "#769200"
              from: 71
              to: 75
              icon: mdi:battery-70
            - color: "#6a9700"
              from: 76
              to: 80
              icon: mdi:battery-80
            - color: "#5d9c00"
              from: 81
              to: 85
              icon: mdi:battery-80
            - color: "#4da100"
              from: 86
              to: 90
              icon: mdi:battery-90
            - color: "#39a500"
              from: 91
              to: 95
              icon: mdi:battery-90
            - color: "#15a911"
              from: 96
              to: 100
              icon: mdi:battery
          style: |
            bar-card-currentbar, bar-card-current, bar-card-backgroundbar {

                height: 5px !important;
                margin-top: 30px;
            }
            ha-card {
                background: none;
                box-shadow: none;
            }
            ha-icon{
                color: white !important;
            }
            bar-card-iconbar {
                margin-bottom: 10px;
            }
          animation:
            state: "on"
            speed: "2"
        filter:
          include:
            - attributes:
                device_class: battery
              domain: sensor
        sort:
          exclude: []
          method: state
          numeric: true
        show_empty: false
1 Like

The “exclude” does not belong to the “sort” option.

Too cumbersome, many entries.
Since you use auto-entities - consider replace “include” with “template” and define these “severities” in a loop with a gradient color.

Probably was on the same indentation level as “style” and then later you added some lines between them.

This is definitely wrong. Severity is a dictionary.
Also, values in dictionary should be separated by comma. Same about “option”.

1 Like

I can get this code to pass the syntax check, I just need to figure out how to add severity to get different colours.

type: custom:auto-entities
card:
  type: grid
  title: Battery Status
  columns: 4
  square: false
card_param: cards
filter:
  template: |-
    {% for state in states.sensor -%}
      {%- if (state.entity_id | regex_match("sensor.*battery*", ignorecase=True)) -%}
        {{
          {
            'type' : "gauge",
            'entity' : state.entity_id,
            'name': state.attributes.friendly_name|regex_replace(find='(:|)(Batterie|)(Battery|)(level|numeric|)', replace='', ignorecase=True),
            'card_mod' : {
              'style': "ha-card > div {\n   font-size: 10px !important;\n}"
            },
            'options' : {
              'type' : "gauge",
              'min' : "0",
              'max' : "100",
              

              
              'entities': [
                state.entity_id,
              ], 
            }
          }
        }},
      {%- endif -%}
    {%- endfor %}
  exclude:
    - entity_id: sensor.total_battery_energy_charged
    - entity_id: sensor.total_battery_energy_discharged
    - entity_id: sensor.battery_power_charging
    - entity_id: sensor.battery_power_discharging
    - device_manufacturer: BYD
sort:
  method: state
  numeric: true

'options' : {
              'prop1' : ‘value1’,
              ‘some_dict’: {
                ‘prop1’: ‘value1’,
                ‘prop2’: ‘value2’
              }
1 Like

Thanks for your suggestion but that’s over my head.

A simplified way to define a template for auto-entities.

1 Like