Brightswitch motion sensors

Okay so the motion sensors, light sensors, temp sensors are all available to be used in SmartThings but you have to do several steps in order to get them setup properly. I don’t know if this is available on anything else or if the code here will help anyone. I was able to make an app that let me get motion notifications and take a picture on my phone, but I couldn’t get it working beyond my local area network so it wasn’t all that great.

Anyway for smartthings users, first login to the https://account.smartthings.com(edited) and paste some code in order for SmartThings to be able to use Brightswitch sensors over WiFi.

**Note Brightswitches and smartthings must be on the same Local Area Network in order for this to work. Also each device both Brightswitch and Smartthings needs to have the IP address as static so reserve the addresses of brights and ST to keep this functional. Took me a long time to figure that out it would work great for 3-4 days then nothing, turned out the hub would change ip addresses.
** Note enable Web Server under developer in Brightswitch. Go to the gear bottom right corner, then connections, then developer, enable web server. There is info there such as IPAddress and AuthToken that will be needed later so write it down, take a picture or come back to it later.

In the graph.api smartthings website select My locations (this is to make sure you are on the right hub (Samsung designed it weird), select your location (mine is just called Home), then select My Device handlers from the top. Click on +Create New Device Handler. Select From Code.
Copy and paste the Device Handler Copy the text below this line until the last } do not accidentally copy both sets of code there is a device handler code and a smart app code.

/**
 *  Bright Motion Sensor
 *
 *  Copyright 2018 Attila Szasz
 *
 *  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.
 *
 */
metadata {
	definition (name: "Bright Motion Sensor", namespace: "com.brightswitch", author: "Attila Szasz") {
		capability "Motion Sensor"
	}

	simulator {
		status "active": "bright motion"
		status "inactive": "bright no motion"
	}

	tiles(scale: 2) {
		multiAttributeTile(name:"motion", type: "generic", width: 6, height: 4){
			tileAttribute("device.motion", key: "PRIMARY_CONTROL") {
				attributeState("active", label:'motion', icon:"st.motion.motion.active", backgroundColor:"#00A0DC")
				attributeState("inactive", label:'no motion', icon:"st.motion.motion.inactive", backgroundColor:"#CCCCCC")
			}
 		}
		main "motion"
		details "motion"
	}
}

def parse(String description) {

}

// Parse incoming device messages to generate events
def parse(Map params) {
	log.debug "Parsing '${params}'"
	if(params["value"] == "active") {
    	movement()
    } else {
    	noMovement()
    }
}

def movement() {
	createEvent(
		name: "motion",
		value: "active"
	)
}

def noMovement() {
	createEvent(
		name: "motion",
		value: "inactive"
	)
}

Select Create the page should not error Save and Publish for Self

Next click on the top bar My SmartApps then +New SmartApp then click on From code then…
copy the Smart App code below

/**
 *  Bright Motion Sensor Manager
 *
 *  Copyright 2018 Attila Szasz
 *
 *  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.
 *
 */
definition(
    name: "Bright Motion Sensor",
    namespace: "com.brightswitch",
    author: "Attila Szasz",
    description: "Exarlabs Bright Motion Sensor Demo",
    category: "My Apps",
    iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
    iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png",
    iconX3Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png")

preferences {
	section("Brightswitch setup") {
        input "bsIpAdress", "text", title: "Ip Address"
        input "bsAuthToken", "text", title: "Auth Token"
    }
}

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

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

def initialize() {
    def ipHex = convertIPtoHex(bsIpAdress)
    def deviceNetworkId = "$ipHex"
    def nId = state.deviceNetworkId
    log.debug "ms: ${nId}"
    if (nId == null) {
    	def motionSensor = getChildDevice(nId)
        state.deviceNetworkId = deviceNetworkId
        addChildDevice("com.brightswitch", "Bright Motion Sensor", deviceNetworkId, null, [label:"${app.label}", name:"Bright Motion", completedSetup: true])
    }
    sendLocalServerCommand()
}

// Send local command for hub to make http request on LAN to get BS data
private sendLocalServerCommand() {
	log.debug "Http request sent"
    sendHubCommand(new physicalgraph.device.HubAction("""GET /getdata?authtoken=$bsAuthToken HTTP/1.1\r\nHOST: $bsIpAdress:3003\r\n\r\n""", physicalgraph.device.Protocol.LAN, "", [callback: parse]))
    // Timer - request brightswitch state in every 5 second
    runIn(3 ,"sendLocalServerCommand")
}

// Parse the response from BS and turn on lamp
def parse(physicalgraph.device.HubResponse hubResponse) {	
    def response =  hubResponse.json
    def mov = response.get("movement");
    log.debug "motion: ${mov}"
    def motionSensor = getChildDevice(state.deviceNetworkId)
    if(mov){
        sendEvent(motionSensor, ["value": "active"])
    } else {
    	sendEvent(motionSensor, ["value": "inactive"])
    }
 }
 
private String convertIPtoHex(ipAddress) {
    String hex = ipAddress.tokenize( '.' ).collect {  String.format( '%02X', it.toInteger() ) }.join()
    return hex
}

Select create, save, publish for self.

Now, I use the classic smartthings app so I only have instructions for the classic app. In the smartthings app select automation, then select SmartApps, then scroll down (if you have to) to the + Add a SmartApp, scroll down again to My Apps. Find the one called Bright Motion Sensor and select it, then input the IP Address and Auth token from the brightwitch developer/web server page. Assign a name (this assigns the name for the smartapp not the sensor), set for specific modes then save. Now go to My Home and you should see a new device listed as Bright Motion Sensor. Select Bright Motion Sensor tap the gear and change the name of the motion sensor to whatever you want then save it.

This is not my code (see copyright) but I am allowed to share it. I do have code that gets temperature and light sensor as well as Brightswitch name and version # if anyone wants that code I can dig it up and share it. It is mostly just a modified version of the above code.

Hope this helps someone!

https://account.smartthings.com is the best link to use for IDE since it automatically redirects you to the appropriate SHARD

2 Likes

Thanks I updated it.