[APP][Pro] Power by the Hour: Insights per hour, day, month and year

Two questions for @Gruijter

  1. Unfortunately, the 15 min device still shows the wrong headings for the prices (in the test version). These are +15M, +30M, +45M prices, but the heading reads Price +1H, Price +2H, etc. Do you agree or do I misunderstand?

  2. If I manipulate the prices to reflect taxes, weekends, nights, will the prices for +15M, +30M, etc automatically be corrected?
    I am using for example the flow card for Time of Day markup: 7:0.1;22:0
    Eg. When night tariff starts at 22:00, then the Price Now reflects that, but will the +15M price still reflect day tariff until it is 22:15pm?

Many thanks

PS For those that want to work with hour prices based on the average of 15 min prices, here is a Homey script that calculates the average of 4 consecutive prices, starting from a period (0 or higher) that you need to provide as numerical input. The script will return a number tag Result with the average.

Note that it requires that all “New Prices Received for Next Hours“ from the DAP are stored in Homey variable EnergyPricesJSON

image

// --- Homey SCRIPT V2.1 --- Average EnergyPrices 4 periods---
// IMPORTANT: Change this to the exact name of your Homey variable 
// that holds the energy price JSON data.
const ENERGY_PRICE_VARIABLE_NAME = 'EnergyPricesJSON';
// --- SCRIPT LOGIC (No need to edit below this line) ---

// 1. Get the starting period from the input argument of the Flow card.
const startPeriodInput = args[0];

// 2. Convert the input argument to an integer.
const startPeriod = parseInt(startPeriodInput, 10);

// 3. Validate that the conversion resulted in a valid number.
if (isNaN(startPeriod)) {
  const errorMsg = `Error: Input must be a number. Received: '${startPeriodInput}'`;
  log(errorMsg);
  throw new Error(errorMsg);
}

// 4. Get all logic variables from Homey. (CHANGED)
const allVariables = await Homey.logic.getVariables();

// 5. Find your specific variable from the list by its name. (CHANGED)
const priceVariable = Object.values(allVariables).find(
  variable => variable.name === ENERGY_PRICE_VARIABLE_NAME
);

// 6. Check if the variable was found.
if (!priceVariable) {
  const errorMsg = `Error: Variable '${ENERGY_PRICE_VARIABLE_NAME}' not found. Please check the name in the script configuration.`;
  log(errorMsg);
  throw new Error(errorMsg);
}

// 7. Get the JSON string from the variable's 'value' property.
const priceDataJSON = priceVariable.value;

// 8. Check if the variable contains data.
if (!priceDataJSON || typeof priceDataJSON !== 'string') {
  const errorMsg = `Error: Variable '${ENERGY_PRICE_VARIABLE_NAME}' is empty or not a string.`;
  log(errorMsg);
  throw new Error(errorMsg);
}

// 9. Parse the JSON string into a JavaScript object.
let priceDataObject;
try {
  priceDataObject = JSON.parse(priceDataJSON);
} catch (e) {
  const errorMsg = `Error parsing JSON from variable '${ENERGY_PRICE_VARIABLE_NAME}'. Please check its content.`;
  log(errorMsg);
  throw new Error(errorMsg);
}

// 10. Calculate the sum of the prices for the 4 consecutive periods.
let sumOfPrices = 0;
const numberOfPeriods = 4;

for (let i = 0; i < numberOfPeriods; i++) {
  const currentPeriodKey = String(startPeriod + i);
  const price = priceDataObject[currentPeriodKey];

  if (price === undefined || typeof price !== 'number') {
    const errorMsg = `Error: Price for period '${currentPeriodKey}' not found or is not a number.`;
    log(errorMsg);
    throw new Error(errorMsg);
  }
  
  sumOfPrices += price;
}

// 11. Calculate the average price.
const averagePrice = sumOfPrices / numberOfPeriods;

// 12. Log the result for debugging in the Homey Script editor.
log(`Starting at period ${startPeriod}, the sum is ${sumOfPrices.toFixed(4)} and the average price over 4 periods is ${averagePrice.toFixed(4)}.`);

// 13. Return the calculated average so it can be used in your Flow.
return averagePrice;