Skip to content

Commit

Permalink
Merge pull request #71 from alinbalutoiu/patch-3
Browse files Browse the repository at this point in the history
Add card refresh interval to reduce CPU usage
  • Loading branch information
lozzd authored Mar 19, 2024
2 parents bf3582c + 2f81599 commit e8c401a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
18 changes: 17 additions & 1 deletion octopus-energy-rates-card.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 = {
Expand Down

0 comments on commit e8c401a

Please sign in to comment.