Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

prevent divisions by zero

Vincentc3bdd56b1347
diff --git a/ui/component/or-attribute-card/src/index.ts b/ui/component/or-attribute-card/src/index.ts
index 098798fcb..b8765f61b 100644
--- a/ui/component/or-attribute-card/src/index.ts
+++ b/ui/component/or-attribute-card/src/index.ts
@@ -357,8 +357,14 @@ export class OrAttributeCard extends LitElement {
protected getFormattedDelta(currentPeriodVal: number, lastPeriodVal: number): {val?: number, unit?: string} {
if (currentPeriodVal && lastPeriodVal) {
- const math = Math.round((lastPeriodVal - currentPeriodVal) / currentPeriodVal * 100);
- return {val: math, unit: "%"};
+ if (lastPeriodVal === 0 && currentPeriodVal === 0) {
+ return {val: 0, unit: "%"};
+ } else if (lastPeriodVal === 0 && currentPeriodVal !== 0) { // would be dividing something by zero
+ return {};
+ } else {
+ const math = Math.round((currentPeriodVal - lastPeriodVal) / lastPeriodVal * 100);
+ return {val: math, unit: "%"};
+ }
} else {
return {};
}