Skip to content

Instantly share code, notes, and snippets.

@danroot
Last active December 26, 2017 20:41
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danroot/8837291 to your computer and use it in GitHub Desktop.
Save danroot/8837291 to your computer and use it in GitHub Desktop.
SmartThings:Color the Weather
/*
Color the weather
Daniel Root
www.danielroot.info
Should work (but not yet tested with) Phillips Hue devices and any SmartThings device that supports "capability.colorControl".
See https://codebender.cc/sketch:28062 for Arduino code that works with the Arduino SmartShield.
*/
preferences {
section("Check the weather in"){
input "zipcode", "text", title: "Zipcode?"
}
section("At this time"){
input "startTime", "time", title: "Start time"
}
section("Or when this switch is on")
{
input "testSwitch", "capability.switch", title:"Switch"
}
section("Set these color devices"){
input "colorDevices", "capability.colorControl", title: "Color devices", multiple:true
}
section("To these colors"){
input "rainColor", "text", title:"Rain", defaultValue:'#08088A'
input "snowColor", "text", title:"Snow", defaultValue:'#00CCFF'
input "clearColor", "text", title:"Clear", defaultValue:'#cccccc'
input "sunnyColor","text", title:"Sunny", defaultValue:'#FFFF00'
input "hotColor","text", title:"Hot", defaultValue:'#FF3300'
input "cloudyColor","text", title:"Cloudy", defaultValue:'#4C4C4C'
}
section("Then turn them off at"){
input "endTime", "time", title: "End Time"
}
}
def installed() {
log.debug "Installed: $settings"
subscribe(testSwitch,"switch.on","scheduleCheck")
schedule(startTime, "scheduleCheck")
schedule(endTime, "endScheduleCheck")
}
def updated() {
log.debug "Updated: $settings"
unschedule()
unsubscribe()
subscribe(testSwitch,"switch.on","scheduleCheck")
schedule(startTime, "scheduleCheck")
schedule(endTime, "endScheduleCheck")
}
def scheduleCheck(evt) {
log.debug "checking the weather"
def response = getWeatherFeature("forecast", zipcode)
log.debug "got some weather, reading it..."
def forecastColor = parseForecast(response)
log.debug "setting color to $forecastColor"
colorDevices.each {
it?.on()
it?.setColor(forecastColor)
}
}
def endScheduleCheck(){
colorDevices.each {
it?.off()
}
}
private textContainsAnyOf(text, keywords)
{
def result = '';
for (int i = 0; i < keywords.size(); i++) {
result = text.contains(keywords[i])
if (result == true) return result
}
return result;
}
private parseForecast(json)
{
def snowKeywords = ['snow','flurries','sleet']
def rainKeywords = ['rain', 'showers', 'sprinkles', 'precipitation']
def clearKeywords = ['clear']
def sunnyKeywords = ['sunny']
def hotKeywords = ['hot']
def cloudyKeywords = ['overcast','cloudy']
def result = '#000000';
def forecast = json?.forecast?.txt_forecast?.forecastday?.first()
if (forecast) {
def text = forecast?.fcttext?.toLowerCase()
def day = forecast?.title
log.debug text
if (text) {
if(textContainsAnyOf(text,cloudyKeywords)) result = cloudyColor
if(textContainsAnyOf(text,clearKeywords)) result = clearColor
if(textContainsAnyOf(text,sunnyKeywords)) result = sunnyColor
if(textContainsAnyOf(text,hotKeywords)) result = hotColor
if(textContainsAnyOf(text,rainKeywords)) result = rainColor
if(textContainsAnyOf(text,snowKeywords)) result = snowColor
}
sendNotificationEvent("Weather for $day : $text")
sendNotificationEvent("Setting weather lights to $result")
}
else
{
sendNotificationEvent("Could not get weather!")
}
log.debug result
return result
}
metadata {
// Simulator metadata
simulator {
status "on": "catchall: 0104 0000 01 01 0040 00 0A21 00 00 0000 0A 00 0A6F6E"
status "off": "catchall: 0104 0000 01 01 0040 00 0A21 00 00 0000 0A 00 0A6F6666"
// reply messages
reply "raw 0x0 { 00 00 0a 0a 6f 6e }": "catchall: 0104 0000 01 01 0040 00 0A21 00 00 0000 0A 00 0A6F6E"
reply "raw 0x0 { 00 00 0a 0a 6f 66 66 }": "catchall: 0104 0000 01 01 0040 00 0A21 00 00 0000 0A 00 0A6F6666"
}
// UI tile definitions
tiles {
standardTile("switch", "device.switch", width: 2, height: 2, canChangeIcon: true, canChangeBackground: true) {
state "on", label: '${name}', action: "switch.off", icon: "st.switches.switch.on", backgroundColor: "#79b821"
state "off", label: '${name}', action: "switch.on", icon: "st.switches.switch.off", backgroundColor: "#ffffff"
}
controlTile("rgbSelector", "device.color", "color", height: 3, width: 3, inactiveLabel: false) {
state "color", action:"setColor"
}
main(["switch"])
details(["switch","rgbSelector"])
}
}
// Parse incoming device messages to generate events
def parse(String description) {
def value = zigbee.parse(description)?.text
def name = value in ["on","off"] ? "switch" : null
def result = createEvent(name: name, value: value)
log.debug "Parse returned ${result?.descriptionText}"
return result
}
// Commands sent to the device
def on() {
zigbee.smartShield(text: "on").format()
}
def off() {
zigbee.smartShield(text: "off").format()
}
def hsvToRgb(float hue, float saturation, float value) {
int h = (int)(hue * 6);
float f = hue * 6 - h;
float p = value * (1 - saturation);
float q = value * (1 - f * saturation);
float t = value * (1 - (1 - f) * saturation);
switch (h) {
case 0: return rgbToString(value, t, p);
case 1: return rgbToString(q, value, p);
case 2: return rgbToString(p, value, t);
case 3: return rgbToString(p, q, value);
case 4: return rgbToString(t, p, value);
case 5: return rgbToString(value, p, q);
default: return "000000";//throw new RuntimeException("Something went wrong when converting from HSV to RGB. Input was " + hue + ", " + saturation + ", " + value);
}
}
def hsvToShieldRgb(hue,saturation,value){
int h = (int)(hue * 6);
float f = hue * 6 - h;
float p = value * (1 - saturation);
float q = value * (1 - f * saturation);
float t = value * (1 - (1 - f) * saturation);
switch (h) {
case 0: return rgbToShieldRgb(value, t, p);
case 1: return rgbToShieldRgb(q, value, p);
case 2: return rgbToShieldRgb(p, value, t);
case 3: return rgbToShieldRgb(p, q, value);
case 4: return rgbToShieldRgb(t, p, value);
case 5: return rgbToShieldRgb(value, p, q);
default: return "0,0,0";//throw new RuntimeException("Something went wrong when converting from HSV to RGB. Input was " + hue + ", " + saturation + ", " + value);
}
}
def rgbToShieldRgb(r,g,b){
//n/2 = r/255
//n = (r/255) * 2
r = (r/255) * 2;
b = (b/255) * 2;
g = (g/255) * 2;
return "$r,$g,$b";
}
def rgbToString(float r, float g, float b) {
String rs = Integer.toHexString((int)(r * 256));
String gs = Integer.toHexString((int)(g * 256));
String bs = Integer.toHexString((int)(b * 256));
return rs + gs + bs;
}
def setColor(value) {
log.debug "setColor: ${value.hue} and setSaturation: ${value.saturation}"
//zigbee.smartShield(text: "debug: hue:${value.hue},${value.saturation}").format()
def rgb = rgbToShieldRgb(value.red, value.green, value.blue);
//log.debug "setColor: $rgb"
//zigbee.smartShield(text:"debug:${value.red}").format();
zigbee.smartShield(text: "setColor:$rgb").format()
}
@danroot
Copy link
Author

danroot commented Feb 6, 2014

SmartApp for the SmartThings Home Automation System to check the weather every day and display colored lights based on the forecast.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment