Skip to content

Commit 1132141

Browse files
committed
Implement new formatting options (#135, #151, #174)
1 parent e4c6cbe commit 1132141

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ The `format` option supports the following values:
140140
| datetime | `timestamp` | Convert timestamp value to date and time |
141141
| brightness | `number` | Convert brightness value to percentage |
142142
| duration | `number` | Convert number of seconds to duration (`5:38:50`) |
143+
| duration-m | `number` | Convert number of milliseconds to duration (`5:38:50`) |
144+
| invert | `number` | Convert number from positive to negative or vice versa |
145+
| kilo | `number` | Divide number value by 1000 (ex. `1500 W` -> `1.5 kW`) |
146+
| position | `number` | Reverses a position percentage (ex. `70%` open -> `30%` closed) |
143147
| precision<0-9> | `number` | Set decimal precision of number value (`precision3` -> `18.123`) |
144148

145149
## Examples

src/entity.js

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ export const entityStateDisplay = (hass, stateObj, config) => {
2929
return hass.localize(`state.default.${stateObj.state}`);
3030
}
3131

32-
const value = config.attribute ? stateObj.attributes[config.attribute] : stateObj.state;
33-
const unit =
32+
let value = config.attribute ? stateObj.attributes[config.attribute] : stateObj.state;
33+
let unit =
3434
config.unit === false
3535
? undefined
3636
: config.attribute !== undefined
@@ -39,23 +39,27 @@ export const entityStateDisplay = (hass, stateObj, config) => {
3939

4040
if (config.format) {
4141
if (isNaN(parseFloat(value)) || !isFinite(value)) {
42-
return value;
43-
}
44-
if (config.format === 'brightness') {
45-
return `${Math.round((value / 255) * 100)} %`;
46-
}
47-
if (config.format === 'duration') {
48-
return secondsToDuration(value);
49-
}
50-
if (config.format.startsWith('precision')) {
42+
// do nothing if not a number
43+
} else if (config.format === 'brightness') {
44+
value = Math.round((value / 255) * 100);
45+
unit = '%';
46+
} else if (config.format.startsWith('duration')) {
47+
value = secondsToDuration(config.format === 'duration-m' ? value / 1000 : value);
48+
unit = undefined;
49+
} else if (config.format.startsWith('precision')) {
5150
const precision = parseInt(config.format.slice(-1), 10);
52-
const formatted = formatNumber(parseFloat(value), hass.locale, {
51+
value = formatNumber(parseFloat(value), hass.locale, {
5352
minimumFractionDigits: precision,
5453
maximumFractionDigits: precision,
5554
});
56-
return `${formatted}${unit ? ` ${unit}` : ''}`;
55+
} else if (config.format === 'kilo') {
56+
value = formatNumber(value / 1000, hass.locale, { maximumFractionDigits: 2 });
57+
} else if (config.format === 'invert') {
58+
value = value - value * 2;
59+
} else if (config.format === 'position') {
60+
value = 100 - value;
5761
}
58-
return value;
62+
return `${value}${unit ? ` ${unit}` : ''}`;
5963
}
6064

6165
if (config.attribute) {

0 commit comments

Comments
 (0)