[PORT] - Send Events to EventGhost

Hi this seems to be working good with my limited testing. Still some changes need to be made, but I thought I would share.

/**
 *	Send Events to EventGhost
 *	
 *	Send SmartThings events to EventGhost
 *	
 *	https://github.com/aderusha/SmartThings/blob/master/Send-Events-to-EventGhost.groovy
 *	Copyright 2015 aderusha
 *	Version 1.0.0 - 2015-09-13 - Initial release
 *	Version 1.1.0 - 2015-09-15 - Changed handling of binary(ish) vs non-binary values to allow sending
 *	                             value data to EG to be handled via Python and eg.event.payload[]
 *	Version 1.2.0 - 2016-04-18 - Added support for individual button values
 *	
 *	This SmartApp will send selected events to an EventGhost server running the Webserver plugin.
 *	EventGhost is a Windows application used for event automation, find out more here: http://www.eventghost.org/
 *	How to setup the EventGhost Webserver plugin: http://www.eventghost.org/mediawiki/index.php?title=Webserver
 *	
 *	TODO:
 *	- Currently doesn't support user authentication or SSL.  EG Webserver authentication must be disabled by leaving
 *	  the username/password field in the plugin configuration blank
 *	- Figure out how to monitor Sonos "musicPlayer" events
 *	- Add additional capabilities to monitor
 *	
 *	ISSUES:
 *	- "Color" values are not being received by EG, presumably due to the "#" character being mishandled somehow
 *	
 *	Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 *	in compliance with the License. You may obtain a copy of the License at:
 *	
 *	    http://www.apache.org/licenses/LICENSE-2.0
 *	
 *	Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
 *	on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
 *	for the specific language governing permissions and limitations under the License.
 *	
 */

// button handling is kinda working but the handler is disabled.  getting different responses from minimote vs scene 
// controller, should sort that out before tearing down the current minimote configs

definition(
    name: "Send Events to EventGhost",
    namespace: "aderusha",
    author: "aderusha",
    description: "Send SmartThings events to EventGhost",
    category: "Convenience",
	iconUrl: "https://s3.amazonaws.com/aderusha/SmartThings/EventGhost_logo.png",
	iconX2Url: "https://s3.amazonaws.com/aderusha/SmartThings/EventGhost_logo@2x.png"
)

preferences {
	section("EventGhost server address and port"){
		input "egServer", "text", title: "Server", description: "EventGhost Web Server IP", required: true
		input "egPort", "number", title: "Port", description: "EventGhost Web Server Port", required: true, defaultValue: 80
	}
	section("EventGhost Command prefix"){
		input "egPrefix", "text", title: "Command prefix", required: false, defaultValue: "ST"
	}
	section("Select events to be sent to EventGhost"){
		input "mySwitch", "capability.switch", title: "Switches", required: false, multiple: true
		input "myDimmer", "capability.switchLevel", title: "Dimmers", required: false, multiple: true
		input "myColorControl", "capability.colorControl", title: "Color Controls", required: false, multiple: true
		input "myButton", "capability.button", title: "Buttons", required: false, multiple: true
		input "myMomentaryContact", "capability.momentary", title: "Momentary Contacts", required: false, multiple: true
		input "myMotion", "capability.motionSensor", title: "Motion Sensors", required: false, multiple: true
		input "myContact", "capability.contactSensor", title: "Contact Sensors", required: false, multiple: true
		input "myLock", "capability.lock", title: "Locks", required: false, multiple: true
		input "myThermostat", "capability.thermostat", title: "Thermostats", required: false, multiple: true
		input "myTemperature", "capability.temperatureMeasurement", title: "Temperature Sensors", required: false, multiple: true
		input "myBrightness", "capability.illuminanceMeasurement", title: "Light Sensors", required: false, multiple: true
		input "myHumidty", "capability.relativeHumidityMeasurement", title: "Humidty Sensors", required: false, multiple: true
		input "myEnergy", "capability.energyMeter", title: "Energy Sensors", required: false, multiple: true
		input "myPower", "capability.powerMeter", title: "Power Sensors", required: false, multiple: true
		input "myAcceleration", "capability.accelerationSensor", title: "Acceleration Sensors", required: false, multiple: true
		input "myPresence", "capability.presenceSensor", title: "Presence Sensors", required: false, multiple: true
		input "mySmoke", "capability.smokeDetector", title: "Smoke Sensors", required: false, multiple: true
		input "myWater", "capability.waterSensor", title: "Water Sensors", required: false, multiple: true
		input "myCO", "capability.carbonMonoxideDetector", title: "Carbon Monoxide Detectors", required: false, multiple: true
	}
}

