Repository navigation
Expand file tree
/
Copy pathWeather_heating_control.js
More file actions
90 lines (83 loc) · 2.32 KB
/
Copy pathWeather_heating_control.js
File metadata and controls
90 lines (83 loc) · 2.32 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
let CONFIG = {
accuWeatherAPIKEY: "YOUR ACCUWETAHER API KEY",
weatherForecastEndpoint:
"http://dataservice.accuweather.com/forecasts/v1/daily/1day/",
weatherCurrentEndpoint:
"http://dataservice.accuweather.com/currentconditions/v1/",
locations: {
// Set your location name and code
Sofia: 51097,
},
//check every 60 seconds
checkInterval: 60 * 1000,
//Ousdide temperature
tempBelowTurnOn: 7,
tempAboveTurnOff: 10,
//Working interval
workTimeStart: 7,
workTimeEnd: 22,
};
let workingH = true;
function getWeatherURLForLocation(location) {
return (
CONFIG.weatherCurrentEndpoint +
JSON.stringify(CONFIG.locations[location]) +
"?apikey=" +
CONFIG.accuWeatherAPIKEY +
"&details=false"
);
}
function activateSwitch(activate) {
Shelly.call(
"Switch.Set",
{ id: 1, on: activate },
function (response, error_code, error_message) {}
);
}
function TemperatureControlLocation(location) {
Shelly.call(
"http.get",
{ url: getWeatherURLForLocation(location) },
function (response, error_code, error_message, location) {
let weatherData = JSON.parse(response.body);
if (weatherData[0].Temperature.Metric.Value <= CONFIG.tempBelowTurnOn) {
activateSwitch(true);
}
if (weatherData[0].Temperature.Metric.Value >= CONFIG.tempAboveTurnOff) {
activateSwitch(false);
}
print(
location,
" Temperature - ",
weatherData[0].Temperature.Metric.Value,
"deg C"
);
},
location
);
}
function CheckWorkingtime() {
Shelly.call(
"http.get",
{ url: "http://worldtimeapi.org/api/timezone/Europe/Sofia" },
function (response, error_code, error_message) {
let currentTime = JSON.parse(response.body).datetime;
let currentHour = currentTime.slice(11,13);
print("Current time in Sofia", currentHour);
if (JSON.parse(currentHour) < CONFIG.workTimeEnd && JSON.parse(currentHour) > CONFIG.workTimeStart) {
workingH = true;
} else {
workingH = false;
activateSwitch(false);
}
print(workingH);
}
);
}
Timer.set(CONFIG.checkInterval, true, function () {
CheckWorkingtime();
if (workingH === true) {
print("Checking weather");
TemperatureControlLocation("Sofia");
}
});