From a4c5d0752d3d32b5d13918f3ea30f16bf80357fe Mon Sep 17 00:00:00 2001 From: Paul Bottein Date: Mon, 4 Dec 2023 11:32:03 +0100 Subject: [PATCH] Fix number state when changing value (#1309) --- src/cards/number-card/number-card.ts | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/cards/number-card/number-card.ts b/src/cards/number-card/number-card.ts index ef894249..0e04483e 100644 --- a/src/cards/number-card/number-card.ts +++ b/src/cards/number-card/number-card.ts @@ -1,5 +1,5 @@ import { HassEntity } from "home-assistant-js-websocket"; -import { css, CSSResultGroup, html, nothing, TemplateResult } from "lit"; +import { css, CSSResultGroup, html, nothing, PropertyValues, TemplateResult } from "lit"; import { customElement, state } from "lit/decorators.js"; import { classMap } from "lit/directives/class-map.js"; import { styleMap } from "lit/directives/style-map.js"; @@ -87,6 +87,24 @@ export class NumberCard extends MushroomBaseCard implements LovelaceCard { } } + protected updated(changedProperties: PropertyValues) { + super.updated(changedProperties); + if (this.hass && changedProperties.has("hass")) { + this.updateValue(); + } + } + + updateValue() { + this.value = undefined; + if (!this._config || !this.hass || !this._config.entity) return; + + const entityId = this._config.entity; + const stateObj = this.hass.states[entityId] as HassEntity | undefined; + + if (!stateObj || Number.isNaN(stateObj.state)) return; + this.value = Number(stateObj.state); + } + protected render() { if (!this._config || !this.hass || !this._config.entity) { return nothing;