Skip to content

Commit

Permalink
Merge pull request #61 from corvus2606/main
Browse files Browse the repository at this point in the history
Feat: current time indicator on grid
Feat: support a variable for limits, with multipliers 
Feat: support a second target rate entity
  • Loading branch information
lozzd authored Jan 26, 2024
2 parents 001ebd6 + bdb9c14 commit 7651151
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 4 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,17 @@ Here's a breakdown of all the available configuration items:
| pastEntity | Y | N/A | Name of the sensor that contains the past rates you want to render, generated from the `HomeAssistant-OctopusEnergy` integration |
| futureEntity | Y | N/A | Name of the sensor that contains the future rates you want to render, generated from the `HomeAssistant-OctopusEnergy` integration |
| targetTimesEntity | Y | N/A | Name of the sensor that contains the Target Rate Sensor, generated from the `HomeAssistant-OctopusEnergy` integration. [More here: doc](https://github.com/BottlecapDave/HomeAssistant-OctopusEnergy/blob/develop/_docs/setup_target_rate.md) |
| targetTimesEntity2 | Y | N/A | Name of the second sensor that contains the Target Rate Sensor, generated from the `HomeAssistant-OctopusEnergy` integration. [More here: doc](https://github.com/BottlecapDave/HomeAssistant-OctopusEnergy/blob/develop/_docs/setup_target_rate.md) |
| cols | Y | 1 | How many columns to break the rates in to, pick the one that fits best with how wide your card is |
| showpast | Y | false | Show the rates that have already happened today. Provides a simpler card when there are two days of dates to show |
| showday | Y | false | Shows the (short) day of the week next to the time for each rate. Helpful if it's not clear which day is which if you have a lot of rates to display |
| title | Y | "Agile Rates" | The title of the card in the dashboard |
| lowlimit | Y | 5 (pence) | If the price is above `lowlimit`, the row is marked dark green. (this option is only applicable for import rates |
| mediumlimit | Y | 20 (pence) | If the price is above `mediumlimit`, the row is marked yellow |
| highlimit | Y | 30 (pence) | If the price is above `highlimit`, the row is marked red. |
| limitEntity | Y | N/A | Name of the sensor tracking the unit rate to be used to calculate limits. e.g. average rate for the last 12 hours If this is set, MediumLimit and HighLimit are ignored|
| highLimitMultiplier | Y | 1.1 | Multiplication factor for Limit Entity, 1.1 = 110% of the entity value. |
| mediumLimitMultiplier | Y | 0.8 | Multiplication factor for Limit Entity, 0.8 = 80% of the entity value. |
| roundUnits | Y | 2 | Controls how many decimal places to round the rates to |
| showunits | Y | N/A | No longer supported. Never worked. Please set a blank string using `unitstr` (see below) |
| unitstr | Y | "p/kWh" | The unit to show after the rate in the table. Set to an empty string for none. |
Expand Down
61 changes: 57 additions & 4 deletions octopus-energy-rates-card.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,23 @@ class OctopusEnergyRatesCard extends HTMLElement {
td.time_highlight {
font-weight: bold;
background-color: Navy;
color: white;
}
td.current {
position: relative;
}
td.current:before{
content: "";
position: absolute;
top: 0;
right: 0;
width: 0;
height: 0;
display: block;
border-top: calc(var(--paper-font-body1_-_line-height)*0.65) solid transparent;
border-bottom: calc(var(--paper-font-body1_-_line-height)*0.65) solid transparent;
border-right: 10px solid;
}
thead th {
text-align: left;
Expand Down Expand Up @@ -117,10 +134,14 @@ class OctopusEnergyRatesCard extends HTMLElement {
const targetTimesId = config.targetTimesEntity;
const targetTimesstate = hass.states[targetTimesId];
const targetTimesttributes = targetTimesstate ? this.reverseObject(targetTimesstate.attributes) : {};

// Read the 2nd targetTimes entity if specified
const targetTimesId2 = config.targetTimesEntity2;
const targetTimesstate2 = hass.states[targetTimesId2];
const targetTimesttributes2 = targetTimesstate2 ? this.reverseObject(targetTimesstate2.attributes) : {};

const lowlimit = config.lowlimit;
const mediumlimit = config.mediumlimit;
const highlimit = config.highlimit;
var mediumlimit = config.mediumlimit;
var highlimit = config.highlimit;
const unitstr = config.unitstr;
const roundUnits = config.roundUnits;
const showpast = config.showpast;
Expand All @@ -134,11 +155,24 @@ class OctopusEnergyRatesCard extends HTMLElement {
var combinedRates = [];
// Check if slotsTargetTimes is available before using forEach
const slotsTargetTimes = targetTimesttributes.target_times || [];
const slotsTargetTimes2 = targetTimesttributes2.target_times || [];

// Grab the rates which are stored as an attribute of the sensor
const paststate = hass.states[pastEntityId];
const currentstate = hass.states[currentEntityId];
const futurestate = hass.states[futureEntityId];

// Get Limit entity values
const limitEntity = config.limitEntity;
const limitEntityState = hass.states[limitEntity];
const limitHighMult = config.highLimitMultiplier;
const limitMedMult = config.mediumLimitMultiplier;

if(!(limitEntity == null)){
const limitAve = parseFloat(limitEntityState.state);
mediumlimit = limitAve * LimitMedMult;
highlimit = limitAve * LimitHighMult;
};

// Combine the data sources
if (typeof(paststate) != 'undefined' && paststate != null)
Expand Down Expand Up @@ -258,11 +292,25 @@ class OctopusEnergyRatesCard extends HTMLElement {
isTargetTime = true;
}
});
slotsTargetTimes2.forEach(function (targetTime) {
const startTime = new Date(targetTime.start);
const endTime = new Date(targetTime.end);
if (date >= startTime && date < endTime) {
isTargetTime = true;
}
});
var isCurrentTime = false;
if((date - Date.parse(new Date())>-1800000) &&(date < new Date())) {
if(showpast){
isCurrentTime = true;
};
};


var valueToDisplay = key.value_inc_vat * multiplier;
// Apply bold styling if the current time is a target time
var boldStyle = isTargetTime ? 'time_highlight' : '';
var boldStyle = isCurrentTime ? "current " : "";
boldStyle = isTargetTime ? boldStyle + "time_highlight" : boldStyle + "";
if (cheapest && (valueToDisplay == cheapest_rate && cheapest_rate > 0)) colour = colours[5];
else if (cheapest && (valueToDisplay == cheapest_rate && cheapest_rate <= 0)) colour = colours[6];
else if (valueToDisplay > highlimit) colour = colours[3]; //red (import) / green (export)
Expand Down Expand Up @@ -321,6 +369,7 @@ class OctopusEnergyRatesCard extends HTMLElement {
const defaultConfig = {
// Entities to get data from
targetTimesEntity: null,
targetTimesEntity2: null,
// Controls how many columns the rates split in to
cols: 1,
// Show rates that already happened in the card
Expand All @@ -340,6 +389,10 @@ class OctopusEnergyRatesCard extends HTMLElement {
lowlimit: 5,
mediumlimit: 20,
highlimit: 30,
// Entity to use for dynamic limits, above are ignored if limitEntity is set.
limitEntity: null,
highLimitMultiplier: 1.1,
mediumLimitMultiplier: 0.8,
// Controls the rounding of the units of the rate
roundUnits: 2,
// The unit string to show if units are shown after each rate
Expand Down

0 comments on commit 7651151

Please sign in to comment.