-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeather script
More file actions
75 lines (64 loc) · 1.75 KB
/
Weather script
File metadata and controls
75 lines (64 loc) · 1.75 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
// Get weather info
let CONFIG = {
accuWeatherAPIKEY: "YOUR-ACCUWEATHER-API-KEY",
weatherForecastEndpoint:
"http://dataservice.accuweather.com/forecasts/v1/daily/1day/",
weatherCurrentEndpoint:
"http://dataservice.accuweather.com/currentconditions/v1/",
locations: {
Venice: 2579882,
Milan: 214046,
Rome: 213490,
},
//check every 30 seconds
checkInterval: 30 * 1000,
};
function getWeatherURLForLocation(location) {
return (
CONFIG.weatherCurrentEndpoint +
JSON.stringify(CONFIG.locations[location]) +
"?apikey=" +
CONFIG.accuWeatherAPIKEY +
"&details=true"
);
}
// Set schedule to manage
function goodweather () {
console.log("Good weather")
Shelly.call(
"Schedule.Update",
{ id: 1, enable: true, timespec: "@sunset+00h40m * * SUN,MON,TUE,WED,THU,FRI,SAT" },
);
}
function badweather () {
console.log("Bad weather")
Shelly.call(
"Schedule.Update",
{ id: 1, enable: true, timespec: "@sunset+00h15m * * SUN,MON,TUE,WED,THU,FRI,SAT" },
);
}
// Set conditions
function WeatherControlLocation(location) {
Shelly.call(
"http.get",
{ url: getWeatherURLForLocation(location) },
function (response, error_code, error_message, location) {
let weatherData = JSON.parse(response.body);
if (weatherData[0].CloudCover > 60) {
badweather();
}
if (weatherData[0].CloudCover < 40) {
goodweather();
}
console.log(location, " clouds - ", weatherData[0].CloudCover, "%");
},
location
);
}
//Check double during script running and locations set
Timer.set(CONFIG.checkInterval, true, function () {
console.log("Checking weather");
WeatherControlLocation("Venice");
//WeatherControlLocation("Milan");
//WeatherControlLocation("Rome");
});