Fix counting entity states

* Add state filters to getCountTemplate()  
States `unavailable` and `unknown` are now excluded from the return
value of `getCountTemplate()`, since there's no interest in counting
those states. This change affect all entities.

* Add states to unclosed cover count  
Aside from state `open`, the cover-chip and -view, will include state 
`opening` and `closing` to the count of unclosed covers.
This commit is contained in:
Ferry Cools
2024-12-13 07:29:15 +01:00
committed by GitHub
parent 9041e0b647
commit ebfd034e20
5 changed files with 19 additions and 7 deletions

View File

@ -218,9 +218,10 @@ class Helper {
* Get a template string to define the number of a given domain's entities with a certain state. * Get a template string to define the number of a given domain's entities with a certain state.
* *
* States are compared against a given value by a given operator. * States are compared against a given value by a given operator.
* States `unavailable` and `unknown` are always excluded.
* *
* @param {string} domain The domain of the entities. * @param {string} domain The domain of the entities.
* @param {string} operator The Comparison operator between state and value. * @param {string} operator The comparison operator between state and value.
* @param {string} value The value to which the state is compared against. * @param {string} value The value to which the state is compared against.
* *
* @return {string} The template string. * @return {string} The template string.
@ -265,7 +266,16 @@ class Helper {
states.push(...newStates); states.push(...newStates);
} }
return `{% set entities = [${states}] %} {{ entities | selectattr('state','${operator}','${value}') | list | count }}`; return (
`{% set entities = [${states}] %}
{{ entities
| selectattr('state','${operator}','${value}')
| selectattr('state','ne','unavailable')
| selectattr('state','ne','unknown')
| list
| count
}}`
);
} }
/** /**

View File

@ -22,7 +22,7 @@ class CoverChip extends AbstractChip {
type: "template", type: "template",
icon: "mdi:window-open", icon: "mdi:window-open",
icon_color: "cyan", icon_color: "cyan",
content: Helper.getCountTemplate("cover", "eq", "open"), content: Helper.getCountTemplate("cover", "search", "(open|opening|closing)"),
tap_action: { tap_action: {
action: "none", action: "none",
}, },

View File

@ -28,7 +28,8 @@
"numbers": "Numbers", "numbers": "Numbers",
"off": "Off", "off": "Off",
"on": "On", "on": "On",
"open": "Open" "open": "Open",
"unclosed": "Unclosed"
}, },
"input_select": { "input_select": {
"input_selects": "Input Selects" "input_selects": "Input Selects"

View File

@ -28,7 +28,8 @@
"numbers": "Nummers", "numbers": "Nummers",
"off": "Uit", "off": "Uit",
"on": "Aan", "on": "Aan",
"open": "Open" "open": "Open",
"unclosed": "Ongesloten"
}, },
"input_select": { "input_select": {
"input_selects": "Lijsten" "input_selects": "Lijsten"

View File

@ -51,8 +51,8 @@ class CoverView extends AbstractView {
#viewControllerCardConfig: cards.ControllerCardOptions = { #viewControllerCardConfig: cards.ControllerCardOptions = {
title: Helper.customLocalize("cover.all_covers"), title: Helper.customLocalize("cover.all_covers"),
subtitle: subtitle:
`${Helper.getCountTemplate(CoverView.#domain, "eq", "open")} ${Helper.customLocalize("cover.covers")} ` `${Helper.getCountTemplate(CoverView.#domain, "search", "(open|opening|closing)")} ${Helper.customLocalize("cover.covers")} `
+ Helper.customLocalize("generic.open"), + Helper.customLocalize("generic.unclosed"),
}; };
/** /**