From c5f4d339974a75b3512b8e798fe009f72797b63a Mon Sep 17 00:00:00 2001 From: Ville Seppanen Date: Sat, 27 Apr 2024 21:14:23 +0300 Subject: [PATCH] feat: update electricity prices --- __tests__/unit/index.test.ts | 10 ++++++---- src/index.ts | 30 +++++++++++++++++++++--------- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/__tests__/unit/index.test.ts b/__tests__/unit/index.test.ts index c34b7fe..60ba7e6 100644 --- a/__tests__/unit/index.test.ts +++ b/__tests__/unit/index.test.ts @@ -2,10 +2,12 @@ import { getPriceWithFeesAndTaxes } from '../../src/index'; describe('getPriceWithFeesAndTaxes', () => { test('should return correct total hourly price', () => { - expect(getPriceWithFeesAndTaxes(4.075).toFixed(5)).toEqual("12.34672"); - expect(getPriceWithFeesAndTaxes(10).toFixed(5)).toEqual("19.69372"); - expect(getPriceWithFeesAndTaxes(17.37).toFixed(5)).toEqual("28.83252"); - expect(getPriceWithFeesAndTaxes(35).toFixed(5)).toEqual("50.69372"); + expect(getPriceWithFeesAndTaxes(4.075).toFixed(5)).toEqual("11.16672"); + expect(getPriceWithFeesAndTaxes(10).toFixed(5)).toEqual("18.51372"); + expect(getPriceWithFeesAndTaxes(17.37).toFixed(5)).toEqual("27.65252"); + expect(getPriceWithFeesAndTaxes(35).toFixed(5)).toEqual("49.51372"); + expect(getPriceWithFeesAndTaxes(12.423).toFixed(2)).toEqual("21.52"); + expect(getPriceWithFeesAndTaxes(4.123).toFixed(0)).toEqual("11"); }); }); diff --git a/src/index.ts b/src/index.ts index 647af3c..70c9e5a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,9 +3,16 @@ import { SpotPrice, TimeSegment } from './datasource/datasourceTypes'; import { renderMessage } from './telegram/render'; import { TelegramClient } from './telegram/telegram'; -const ELECTRICITY_COMPANY_MARGIN = 1.25; +// General multiplier for VAT 24%, for spot prices which are received in VAT 0% const ELECTRICITY_TAX_MULTIPLIER = 1.24; -const GRID_SERVICE_FEE = 3.25; + +// Electricity service company margin, eurocents per kWh, including VAT 24% +const ELECTRICITY_COMPANY_MARGIN = 0.42; + +// Electricity transfer fee for grid operator, including VAT 24% +const GRID_SERVICE_FEE = 2.90; + +// Electricity grid tax for households (Tax Class 1), eurocents per kWh including VAT 24% and including "Huoltovarmuusmaksu" 0,01612 c/kWh const GRID_SERVICE_TAX_FOR_HOUSEHOLDS = 2.79372; export const mainApp = async (dryrun: boolean): Promise => { @@ -21,17 +28,20 @@ export const mainApp = async (dryrun: boolean): Promise => { console.log(`Fetching prices from ${startDate} to ${endDate}`); const hourlySpotPrices = await sourceClient.getSpotPrices(startDate, endDate); + // API should return prices for 48 hours, for today and tomorrow if (hourlySpotPrices.length == 48) { + // Daytime prices include hourly prices between 07-23, and min,avg,max of these hours + // We are not interested in nighttime prices, because we're only consuming electricity during the day + const todaysDaytimePrices: TimeSegment = getSegment(hourlySpotPrices.slice(7, 24)); // Today 07-23 + const tomorrowsDaytimePrices: TimeSegment = getSegment(hourlySpotPrices.slice(31)); // Tomorrow 07-23 + const interestingSlices = [ - hourlySpotPrices.slice(24, 31), - hourlySpotPrices.slice(31, 40), - hourlySpotPrices.slice(41, 44), - hourlySpotPrices.slice(44, 48), + hourlySpotPrices.slice(24, 31), // Tomorrow 00-06 + hourlySpotPrices.slice(31, 41), // Tomorrow 07-16 + hourlySpotPrices.slice(41, 44), // Tomorrow 17-19 + hourlySpotPrices.slice(44, 48), // Tomorrow 20-23 ]; - - const todaysDaytimePrices: TimeSegment = getSegment(hourlySpotPrices.slice(7, 24)); - const tomorrowsDaytimePrices: TimeSegment = getSegment(hourlySpotPrices.slice(31)); const tomorrowsDetailedPrices: TimeSegment[] = interestingSlices.map(slice => getSegment(slice)); const message = renderMessage(todaysDaytimePrices, tomorrowsDaytimePrices, tomorrowsDetailedPrices); @@ -40,6 +50,7 @@ export const mainApp = async (dryrun: boolean): Promise => { } else { console.log("Dryrun, not sending a message to Telegram"); + console.log(todaysDaytimePrices); console.log(tomorrowsDaytimePrices); console.log(message); } @@ -66,6 +77,7 @@ export const getSegment = (segment: SpotPrice[]): TimeSegment => { } } +// Returns total electricity cost including all taxes and grid fees when given only the service provider price with VAT 0% export const getPriceWithFeesAndTaxes = (basePrice: number): number => { return (basePrice * ELECTRICITY_TAX_MULTIPLIER) + ELECTRICITY_COMPANY_MARGIN + GRID_SERVICE_FEE + GRID_SERVICE_TAX_FOR_HOUSEHOLDS; }