diff --git a/octopus-energy-rates-card.js b/octopus-energy-rates-card.js index f107f25..598517d 100644 --- a/octopus-energy-rates-card.js +++ b/octopus-energy-rates-card.js @@ -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; @@ -123,11 +114,36 @@ 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: @@ -135,64 +151,62 @@ class OctopusEnergyRatesCard extends HTMLElement { 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; @@ -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 @@ -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("