Readme

Backend val for https://www.val.town/v/MattieTK.laMetricOctopus

Feel free to use it in your own apps.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { fetch } from "https://esm.town/v/std/fetch";
type OctopusPrice = {
value_exc_vat: number;
value_inc_vat: number;
valid_from: Date;
valid_to: Date;
payment_method: null;
};
type OctopusResults = {
count: number;
next: string | null;
previous: string | null;
results: OctopusPrice[];
};
export async function octopusAgilePricing(location: string): Promise<OctopusPrice> {
const locationMap = {
"London": "C",
"East Midlands": "B",
"Eastern England": "A",
"Merseyside & Northern Wales": "D",
"North Eastern England": "F",
"North Western England": "G",
"Northern Scotland": "P",
"South Eastern England": "J",
"South Western England": "L",
"Southern England": "H",
"Southern Scotland": "N",
"Southern Wales": "K",
"West Midlands": "E",
"Yorkshire": "M",
};
const locationCode = locationMap[location];
const octopusDataUrl =
`https://api.octopus.energy/v1/products/AGILE-BB-23-12-06/electricity-tariffs/E-1R-AGILE-BB-23-12-06-${locationCode}/standard-unit-rates/`;
const data = await fetch(octopusDataUrl);
let { results: results, next: next } = await data.json();
const now = results.filter((data) =>
Date.parse(data.valid_from) < Date.now()
&& Date.parse(data.valid_to) > Date.now()
);
if (now[0] == null) {
const nextData = await fetch(next);
let { results: results } = await nextData.json();
const nextNow = results.filter((data) =>
Date.parse(data.valid_from) < Date.now()
&& Date.parse(data.valid_to) > Date.now()
);
return nextNow[0];
}
else {
return (now[0]);
}
}
let result = await octopusAgilePricing("London");
console.log(result);
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
March 25, 2024