Skip to content

Instantly share code, notes, and snippets.

@adnbr
Last active April 5, 2018 12:32
Show Gist options
  • Save adnbr/80dc4cf0a52ba7c1f31c49e099af4335 to your computer and use it in GitHub Desktop.
Save adnbr/80dc4cf0a52ba7c1f31c49e099af4335 to your computer and use it in GitHub Desktop.
Trending temperature, time print on Slack is now the time of the last Thingspeak update
# -*- coding: utf-8 -*-
# Consolidates several data feeds from Thingspeak and SpaceAPI for consumption
# in Slack, where users can find out whether they should wear shorts by using
# the '/temperature' command.
# Outside weather data is from DarkSky. You'll need to get an API key.
# This script is run on a cron job every 15 minutes writing out to a static file
# available on a web server.
import urllib
import json
from datetime import datetime
import time
import calendar
inside_newspace_feed = "https://thingspeak.com/channels/251910/feed/"
outside_darksky = "https://api.darksky.net/forecast/cf3b0b8cdb7ef54eb34ea417062ade7e/53.806271,-1.533507?exclude=daily,minutely,alerts,flags,hourly&units=uk2"
# Determine the colour to be displayed next to the Slack attachment.
def determine_colour(temp):
colour = "good" # Green (Default)
if (int(temp) < 12):
colour = "#439FE0" # Blue
if (int(temp) > 24):
colour = "warning" # Yellow
if (int(temp) > 28):
colour = "danger" # Red
if (temp == "Unavailable"):
colour = "danger"
return colour
def determine_uv_index(uv):
text = ":partly_sunny: Low risk"
if (int(uv) > 2):
text = ":sunny: Moderate risk"
if (int(uv) > 5):
text = ":sunny: :sunny: High risk"
if (int(uv) > 7):
text = ":sunny: :sunny: :sunny: Very high risk"
if (int(uv) > 10):
text = ":fire: Extreme risk"
return text
def collect_data(url):
response = urllib.urlopen(url)
return json.loads(response.read())
# Outside Newspace
newspace_outside_data = collect_data(outside_darksky)
newspace_outside_temperature = newspace_outside_data["currently"]["temperature"]
newspace_uv = newspace_outside_data["currently"]["uvIndex"]
# Inside Newspace
newspace_inside_data = collect_data(inside_newspace_feed)
#print newspace_inside_data
raw_newspace_inside_temperature = newspace_inside_data["feeds"][-1]["field1"]
raw_newspace_inside_previous_temperature = newspace_inside_data["feeds"][-2]["field1"]
newspace_inside_temperature = raw_newspace_inside_temperature.split(".",1)[0]
temp_diff = float(raw_newspace_inside_temperature) - float(raw_newspace_inside_previous_temperature)
warmer_cooler = ""
if (temp_diff > 0):
warmer_cooler = " and it's getting warmer."
elif (temp_diff < 0):
warner_cooler = " and it's getting colder."
newspace_inside_humidity = newspace_inside_data["feeds"][-1]["field2"].split(".",1)[0]
thingspeak_last_update = datetime.strptime(newspace_inside_data["feeds"][-1]["created_at"], '%Y-%m-%dT%H:%M:%SZ')#2018-04-04T01:57:14Z
#newspace_inside_dust_data = newspace_inside_data["field4"]
#newspace_inside_temperature = "-273"
#newspace_inside_dust_data = "0"
newspace_colour = determine_colour(newspace_inside_temperature)
newspace_uv_text = str(newspace_uv) + " - " + determine_uv_index(newspace_uv)
# This is the entire json feed, including placeholders for the data
# Expanded it looks like this:
# https://gist.github.com/adnbr/a3f9f06cb7bcd9e7bd02e47ae35ad19a
#output = '{"username":"Environmental Bot","icon_emoji":":thermometer:","attachments":[{"color":"{newspace_colour}","title":"New Space","text":"Note: The outside temperature sensor at Hackspace3 is positioned in an area that receives direct sunlight.","mrkdwn_in":["text","pretext"],"ts":"{timecode}","fields":[{"title":"Inside Temperature","value":"{newspace_inside} {degrees}C","short":true},{"title":"Outside Temperature","value":"{newspace_outside} {degrees}C","short":true},{"title":"Inside Dust Density","value":"{newspace_inside_dust}","short":true}]},{"color":"{oldspace_colour}","title":"Current Space","mrkdwn_in":["text","pretext"],"ts":"{timecode}","fields":[{"title":"Inside Temperature","value":"{oldspace_inside} {degrees}C","short":true}]}]}'
output = {
"response_type": "in_channel",
"text":"The space is currently "+ newspace_inside_temperature + " " + unichr(176) + "C" + warmer_cooler,
"username":"Environmental Bot",
"icon_emoji":":thermometer:",
"attachments":[
{
"color":newspace_colour,
"mrkdwn_in":[
"text",
"pretext"
],
"ts":str((thingspeak_last_update - datetime(1970,1,1)).total_seconds()).split(".",1)[0],
"fields":[
{
"title":"Inside Temperature (Mainspace)",
"value": newspace_inside_temperature + " " + unichr(176) + "C",
"short":True
},
{
"title":"Outside Temperature",
"value":str(newspace_outside_temperature) + " " + unichr(176) + "C",
"short":True
},
{
"title":"Inside Relative Humidity (Mainspace)",
"value":newspace_inside_humidity + "%",
"short":True
},
{
"title":"Outside UV Index",
"value":newspace_uv_text,
"short":True
}
],
"footer": "Weather data from <https://darksky.net/forecast/53.8064,-1.5335/uk224/en|DarkSky>. Inside data from <https://thingspeak.com/channels/251910|ThingSpeak>. <https://gist.github.com/adnbr/80dc4cf0a52ba7c1f31c49e099af4335|Slack Interface Code>.",
"footer_icon": "https://darksky.net/images/logo.png"
}
]
}
print json.dumps(output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment