Skip to content

OpenHAB Rules

arch1v1st edited this page Nov 7, 2016 · 8 revisions

OpenHAB Rules to handle arbitrary Voice Commands

.items:

String ECHO_VoiceCMD "ECHO CMD: [%s]"
String ECHO_Answer "ECHO Answer: [%s]"
Switch ECHO_Processed "ECHO Proc [%s]"

.rules:

rule "Echo VoiceCMD"
when
  Item ECHO_VoiceCMD received update
then
  var String voicecmd = ECHO_VoiceCMD.state.toString.toLowerCase   
  logInfo("Voice.Rec","VoiceCMD received from ECHO "+ voicecmd)

    // Scene
    if (voicecmd.contains("let's party")) { sendCommand(Scene_House, 3) }
    
    // Telephony
    if (voicecmd.contains('call my cell phone')) { 
    	sendHttpGetRequest("http://PHONEIP/cgi-bin/api-make_call?phonenumber=123456789&account=0&password=PASSWORD")
    	sendCommand(ECHO_Answer, 'calling my cell phone')
    }
    if (voicecmd.contains('call my bank')) { 
    	sendHttpGetRequest("http://PHONEIP/cgi-bin/api-make_call?phonenumber=123456789&account=0&password=PASSWORD")
    	sendCommand(ECHO_Answer, 'calling my bank')
    }
    if (voicecmd.contains('call work')) { 
    	sendHttpGetRequest("http://PHONEIP/cgi-bin/api-make_call?phonenumber=123456789&account=0&password=PASSWORD")
    	sendCommand(ECHO_Answer, 'calling my work')
    }
    
    // Misc
    if (voicecmd.contains('good evening')) { 
    	sendCommand(ECHO_Answer, 'And a good evening to you sir')
    }
    if (voicecmd.contains('good morning')) { 
    	sendCommand(ECHO_Answer, 'top of the morning to ya')
    }
       
    // Climate
    if (voicecmd.contains('set temperature')) { 
    	var String targettemp = voicecmd.replaceAll("[^0-9]", "")
    	postUpdate(HVAC_Target_Temp, targettemp)
    	sendCommand(ECHO_Answer, 'setting house temperature to' + targettemp + 'degrees')
    }

    if (voicecmd.contains('the weather like')) { 
	  var String conditions = Weather_Condition.state.toString
	  var String temp = Weather_Temp.state.format("%.0f")
	  var String wind = Weather_Wind.state.format("%.0f")
	  var String feelslike = Weather_Temp_Feel.state.format("%.0f")
	  var String forecast_condition = Weather_Forecast0_CommonId.state.toString
	  var String forecast_temp = Weather_Forecast_Temp_Max0.state.format("%.0f")
	  
	  var String message = 'The weather conditions are ' + conditions + ' with a temperature of ' + temp + ' degrees.  Due to a windchill of ' + wind + ' miles per hour it feels like ' + feelslike + ' degrees.  Tomorrows forcast is expected to be ' + forecast_condition + ' with a high of ' + forecast_temp + 'degrees'
      sendCommand(ECHO_Answer, message)
    }
       
    // Status
    if (voicecmd.contains('status update')) {
    	var String Temp = Avg_Temp.state.format("%.0f")
    	var String Humidity = Avg_Humidity.state.format("%.0f")
    	var String DataCenterPeak = HVAC_DataCenter_Temp_Max.state.format("%.0f")
    	// TODO count security violations and report
    	var String SecurityViolations = 'no'
    	// TODO count infrastructure monitoring alerts
    	//var String Infra = Infra_Alerts.state.format("%.0f")
    	var String BatteryState = transform("MAP","en.map",UPS1_Status.state.toString)
    	var String UPSLoad = UPS1_Load.state.format("%.0f")
    	var String BatteryCharge = UPS1_Battery_Charge.state.format("%.0f")
    	var String BatteryRuntime = UPS1_Battery_Runtime.state.format("%.0f")

    	var String message = "Your home temperature is a comfortable " + Temp + " degrees, with a humidity of " + Humidity + " percent.  The Data Center peaked at " + DataCenterPeak + " degrees today.  There have been " + SecurityViolations +  " security violations or vital infrastructure alerts.  My battery backup is " + BatteryState + " at " + BatteryCharge + " percent capacity.  At current load of " + UPSLoad + " percent I can remain online for approximately " + BatteryRuntime + " minutes in the event of a power outage."
    	sendCommand(ECHO_Answer, message)
    }
            
//    else  { 
//    	sendCommand(ECHO_Answer, 'Sorry, I didnt understand your question')
//    }
    
    // Processed and ready for speech
    sendCommand(ECHO_Processed, ON)
end

Clone this wiki locally