def installed() {
	log.debug "Installed with settings: ${settings}"
	subscribeToEvents()
}

def updated() {
	log.debug "Updated with settings: ${settings}"
	unsubscribe()
	subscribeToEvents()
}

def subscribeToEvents() {
	subscribe(mySwitch, "switch", eventHandlerBinary)
	subscribe(myDimmer, "level", eventHandlerValue)
	subscribe(myColorControl, "color", eventHandlerValue)
	subscribe(myButton, "button", eventHandlerButton)
	subscribe(myMomentaryContact, "momentary", eventHandlerBinary)
	subscribe(myMotion, "motion", eventHandlerBinary)
	subscribe(myContact, "contact", eventHandlerBinary)
	subscribe(myLock, "lock", eventHandlerBinary)
	subscribe(myThermostat, "thermostat.thermostatMode", eventHandlerBinary)
	subscribe(myThermostat, "thermostat.thermostatFanMode", eventHandlerBinary)
	subscribe(myThermostat, "thermostat.thermostatOperatingState", eventHandlerBinary)
	subscribe(myThermostat, "thermostat.temperature", eventHandlerValue)
	subscribe(myThermostat, "thermostat.heatingSetpoint", eventHandlerValue)
	subscribe(myThermostat, "thermostat.coolingSetpoint", eventHandlerValue)
	subscribe(myThermostat, "thermostat.thermostatSetpoint", eventHandlerValue)
	subscribe(myTemperature, "temperature", eventHandlerValue)
	subscribe(myBrightness, "illuminance", eventHandlerValue)
	subscribe(myHumidty, "humidity", eventHandlerValue)
	subscribe(myEnergy, "energy", eventHandlerValue)
	subscribe(myPower, "power", eventHandlerValue)
	subscribe(myAcceleration, "acceleration", eventHandlerBinary)
	subscribe(myPresence, "presence", eventHandlerBinary)
	subscribe(mySmoke, "smoke", eventHandlerBinary)
	subscribe(myWater, "water", eventHandlerBinary)
	subscribe(myCO, "carbonMonoxide", eventHandlerBinary)
}

def eventHandlerBinary(evt) {
	def egHost = "${settings.egServer}:${settings.egPort}"
	def egRawCommand = "${settings.egPrefix}.${evt.displayName}.${evt.name}.${evt.value}"
	def egRestCommand = java.net.URLEncoder.encode(egRawCommand)
	log.debug "processed binary event ${evt.name} from device ${evt.displayName} with value ${evt.value} and data ${evt.data}"
	log.debug "egRestCommand:  $egRestCommand"
	sendHubCommand(new hubitat.device.HubAction("""GET /?$egRestCommand HTTP/1.1\r\nHOST: $egHost\r\n\r\n""", hubitat.device.Protocol.LAN))
}

def eventHandlerValue(evt) {
	def egHost = "${settings.egServer}:${settings.egPort}"
	def egRawCommand = "${settings.egPrefix}.${evt.displayName}.${evt.name}"
	def egRestCommand = java.net.URLEncoder.encode(egRawCommand)
	def egRestValue = java.net.URLEncoder.encode("${evt.value}")
	def egRestCommandValue = "$egRestCommand&$egRestValue"
	log.debug "processed data event ${evt.name} from device ${evt.displayName} with value ${evt.value} and data ${evt.data}"
	log.debug "egRestCommand:  $egRestCommandValue"
	sendHubCommand(new hubitat.device.HubAction("""GET /?$egRestCommandValue HTTP/1.1\r\nHOST: $egHost\r\n\r\n""", hubitat.device.Protocol.LAN))
}

