Skip to content

Commit

Permalink
Merge pull request #37 from igloo32/main
Browse files Browse the repository at this point in the history
Ensured Past/future entities are optional
  • Loading branch information
lozzd committed Nov 25, 2023
2 parents 7120739 + dcaf76a commit d8bd4f4
Showing 1 changed file with 62 additions and 53 deletions.
115 changes: 62 additions & 53 deletions octopus-energy-rates-card.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,20 +98,11 @@ class OctopusEnergyRatesCard extends HTMLElement {
}

const colours_import = ['green', 'red', 'orange', 'blue', 'cheapest', 'cheapestblue'];
const colours_export = ['red', 'green', 'orange'];
const colours_export = [ 'red', 'green', 'orange' ];

const currentEntityId = config.currentEntity;
const currentstate = hass.states[currentEntityId];
const currentattributes = this.reverseObject(currentstate.attributes);

const futureEntityId = config.futureEntity;
const futurestate = hass.states[futureEntityId];
const futureattributes = this.reverseObject(futurestate.attributes);

const pastEntityId = config.pastEntity;
const paststate = hass.states[pastEntityId];
const pastattributes = this.reverseObject(paststate.attributes);

const mediumlimit = config.mediumlimit;
const highlimit = config.highlimit;
const unitstr = config.unitstr;
Expand All @@ -123,76 +114,99 @@ class OctopusEnergyRatesCard extends HTMLElement {
const combinerate = config.combinerate;
const multiplier = config.multiplier
var colours = (config.exportrates ? colours_export : colours_import);

var rates_totalnumber = 0;
var combinedRates = [];

// Grab the rates which are stored as an attribute of the sensor
var ratesCurrent = currentattributes.rates;
var ratesFuture = futureattributes.rates;
var ratesPast = pastattributes.rates;
const paststate = hass.states[pastEntityId];
const currentstate = hass.states[currentEntityId];
const futurestate = hass.states[futureEntityId];

// Combine the data sources
if (typeof(paststate) != 'undefined' && paststate != null)
{
const pastattributes = this.reverseObject(paststate.attributes);
var ratesPast = pastattributes.rates;

ratesPast.forEach(function (key) {
combinedRates.push(key);
rates_totalnumber ++;
});
}

if (typeof(currentstate) != 'undefined' && currentstate != null)
{
const currentattributes = this.reverseObject(currentstate.attributes);
var ratesCurrent = currentattributes.rates;

ratesCurrent.forEach(function (key) {
combinedRates.push(key);
rates_totalnumber ++;
});
}
// Check to see if the 'rates' attribute exists on the chosen entity. If not, either the wrong entity
// was chosen or there's something wrong with the integration.
// The rates attribute also appears to be missing after a restart for a while - please see:
// https://github.com/BottlecapDave/HomeAssistant-OctopusEnergy/issues/135
if (!ratesCurrent) {
throw new Error("There are no rates assigned to that entity! Please check integration or chosen entity");
}


if (typeof(futurestate) != 'undefined' && futurestate != null)
{
const futureattributes = this.reverseObject(futurestate.attributes);
var ratesFuture = futureattributes.rates;

ratesFuture.forEach(function (key) {
combinedRates.push(key);
rates_totalnumber ++;
});
}

// This is critical to breaking down the columns properly. For now, there's now
// two loops doing the same thing which is not ideal.
// TODO: there should be one clear data process loop and one rendering loop? Or a function?
var rates_list_length = 0;
var cheapest_rate = 5000;
var previous_rate = 0;
var rates_totalnumber = 0;

var rates_currentNumber = 0;
var previous_rates_day = "";
var rates_processingRow = 0;
var combinedRates = [];
var filteredRates = [];

// Combine the data sources
ratesPast.forEach(function (key) {
combinedRates.push(key);
rates_totalnumber++;
});
ratesCurrent.forEach(function (key) {
combinedRates.push(key);
rates_totalnumber++;
});
ratesFuture.forEach(function (key) {
combinedRates.push(key);
rates_totalnumber++;
});

// filter out rates to display
combinedRates.forEach(function (key) {
const date_milli = Date.parse(key.start);
var date = new Date(date_milli);
const lang = navigator.language || navigator.languages[0];
var current_rates_day = date.toLocaleDateString(lang, { weekday: 'short' });
rates_processingRow++;
rates_processingRow ++;
var ratesToEvaluate = key.value_inc_vat * multiplier;

if (showpast || (date - Date.parse(new Date()) > -1800000)) {
if(showpast || (date - Date.parse(new Date())>-1800000))
{
rates_currentNumber++;

// Find the cheapest rate that hasn't past yet
if ((ratesToEvaluate < cheapest_rate) && (date - Date.parse(new Date()) > -1800000)) cheapest_rate = ratesToEvaluate;

if ((ratesToEvaluate < cheapest_rate) && (date - Date.parse(new Date())>-1800000)) cheapest_rate = ratesToEvaluate;
// If we don't want to combine same values rates then just push them to new display array
if (!combinerate) {
if (!combinerate){
filteredRates.push(key);
rates_list_length++;
}

if (combinerate &&
(
if (combinerate &&
(
(rates_currentNumber == 1)
|| (current_rates_day != previous_rates_day)
|| (current_rates_day != previous_rates_day)
|| (previous_rate != ratesToEvaluate)
)
)
) {
filteredRates.push(key);
rates_list_length++;
{
filteredRates.push(key);
rates_list_length++;
}
previous_rate = ratesToEvaluate;
previous_rates_day = current_rates_day;
Expand All @@ -210,7 +224,7 @@ class OctopusEnergyRatesCard extends HTMLElement {
const date_milli = Date.parse(key.start);
var date = new Date(date_milli);
const lang = navigator.language || navigator.languages[0];
var options = { hourCycle: 'h23', hour12: hour12, hour: '2-digit', minute: '2-digit' };
var options = {hourCycle: 'h23', hour12: hour12, hour: '2-digit', minute:'2-digit'};
// The time formatted in the user's Locale
var time_locale = date.toLocaleTimeString(lang, options);
// If the showday config option is set, include the shortened weekday name in the user's Locale
Expand All @@ -224,9 +238,9 @@ class OctopusEnergyRatesCard extends HTMLElement {
else if (valueToDisplay > mediumlimit) colour = colours[2];
else if (valueToDisplay <= 0) colour = colours[3];

if (showpast || (date - Date.parse(new Date()) > -1800000)) {
table = table.concat("<tr class='rate_row'><td class='time time_" + colour + "'>" + date_locale + time_locale +
"</td><td class='rate " + colour + "'>" + valueToDisplay.toFixed(roundUnits) + unitstr + "</td></tr>");
if(showpast || (date - Date.parse(new Date())>-1800000)) {
table = table.concat("<tr class='rate_row'><td class='time time_"+colour+"'>" + date_locale + time_locale +
"</td><td class='rate "+colour+"'>" + valueToDisplay.toFixed(roundUnits) + unitstr + "</td></tr>");

if (x % rows_per_col == 0) {
tables = tables.concat(table);
Expand All @@ -236,7 +250,7 @@ class OctopusEnergyRatesCard extends HTMLElement {
tables = tables.concat("<td><table class='sub_table'><tbody>");
}
};
x++;
x++;
}
});
tables = tables.concat(table);
Expand Down Expand Up @@ -273,10 +287,6 @@ class OctopusEnergyRatesCard extends HTMLElement {
}

const defaultConfig = {
// Entities to get data from
currentEntity: null,
pastEntity: null,
futureEntity: null,
// Controls how many columns the rates split in to
cols: 1,
// Show rates that already happened in the card
Expand Down Expand Up @@ -314,7 +324,6 @@ class OctopusEnergyRatesCard extends HTMLElement {
};

this._config = cardConfig;

}

// The height of your card. Home Assistant uses this to automatically
Expand Down

0 comments on commit d8bd4f4

Please sign in to comment.