Dagwood
November 17, 2023, 4:13pm
1
I’m new to HA and I’m trying to create a dashboard to show my devices with batteries and the level of charge. I’ve come up with the code listed below, but the only thing that’s not working is the color based on percentage remaining. Everything shows in blue. Any suggestions for what I need to change?
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
Mats789
November 17, 2023, 4:32pm
2
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:
Before we begin…
This forum is not a helpdesk
The people here don’t work for Home Assistant, that’s an open source project. We are volunteering our free time to help others. Not all topics may get an answer, never mind one that helps you solve your problem.
[image]
This also isn’t a general home automation forum, this is a forum for Home Assistant and things related to it. Any question about Home Assistant, and about using things with Home Assistant, is welcome here. We can’t help you with e…
1 Like
NathanCu
(Nathan Curtis)
November 17, 2023, 4:33pm
3
…and mats beat me to it.
Yeah Dag, we cant read that on our end (especially on a phone) if its not formatted right.
Dagwood
November 17, 2023, 5:03pm
4
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
NathanCu
(Nathan Curtis)
November 17, 2023, 5:05pm
5
Wooboy - you got a BAD indentation problem.
Indentations are critical - they matter in YAML.
Mats789
November 17, 2023, 5:10pm
6
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
Dagwood
November 17, 2023, 5:29pm
7
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
NathanCu
(Nathan Curtis)
November 17, 2023, 5:39pm
8
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?)
NathanCu
(Nathan Curtis)
November 17, 2023, 5:47pm
9
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
Dagwood
November 17, 2023, 6:11pm
10
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
NathanCu
(Nathan Curtis)
November 17, 2023, 6:26pm
11
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.
Dagwood
November 17, 2023, 7:07pm
12
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
NathanCu
(Nathan Curtis)
November 17, 2023, 7:08pm
13
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
ValMarDav:
sort:
exclude: []
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.
ValMarDav:
card_mod: null
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
Dagwood
November 17, 2023, 10:25pm
18
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
Dagwood
November 17, 2023, 10:45pm
20
Thanks for your suggestion but that’s over my head.