-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
114 lines (96 loc) · 3.3 KB
/
app.js
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
'use strict';
const Homey = require('homey');
const { HomeyAPI } = require('athom-api');
class MyApp extends Homey.App {
/**
* onInit is called when the app is initialized.
*/
async onInit() {
this.log('MyApp has been initialized');
this.homey.api = await HomeyAPI.forCurrentHomey(this.homey);
this.runCheck();
this.everyHour();
}
everyHour() {
const now = new Date();
const nextHour = new Date(now.getFullYear(), now.getMonth(), now.getDate(), now.getHours() + 0, 59, 50, 0);
const timeToNextHour = nextHour - now;
// console.log('everyHour starts in', timeToNextHour / 1000);
this.homey.setTimeout(() => {
this.homey.setInterval(() => {
//this.homey.emit('everyhour', true);
this.runCheck()
}, 60 * 60 * 1000);
//this.homey.emit('everyhour', true);
this.runCheck()
}, timeToNextHour);
this.log('everyHour job started');
}
async runCheck() {
try {
// Get all devices
const devices = await this.homey.api.devices.getDevices();;
let measurableDevices = await this.homey.settings.get('data')
// Loop over all devices
for (const device of Object.values(devices)) {
//console.log(device)
let currentDate = new Date();
currentDate.setMinutes(0,0,0);
let prevDate = new Date(currentDate)
prevDate.setHours(currentDate.getHours()-1)
if (device.capabilities.indexOf('meter_power') > -1) {
let tibberData = getTibberData();
let deviceData = {
id: device.id,
name: device.name,
zoneName: device.zoneName,
currentMeter: device.capabilitiesObj.meter_power.value
}
let kwhSinceLastHour = ((measurableDevices[device.id]) && (measurableDevices[device.id].data[prevDate])) ? deviceData.currentMeter-measurableDevices[device.id].data[prevDate].meter : 0
if (measurableDevices[device.id]) {
measurableDevices[device.id].data = {
...measurableDevices[device.id].data,
[currentDate] : {
meter: deviceData.currentMeter,
kwh: kwhSinceLastHour,
cost: kwhSinceLastHour*tibberData.price,
kwhPrice: tibberData.price
}
}
measurableDevices[device.id].deviceInfo = deviceData
} else {
measurableDevices[device.id] = {
deviceInfo: deviceData,
data: {
[currentDate]: {
meter: deviceData.currentMeter,
kwh: kwhSinceLastHour,
cost: kwhSinceLastHour*tibberData.price,
kwhPrice: tibberData.price
}
}
}
}
}
}
await this.homey.settings.set('data', measurableDevices)
function getTibberData() {
let data = {
lastUpdated: null,
price: null
}
for (const device of Object.values(devices)) {
//console.log(device)
if (device.driverUri.includes('tibber')) {
data.price = device.capabilitiesObj.price_total.value;
data.lastUpdated = device.capabilitiesObj.price_total.lastUpdated;
}
}
return data;
}
} catch (err) {
this.log(err)
}
}
}
module.exports = MyApp;