Skip to content

Commit d2b3fb5

Browse files
committed
Support hiding entities based on specific value(s) or criteria (#218)
1 parent 1132141 commit d2b3fb5

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ attribute value instead of the state value. `icon` lets you display an icon inst
8181
| icon | string/bool | `false` | Display default or custom icon instead of state or attribute value |
8282
| state_color | bool | `false` | Enable colored icon when entity is active |
8383
| hide_unavailable | bool | `false` | Hide entity if unavailable or not found |
84+
| hide_if | object/any | _[Hiding](#hiding)_ | Hide entity if its value matches specified value or criteria |
8485
| styles | object | | Add custom CSS styles to the entity element |
8586
| format | string | _[Formatting](#formatting)_ | Format entity value |
8687
| tap_action | object | _[Actions](#actions)_ | Custom entity tap action |
@@ -109,6 +110,7 @@ an object containing configuration options listed below, or any of the default s
109110
| name | string/bool | `friendly_name` | Override entity friendly name (or `false` to hide) |
110111
| unit | string/bool | `unit_of_measurement` | Override entity unit of measurement (or `false` to hide) |
111112
| hide_unavailable | bool | `false` | Hide secondary info if unavailable or not found |
113+
| hide_if | object/any | _[Hiding](#hiding)_ | Hide secondary info if value matches specified criteria |
112114
| format | string | _[Formatting](#formatting)_ | Format secondary info value |
113115

114116
### Actions
@@ -146,6 +148,18 @@ The `format` option supports the following values:
146148
| position | `number` | Reverses a position percentage (ex. `70%` open -> `30%` closed) |
147149
| precision<0-9> | `number` | Set decimal precision of number value (`precision3` -> `18.123`) |
148150

151+
### Hiding
152+
153+
The `hide_if` option can be used to hide an entity if its state or attribute value matches the specified criteria.
154+
It can be used directly with a string, number or boolean value (i.e. `hide_if: 'off'`), as a list with several values,
155+
or as an object with one or more of the options listed below.
156+
157+
| Name | Type | Description |
158+
| ------- | -------- | --------------------------------------------------------------- |
159+
| above | number | Hidden if entity _number_ value is above the specified value |
160+
| below | number | Hidden if entity _number_ value is below the specified value |
161+
| value | list/any | Hidden if value matches specified value or any value in a list |
162+
149163
## Examples
150164
151165
![multiple-entity-row](https://raw.githubusercontent.com/benct/lovelace-multiple-entity-row/master/example.png)

src/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { handleClick } from 'custom-card-helpers';
33

44
import { LAST_CHANGED, LAST_UPDATED, TIMESTAMP_FORMATS } from './lib/constants';
55
import { checkEntity, entityName, entityStateDisplay, entityStyles } from './entity';
6-
import { getEntityIds, hasConfigOrEntitiesChanged, hasGenericSecondaryInfo, hideUnavailable, isObject } from './util';
6+
import { getEntityIds, hasConfigOrEntitiesChanged, hasGenericSecondaryInfo, hideIf, isObject } from './util';
77
import { style } from './styles';
88

99
console.info(
@@ -84,7 +84,7 @@ class MultipleEntityRow extends LitElement {
8484
if (
8585
!this.config.secondary_info ||
8686
hasGenericSecondaryInfo(this.config.secondary_info) ||
87-
hideUnavailable(this.info, this.config.secondary_info)
87+
hideIf(this.info, this.config.secondary_info)
8888
) {
8989
return null;
9090
}
@@ -106,7 +106,7 @@ class MultipleEntityRow extends LitElement {
106106
}
107107

108108
renderEntity(stateObj, config) {
109-
if (!stateObj || hideUnavailable(stateObj, config)) {
109+
if (!stateObj || hideIf(stateObj, config)) {
110110
return null;
111111
}
112112
const onClick = this.clickHandler(stateObj.entity_id, config.tap_action);

src/util.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,33 @@ export const hideUnavailable = (stateObj, config) =>
88
config.hide_unavailable &&
99
(isUnavailable(stateObj) || (config.attribute && stateObj.attributes[config.attribute] === undefined));
1010

11+
export const hideIf = (stateObj, config) => {
12+
if (hideUnavailable(stateObj, config)) {
13+
return true;
14+
}
15+
if (config.hide_if === undefined) {
16+
return false;
17+
}
18+
19+
const value = config.attribute ? stateObj.attributes[config.attribute] : stateObj.state;
20+
let hideValues = [];
21+
22+
if (isObject(config.hide_if)) {
23+
if (config.hide_if.below && value < config.hide_if.below) {
24+
return true;
25+
}
26+
if (config.hide_if.above && value > config.hide_if.above) {
27+
return true;
28+
}
29+
if (config.hide_if.value) {
30+
hideValues = hideValues.concat(config.hide_if.value);
31+
}
32+
} else {
33+
hideValues = hideValues.concat(config.hide_if);
34+
}
35+
return hideValues.some((hideValue) => (typeof hideValue === 'number' ? hideValue === +value : hideValue === value));
36+
};
37+
1138
export const hasGenericSecondaryInfo = (config) => typeof config === 'string' && SECONDARY_INFO_VALUES.includes(config);
1239

1340
export const getEntityIds = (config) =>

0 commit comments

Comments
 (0)