-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathTibberSmall.js
More file actions
208 lines (169 loc) · 6.35 KB
/
TibberSmall.js
File metadata and controls
208 lines (169 loc) · 6.35 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: cyan; icon-glyph: bolt;
// Tibber-widget
// v1.0.0 - første versjon - Sven-Ove Bjerkan
// v1.0.1 - Lagt til "HOME_NR" som innstilling
// v1.5.0 - Laget medium- og large-størrelse widget (foreløpig som 3 separate script)
// v1.5.1 - Uploaded to GitHub by Daniel Eneström (https://github.com/danielenestrom)
// Find your token by logging in with your Tibber account here:
// https://developer.tibber.com/settings/accesstoken
// NOTE! Your token is private, don't share it with anyone!
const TIBBERTOKEN = "476c477d8a039529478ebd690d35ddd80e3308ffc49b59c65b142321aee963a4";
// In most cases, the HOME_NR should be 0, but if you have several subscriptions (house + cabin eg)
// then you may need to change it to 1 (or 2).
// Try 0 first and if there is an error message, try with 1 (and then 2).
const HOME_NR = 0;
// HTML code for background color (#000000 is black)
const BACKGROUNDCOLOR = "#000000";
// HTML code for text color (#FFFFFF is white)
const TEXTCOLOR = "#FFFFFF";
// When the hourly rate is higher than the day's average rate this color is used (red)
const TEXTCOLOR_HIGH = "#de4035";
// Når prisen denne timen er lavere enn snittprisen i dag, så brukes denne tekstfargen (grønn)
const TEXTCOLOR_LOW = "#35de3b";
// YOU DON'T HAVE TO CHANGE ANYTHING BELOW !
// -----------------------------------------
// GraphQL-spørring
let body = {
"query": "{ \
viewer { \
homes { \
currentSubscription { \
priceInfo { \
current { \
total \
} \
today { \
total \
} \
} \
} \
consumption (resolution: HOURLY, last: " + new Date().getHours() + ") { \
pageInfo { \
totalConsumption \
totalCost \
} \
} \
} \
} \
}"
}
let req = new Request("https://api.tibber.com/v1-beta/gql")
req.headers = {
"Authorization": "Bearer " + TIBBERTOKEN,
"Content-Type": "application/json"
}
req.body = JSON.stringify(body)
req.method = "POST";
let json = await req.loadJSON()
// Array with all of the day's hourly prices
let allToday = json["data"]["viewer"]["homes"][HOME_NR]["currentSubscription"]["priceInfo"]["today"]
// Loop through all of the day's hourly prices to find min/max/avg
let minPrice = 100000
let maxPrice = 0
let avgPrice = 0
for (var i = 0; i < allToday.length; i++) {
if (allToday[i].total * 100 < minPrice)
minPrice = Math.round(allToday[i].total * 100)
if (allToday[i].total * 100 > maxPrice)
maxPrice = Math.round(allToday[i].total * 100)
avgPrice += allToday[i].total
}
avgPrice = avgPrice / allToday.length * 100
// Fetch total usage/cost so far today
let totCost = Math.round(json["data"]["viewer"]["homes"][HOME_NR]["consumption"]["pageInfo"]["totalCost"])
let totUsage = Math.round(json["data"]["viewer"]["homes"][HOME_NR]["consumption"]["pageInfo"]["totalConsumption"])
// Fetch price in kr for the current hour
let price = (json["data"]["viewer"]["homes"][HOME_NR]["currentSubscription"]["priceInfo"]["current"]["total"]);
// Recalculate to øre/öre
let priceOre = Math.round(price * 100)
// Fetch the Tibber logo
const TIBBERLOGO = await new Request("https://tibber.imgix.net/zq85bj8o2ot3/6FJ8FvW8CrwUdUu2Uqt2Ns/3cc8696405a42cb33b633d2399969f53/tibber_logo_blue_w1000.png").loadImage()
// Create widget
async function createWidget() {
// Create new empty ListWidget instance
let lw = new ListWidget();
// Set new background color
lw.backgroundColor = new Color(BACKGROUNDCOLOR);
// We can't control when the widget fetches a new price,
// but we try to ask the widget to refresh one minute after the next hour
var d = new Date();
d.setHours(d.getHours() + 1);
d.setMinutes(1);
lw.refreshAfterDate = d;
// Add the Tibber logo in its own stack
let stack = lw.addStack()
stack.addSpacer(12)
let imgstack = stack.addImage(TIBBERLOGO)
imgstack.imageSize = new Size(100, 30)
imgstack.centerAlignImage()
stack.setPadding(0, 0, 25, 0)
let stack2 = lw.addStack()
// Left column
let stackV = stack2.addStack();
stackV.layoutVertically()
stackV.centerAlignContent()
stackV.setPadding(0, 10, 0, 0)
// Add current price in left column
let price = stackV.addText(priceOre + "");
price.centerAlignText();
price.font = Font.lightSystemFont(20);
// Price higher or lower than the daily average defines the color
if (priceOre < avgPrice)
price.textColor = new Color(TEXTCOLOR_LOW)
if (priceOre > avgPrice)
price.textColor = new Color(TEXTCOLOR_HIGH)
const priceTxt = stackV.addText("øre/kWh");
priceTxt.centerAlignText();
priceTxt.font = Font.lightSystemFont(10);
priceTxt.textColor = new Color(TEXTCOLOR);
// Add today's "max | min" hourly price
let maxmin = stackV.addText(minPrice + " | " + maxPrice)
maxmin.centerAlignText()
maxmin.font = Font.lightSystemFont(10);
maxmin.textColor = new Color(TEXTCOLOR);
// Distance between the columns
stack2.addSpacer(20)
// Right column
let stackH = stack2.addStack();
stackH.layoutVertically()
// Add usage so far today in right column
let usage = stackH.addText(totCost + " kr");
usage.rightAlignText();
usage.font = Font.lightSystemFont(16);
usage.textColor = new Color(TEXTCOLOR);
let usage2 = stackH.addText(totUsage + " kWh");
usage2.rightAlignText();
usage2.font = Font.lightSystemFont(14);
usage2.textColor = new Color(TEXTCOLOR);
let usageTxt = stackH.addText("Hittil i dag");
usageTxt.rightAlignText();
usageTxt.font = Font.lightSystemFont(10);
usageTxt.textColor = new Color(TEXTCOLOR);
// Distance to bottom text
lw.addSpacer(30)
// Add info about when the widget was last refreshed
d = new Date()
let hour = d.getHours();
// Convert to the format HH:mm
if (hour < 10) hour = "0" + hour;
let min = d.getMinutes();
if (min < 10) min = "0" + min;
let time = lw.addText("Oppdatert: " + hour + ":" + min);
time.centerAlignText();
time.font = Font.lightSystemFont(8);
time.textColor = new Color(TEXTCOLOR);
// Return the created widget
return lw;
}
let widget = await createWidget();
// Check where the script is running
if (config.runsInWidget) {
// Runs inside a widget so add it to the homescreen widget
Script.setWidget(widget);
} else {
// Show the medium widget inside the app
widget.presentSmall();
}
Script.complete();