def eventHandlerButton(evt) {
	def buttonNumber = evt.jsonData.buttonNumber
	def egHost = "${settings.egServer}:${settings.egPort}"
	def egRawCommand = "${settings.egPrefix}.${evt.displayName}.${evt.name}.$buttonNumber.${evt.value}"
	def egRestCommand = java.net.URLEncoder.encode(egRawCommand)
	log.debug "processed button event ${evt.name} from device ${evt.displayName} with value ${evt.value} and button $buttonNumber"
	log.debug "egRestCommand:  $egRestCommand"
	sendHubCommand(new hubitat.device.HubAction("""GET /?$egRestCommand HTTP/1.1\r\nHOST: $egHost\r\n\r\n""", physicalgraph.device.Protocol.LAN))
}
8 Likes

You're awesome. Code works great on my hub! Using Eventghost opens up the hub to a lot more automation possibilities.

For example, I use mine to get input from a couple of zigbee buttons mounted on a wall, to start a youtube playlist, turn my TV and receiver on and set a certain volume. Thanks again!

I was just testing this app and it does seem to work, was able to send a sleep command to a PC successfully. But in the event logs I see the following warning, wondering if its anything I should worry about.

Premature end of Content-Length delimited message body (expected: 870; received: 0

For those who know EventGhost and how it integrates with Hubitat....

Can I trigger my Windows PC upon some Hubitat event to send a text and/or email message with the most recently created picture or clip in a designated folder as an attachment?

Trying to understand what's possible before I dive headlong. TIA....

can anyone help me figure out how to identify the port and Web server IP for adding the app? I guess I haven't crossed this bridge yet. Searched THOROUGHLY for help - not much out there on EventGhost/Hubitat. If I need to do some router config, I could use some help on that too.

I was able to get TRIGGERcmd to work. I'd like to try both and report back.

Once you have eventGhost installed, you would need to add the webserver plugin. Once setup it will use your pc's ip and default to port 80.

1 Like

finally got it to work. thanks

I believe you would be able to do that. You'd need to mess around a bit with EventGhost

Can anyone think of a way to have EventGhost run macros as parts of existing rules? For example, If I have a rule to shut some lights off and I'd like to use it to shut down some applications on my PC via EventGhost as well. I tried adding some code to send global variable changes to event ghost but I couldn't get it to work. I'm a weak programmer and pretty unfamiliar with Hubitat dev documentation.

There are quite a few ways to accomplish this. Since you already have this working with this app here's an option...assuming you just want to activate this EventGhost macro as easily as possible

  • Create a virtual switch and set the AutoOff preference to 500ms

  • In this app, select the virtual switch and set a command prefix. I chose "hubitat" for simplicity.

  • While eventghost is running toggle the switch. You should see the events in the log stream
    image

  • Now you can drag that event from the log and into the Macro on the right
    image

  • Turn on the virtual switch to test.

  • if it works as expected, all you need to do is turn on this switch in any automation/rule you need.

1 Like

eyyy. that's clever. Thanks. I'll try it in a few

works beautifully

1 Like

That's what I was afraid of :slight_smile:

I feel you :smile:

This is a pretty powerful tool. Do a lot of folks use it? I don't see it mentioned too much. Has anyone found a way to secure the communication?

1 Like

I just found this and plan to use it as soon as I get a little free time.

As for securing it. I plan to use Windows Firewall to ONLY allow the Hubitat IP address to talk to it.

Finally had the chance to dive into this. I keep getting a "connect timed out" when I flip the virtual switch in the EventGhost app and nothing shows up in the EventGhost log in Windows. I've tripled check the IP address; it's correct.

Any ideas?

I've not used this in sometime. At last check it worked well. I'll set it up when I get home and see what there is to see.

1 Like

Ok i Just installed the app on my hub configured and my eventghost client started responding right away (it was just sitting there waiting for something to do apparently lol).

I believe you have to configure the eventghost webserver to activate and be listening before this will work. I followed these instructions years ago.

Appreciate the pointer, but I can't even get past step one. I go to File/Options in EG and get an error. Even reinstalled the software and still get an error. I've poked around the internet for pointers, but I'm out of ideas. Not a python guy, so...

EDIT: Found and resolved the known error with File/Options, but still struggling to interpret these instructions since I'm not using IFTTT or ST. Should the HE app, on its own, deliver the Hubitat device event to EG? I'm thinking I should see the HE event in the EG log. But I don't. The log shows everything I'm doing on the PC, but no events from Hubitat. Wondering if I need to use Maker API or the like. I'm clearly missing something.