-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoctopus.html
76 lines (69 loc) · 2.76 KB
/
octopus.html
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<!DOCTYPE html>
<html>
<head>
<title>Easee Site Price Updater for Octopus Agile</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.33/moment-timezone-with-data.min.js"></script>
</head>
<body>
<h1>Easee Site Price Updater for Octopus Agile</h1>
<div id="data"></div>
<script>
function getCurrentAndNextHalfHour() {
var now = moment().tz("Europe/London");
now.minutes(now.minutes() < 30 ? 0 : 30).seconds(0).milliseconds(0);
var next = moment(now).add(30, 'minutes');
return [now.toISOString(), next.toISOString()];
}
var timeParams = getCurrentAndNextHalfHour();
var url = 'https://api.octopus.energy/v1/products/AGILE-FLEX-22-11-25/electricity-tariffs/E-1R-AGILE-FLEX-22-11-25-H/standard-unit-rates/?period_from=' + encodeURIComponent(timeParams[0]) + '&period_to=' + encodeURIComponent(timeParams[1]);
fetch(url, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
})
.then(response => response.json())
.then(data => {
var valueIncVat = data.results[0].value_inc_vat;
var valueIncVatInPounds = valueIncVat / 100; // convert pennies to pounds
var roundedValueIncVat = parseFloat(valueIncVatInPounds).toFixed(2);
console.log(roundedValueIncVat);
document.getElementById("data").innerText = roundedValueIncVat;
const loginOptions = {
method: 'POST',
headers: {
accept: 'application/json',
'content-type': 'application/json'
},
body: JSON.stringify({ username: '***YOURUSERNAME***', password: '***YOURPASSWORD***' })
};
fetch('https://api.easee.com/api/accounts/login', loginOptions)
.then(response => response.json())
.then(data => {
const accessToken = data.accessToken;
const priceOptions = {
method: 'POST',
headers: {
'content-type': 'application/*+json',
Authorization: `Bearer ${accessToken}`
},
body: JSON.stringify({ currencyId: 'GBP', costPerKWh: roundedValueIncVat })
};
fetch('https://api.easee.com/api/sites/***YOURSITEID***/price', priceOptions)
.then(response => {
return response.text().then(function(text) {
return text ? JSON.parse(text) : {}
})
})
.then(data => console.log(data))
.catch(error => console.error(error));
})
.catch(error => console.error(error));
})
.catch((error) => {
console.error('Error:', error);
});
</script>
</body>
</html>