h1ghrise
(Ben)
December 5, 2021, 9:27am
1
Hey there,
I’m using the auto-entity
card to display my battery powered entities.
Code:
card:
show_header_toggle: false
title: Battery levels
type: custom:bar-card
severity:
- color: Red
from: 0
to: 10
icon: mdi-battery-alert-variant-outline
- color: Red
from: 11
to: 15
icon: mdi-battery-low
- color: Orange
from: 26
to: 50
icon: mdi-battery-medium
- color: Green
icon: mdi:battery-high
from: 51
to: 100
filter:
include:
- entity_id: '*battery'
type: custom:auto-entities
show_empty: true
sort:
method: state
numeric: true
reverse: true
The friendly name for the battery sensor is always entity name + battery
.
As i’m only showing battery entities here, the amended battery
is redundant, therefore i’d like to remove it.
Is there any way to strip the battery
from the friendly name?
Currently it looks like this:
thanks in advance.
try this (untested):
filter:
template: >-
{% for state in states.sensor -%}
{%- if state.entity_id | regex_match("sensor.battery_", ignorecase=False) -%}
{{
{
'entity': state.entity_id,
'name': state.attributes.friendly_name.split(' battery')[0]
}
}},
{%- endif -%}
{%- endfor %}
6 Likes
h1ghrise
(Ben)
December 8, 2021, 3:19pm
3
yes! Exactly I was looking for. Thanks
klogg
(Klogg)
December 24, 2024, 3:53pm
4
I know this is 3 years old so much might have changed but should it still work?
I want ot do exactly the same thing with my sensors all beginning with “tfl_”.
show_empty: false
sort:
method: name
card:
type: entities
show_header_toggle: false
filter:
template: >
{% for state in states.sensor | selectattr('entity_id', 'search', 'tfl_')
| rejectattr('state', 'eq', 'Good Service') %}
{{
{
'entity': state.entity_id,
'name': state.attributes.friendly_name.split('TFL ')[1]
}
}}
{% endfor %}
In the template tool it returns:
{'entity': 'sensor.tfl_victoria_line', 'name': 'Victoria Line'}
{'entity': 'sensor.tfl_piccadilly_line', 'name': 'Piccadilly Line'}
{'entity': 'sensor.tfl_dlr_line', 'name': 'DLR Line'}
But the card shows as empty.
Thanks.
See that you missed a COMMA between dictionaries
klogg
(Klogg)
December 24, 2024, 5:53pm
6
Thanks, yes I did.
(That was a typo when I reverted back to your original method after trying various alternatives).
But it still gives a blank card even with the comma.
The correct template shows this:
{'entity': 'sensor.tfl_victoria_line', 'name': 'Victoria Line'},
{'entity': 'sensor.tfl_piccadilly_line', 'name': 'Piccadilly Line'},
{'entity': 'sensor.tfl_bakerloo_line', 'name': 'Bakerloo Line'},
{'entity': 'sensor.tfl_lioness_line', 'name': 'Lioness Line'},
{'entity': 'sensor.tfl_dlr_line', 'name': 'DLR Line'},
klogg:
%}
Also, these “{%” and “%}” must be with “-”.
1 Like
klogg
(Klogg)
December 24, 2024, 11:26pm
8
That did it. Thanks again.
klogg
(Klogg)
December 24, 2024, 11:37pm
9
Is it possible to also include options
if using a template filter?
No, not supported. All options should be defined inside a template.
1 Like
Any ideia what I’m doing wrong here? The template code works on Tools & Development tab, but in auto entities doesn’t return anything
type: custom:auto-entities
filter:
include:
- template: >
{%- for state in (states.sensor | selectattr('attributes.device_class', 'eq', 'battery')) -%}
{{
{
'entity': state.entity_id,
'name': state.attributes.friendly_name.split('Battery')[0].split('Bateria')[0] | trim
}
}},
{%- endfor -%}
- entity_id: sensor.smoke_alarm_estado_da_bateria
exclude: []
sort:
method: state
reverse: false
numeric: true
show_empty: true
card:
type: entities
state_color: true
Can not tell anything about the template itself, but this is a wrong use of auto-entities:
it must be like
filter:
template: …
Check other similar examples.
@Ildar_Gabdullin ,
I’ve been using your guidance above to try to produce a list of entities and also rename the friendly-name.
I got this to work in the developer tools tab:
{%- for s in states.sensor | selectattr('entity_id', 'search', 'recycleapp') -%}
{%- if s.attributes.Days_until is defined and s.attributes.Days_until <= 10 -%}
{{
{
'entity': s.entity_id,
'name': s.attributes.friendly_name.split(' ')[1]
}
}}
{%- endif -%}
{%- endfor -%}
It produces this:
> {'entity': 'sensor.recycleapp_restafval', 'name': 'restafval'}{'entity': 'sensor.recycleapp_papier', 'name': 'papier'}{'entity': 'sensor.recycleapp_pmd', 'name': 'pmd'}{'entity': 'sensor.recycleapp_kerstbomen', 'name': 'kerstbomen'}
But when I try it in the auto-entities, it comes up empty:
type: custom:auto-entities
card:
type: entities
filter:
template: >-
{% for s in states.sensor | selectattr('entity_id', 'search', 'recycleapp') -%}
{%- if s.attributes.Days_until is defined and s.attributes.Days_until <= 10 -%}
{{
{
'entity': s.entity_id,
'name': s.attributes.friendly_name.split(' ')[1]
}
}}
{%- endif -%}
{%- endfor %}
show_empty: true
sort:
method: attribute
attribute: Sort_date
numeric: true
Would you have a clue about what I’m doing wrong?
Thanks!
I do not have access to PC. Try checking again a difference between your code and my example.
OK I found it! I was missing a comma!
This code works:
type: custom:auto-entities
card:
type: entities
filter:
template: >-
{% for s in states.sensor | selectattr('entity_id', 'search', 'recycleapp') -%}
{%- if s.attributes.Days_until is defined and s.attributes.Days_until <= 10 -%}
{{
{
'entity': s.entity_id,
'name': s.attributes.friendly_name.split(' ')[1]
}
}},
{%- endif -%}
{%- endfor %}
show_empty: true
sort:
method: attribute
attribute: Sort_date
numeric: true