From 2f81599a9548196f814e2d69519e16c7f6b044b7 Mon Sep 17 00:00:00 2001 From: Alin Balutoiu Date: Mon, 19 Feb 2024 19:13:35 +0000 Subject: [PATCH] Add card refresh interval to reduce CPU usage --- README.md | 3 ++- octopus-energy-rates-card.js | 18 +++++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 6455411..e541a03 100644 --- a/README.md +++ b/README.md @@ -106,7 +106,8 @@ Here's a breakdown of all the available configuration items: | cheapest | Y | false | If true show the cheapest rate in light green / light blue | | combinerate | Y | false | If true combine rows where the rate is the same price, useful if you have a daily tracker tarrif for instance | | multiplier | Y | 100 | multiple rate values for pence (100) or pounds (1) | -| rateListLimit | Y | N/A | Limit number of rates to display, useful if you only want to only show next 4 rates +| rateListLimit | Y | N/A | Limit number of rates to display, useful if you only want to only show next 4 rates | +| cardRefreshIntervalSeconds | Y | 60 | How often the card should refresh to avoid using lots of CPU, defaults to once a minute | #### A note on colouring diff --git a/octopus-energy-rates-card.js b/octopus-energy-rates-card.js index d3ce51e..12af202 100644 --- a/octopus-energy-rates-card.js +++ b/octopus-energy-rates-card.js @@ -124,6 +124,20 @@ class OctopusEnergyRatesCard extends HTMLElement { this.appendChild(card); } + // Initialise the lastRefreshTimestamp + if (!this.lastRefreshTimestamp) { + // Store the timestamp of the last refresh + this.lastRefreshTimestamp = 0; + } + + // Check if the interval has passed + const currentTime = Date.now(); + const cardRefreshIntervalSecondsInMilliseconds = config.cardRefreshIntervalSeconds * 1000; + if (!(currentTime - this.lastRefreshTimestamp >= cardRefreshIntervalSecondsInMilliseconds)) { + return + } + this.lastRefreshTimestamp = currentTime; + const colours_import = ['lightgreen', 'green', 'orange', 'red', 'blue', 'cheapest', 'cheapestblue']; const colours_export = ['red', 'green', 'orange', 'green']; const currentEntityId = config.currentEntity; @@ -410,7 +424,9 @@ class OctopusEnergyRatesCard extends HTMLElement { // multiple rate values for pence (100) or pounds (1) multiplier: 100, // Limit display to next X rows - rateListLimit: 0 + rateListLimit: 0, + // How often should the card refresh in seconds + cardRefreshIntervalSeconds: 60 }; const cardConfig = {