Created
December 2, 2020 20:03
-
-
Save AntoineLemaire/66712b1d9c76738e5df159c1b6929c43 to your computer and use it in GitHub Desktop.
Send Pushbullet notification when temperature is bellow 0 in the next 12h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var pushbullet_access_key = '123456789123456789'; // Can be create here : https://www.pushbullet.com/#settings/account | |
var accuweather_api_key = 'ABCDEFGHIJKLMNOPQRST'; // Find it https://developer.accuweather.com/user/me/apps | |
var accuweather_location_key = 123456; // Find it with https://developer.accuweather.com/accuweather-locations-api/apis/get/locations/v1/cities/search | |
function checkWeather() { | |
var accuweather_url = 'http://dataservice.accuweather.com/forecasts/v1/hourly/12hour/'; | |
var response =UrlFetchApp.fetch(accuweather_url + accuweather_location_key + '?apikey='+accuweather_api_key+'&metric=true', { | |
"method": "GET" | |
}) | |
var data = JSON.parse(response.getContentText()); | |
var minTempValue = null; | |
var minValueTime = null; | |
for (let i = 0; i < data.length; i++) { | |
if (data[i]['Temperature']['Value'] < 0) { | |
var date = new Date(data[i]['EpochDateTime'] * 1000); | |
var hours = date.getHours(); | |
var minutes = "0" + date.getMinutes(); | |
// Will display time in 10:30:23 format | |
var formattedTime = hours + 'h' + minutes.substr(-2); | |
if (minTempValue == null || minTempValue > data[i]['Temperature']['Value']) { | |
minTempValue = data[i]['Temperature']['Value']; | |
minValueTime = formattedTime; | |
} | |
} | |
} | |
if (minTempValue !== null) { | |
sendPushBulletNotification(minTempValue, minValueTime); | |
} | |
} | |
function sendPushBulletNotification(minTempValue, minValueTime) { | |
var options = { | |
"method" : "post", | |
"Content-Type" : "application/json", | |
"headers" : { "Authorization": "Basic "+Utilities.base64Encode(pushbullet_access_key+":") }, | |
"payload" : { | |
"type" : "note", | |
"title": "Attention il va faire froid", | |
"body": "Il faut couvrir la voiture ! Température minimale sur 12h : "+minTempValue+"°C à "+minValueTime+" cette nuit 🥶️", | |
} | |
}; | |
var push_bullet_url = "https://api.pushbullet.com/v2/pushes"; | |
UrlFetchApp.fetch(push_bullet_url, options); | |
return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To install it :