Replace string if open doors <1

Hi,
I’m trying to create a template card that change its text depending on how many doors is open.

This is my current card

type: custom:mushroom-title-card
title: Dörrar
subtitle: >-
  {{ states.binary_sensor | selectattr('state', 'eq', 'on') |
  selectattr('attributes.device_class', 'eq', 'door') | list | count }}st öppen
alignment: center

If there are more than 1 door open, I’d like to change the text to “st öppna”. What would be the best way to achieve this? Using helpers?

Also, is it possible to use icons in text?

Thank you

Maybe by using “Groups” , i.e a group for your binary-Door sensors ( so you don’t “call” every single binary sensors in your system, as you do ! )
Your template should then call the group

type: custom:mushroom-title-card
title: Dörrar
subtitle: >-
  {% set x = states.binary_sensor | selectattr('state', 'eq', 'on') |
  selectattr('attributes.device_class', 'eq', 'door') | list | count %}
  {{ x ~ "st öpp" ~ ("na" if x > 1 else "en") }}
alignment: center

Wouldn’t that also “call” every binary sensor | then check if it has the state “on” | then check whether it has “device_class” “door” ?
When one instead can call the group for the for the state “on”

Thou i like this one :wink:

Thank you, I will try out using groups and your code. Thanks!

You could go for Troon’s code , i just feel it’s creating unnecessary usage.(specially if you have tons of binary-sensors)

There is another code example to extracts from group below, i think it has to follow the guideline/format for the “old*” group-format , also look at petro’s post 2 step above, howto add “entity_id field”, so the group is updated when entities changes

Beside there are many other benefits with i.e various sensors/devices in a groups

I’ve just edited it above: needed a pair of brackets.

haha, i didn’t even noticed, and didn’t try it, … but it can come in handy in many “situations” (so i copied it) :+1: (EDIT: And i didn’t knew one could use tilde like that, it makes me “confused” :slight_smile: )

PS: should it be “else if” when one has 3 or more “states” ?

Tilde covered here:

https://jinja.palletsprojects.com/en/latest/templates/#other-operators

elif here:

https://jinja.palletsprojects.com/en/latest/templates/#if

1 Like

Thanks alot

tbh, with something like that, I don’t bother with tilda. And I know you like less characters

{{ x }}st öpp{{ iif(x > 1,"na","en") }}
1 Like

Thanks, I’ll try that.

@boheme61 I added the sensors in a group and noticed much faster response, will this also have a positive impact on battery life?

I’ve noticed some of my temperature sensors have drained their batteries to 50% in just a couple of weeks. Trying to improve signal strength with routed products and extenders, so it seems a bit better, for now at least

No. The sensors communicate with HA in exactly the same way. The group just makes it easier / faster for HA to filter and count them — it’s hard-coding the result of your two selectattr filters.

:rofl: Take your pick:

{{x~"st öpp"~"enna"[x>1::2]}}
{{x}}st öpp{{"enna"[x>1::2]}}
1 Like