Skip to content

Commit 5ebeb4b

Browse files
Conditional case insesntive search
1 parent 56adb3c commit 5ebeb4b

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

docs/community.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ The overview video is available on YouTube [here](https://youtu.be/D403Vy2VaFA)
5454
### Search for devices with a particular battery
5555

5656
Again from the excellent [Smart Live](https://smart-live.net/battery-notes-batteriemanagement-mit-home-assistant/)
57-
This requires creating a helper of type text called `Battery search` with a max length of 20, which you can then reference in the below yaml on a dashboard.
57+
This requires creating a helper of type text called `Battery search` with a max length of 20, which you can then reference in the below yaml on a dashboard.
5858

5959
```yaml
6060
type: vertical-stack
@@ -68,13 +68,19 @@ cards:
6868
state_color: false
6969
- type: markdown
7070
content: |-
71-
{% set search_term = states('input_text.battery_search') | upper %}
72-
{% if search_term != "" %}
71+
{% set search_term = states('input_text.battery_search') | upper | trim %}
72+
{% if search_term != "" %}
7373
{% set devices = states | selectattr('attributes.battery_type', 'defined')
7474
| selectattr('entity_id', 'search', '_battery_type$') | list %}
75-
{% set matching_devices = devices | selectattr('attributes.battery_type', 'string')
76-
| selectattr('attributes.battery_type', 'eq', search_term)
77-
| map(attribute='name') | unique | list %}
75+
{% if search_term | count < 7 %}
76+
{% set matching_devices = devices | selectattr('attributes.battery_type', 'string')
77+
| selectattr('attributes.battery_type', 'eq', search_term)
78+
| map(attribute='name') | unique | list %}
79+
{% else %}
80+
{% set matching_devices = devices | selectattr('attributes.battery_type', 'string')
81+
| selectattr('attributes.battery_type', 'search', search_term, ignorecase=true)
82+
| map(attribute='name') | unique | list %}
83+
{% endif %}
7884
{% if matching_devices | length > 0 %}
7985
{{ matching_devices | join('\n') }}
8086
{% else %}

0 commit comments

Comments
 (0)