[OBSOLETE] Xiaomi Sensors and Button (beta)

You have lost me?

I know what a Door sensor is/does, I have over 30 of them.

Do you mean a ‘status’ override button on the ‘Thing’ (Sensor) in the app itself?

If your sensor drops connection to the ST Hub the ONLY way to restore this is to physically press the little reset button on the sensor itself. There is NO way you can do this via the app as if there is NO connection then the app/Hub has no way to talk to the sensor. Sometimes overriding the status can bring it back to life but only if you catch it before it completely drops off from the Hub.

2 Likes

With the Xiaomi Wireless Switch, do we only have two actions? Single push and held?
I just want to confirm this. I am wondering if it has double or triple click actions.

Also, I am using Button Controller app. I also noticed you can use Core. Are there any other apps specifically for this device?

@ArstenA I have been using your updated DH:s for a while (great work!), but haven’t changed for my Xiaomi Buttons until today - the old one was working fine.

However, I noticed that your DH dosen’t seem to support long press/hold? The option for time is still in settings, but has no effect. I looked in the code, and the variable is never used.

Is that intentional or something that’s WIP? I use long press for pretty much every button, so I’m back to the old a4refillpad DH for now. As I said it’s working fine, so I’m mostly curious. :slight_smile:

1 Like

This DH worked for me. a4refillpad’s DH worked for me as well, but it kept on disconnecting devices from the hub. Hopefully it they will stay connected.

Two weird issues:

If I use the Button Controller app, it does nothing. Even though I see the click, my action (e.g. turn on lamp) does not get fired.

If I use Core, it works fine.

The other questions is with regard to held action. As mentioned above, does it work in this DH?

Thanks.

The original button is not supporting held. I will try to get it reintegrated. The aqara will probably not be able to support it, it does not give me a released event.

1 Like

@Emil_F

9d400d5e-fd81-4ada-9fcb-32e3cac87bb8 21:09:35: error java.lang.NullPointerException
9d400d5e-fd81-4ada-9fcb-32e3cac87bb8 21:09:35: debug Xiaomi Aqara Temp Sensor Parse returned: Xiaomi Aqara Temp Sensor pressure is 9845.0

I haven’t seen any answer here, so here is mine:

You have an issue with the event logging, as it uses the following code for it, actually with the definition of ‘now’

def now = new Date().format("yyyy MMM dd EEE h:mm:ss a", location.timeZone)
sendEvent(name: "lastCheckin", value: now)

This is the point, where you receive the NullPointerExeception error. I’ve had the same issue. Your location hasn’t been set up correctly, so you don’t have timeZone for it. You may see similar issues with SmartApps which use Sunrise/Sunset. You can either try to set your location again, and verify after, that the DH is working or just change the code. (You can replace the UTC with your local timezone.)

def now = new Date().format("yyyy MMM dd EEE h:mm:ss a",  TimeZone.getTimeZone("UTC"))

I hope you will get it work.

Thanks.
It was indeed the location but because i opened the ST account on the phone when i logged in on tablet did not want to update the location in any way, so i have to log in on the mobile phone again and worked.

I think the code could be changed to include a failsafe of using the UTC timezone if the user hasn’t set up their hub’s timezone, like this:

def now = new Date().format("yyyy MMM dd EEE h:mm:ss a", ${location.timeZone != null ? location.timeZone : TimeZone.getTimeZone("UTC"))}

@ArstenA - what do you think?

I don’t have either the original Xiaomi button or the Aqara button to test with, but looking at the DH code, the original Xiaomi button provides on / off data, so it’s a momentary switch. This means that code could be incorporated to add a held state.

There’s a ZigBee button DH on the SmartThingsPublic repository that has code that adds a held state by comparing the current time to the time of the last on state, as long as the off state hasn’t been read by X seconds (X being set in the preferences). I think this code could be adapted to the original Xiaomi button DH.

As for the Aqara button, based on this page listing of cluster data on the ZiGate website, the Aqara button is also capable of on / off data, as well as multi-clicks up to 4 clicks (in cluster 0x0006, attribute 0x8000). If that’s true, then the held state code could be used for this button’s DH as well. And maybe multi-click capability as well, though that gets more complicated, something similar to how the Xiaomi Cube controller DH works.

The original button supports held using the original a4refillpad DH. I have 2 working well for over a year now.

I know, I just have not had a chance to work on this. It should be next on the list unless some other major bug comes up.

I put this into all the DHs and then realized it broke everything…

I reverted this change.

It seems like you cannot test location.timezone it gives an error.

Oh wow, I didn’t expect that - very sorry. I didn’t have a chance to test that suggested code when I posted. I promise in future if I suggest any code changes to explain if I have had a chance to test it or not.

I will experiment to see if there’s a way to avoid the timezone error. It’s a bit tricky to test, because I don’t think I can force my hub to “forget” the timezone I’ve already set, though.

Just set up my temp/humidity sensor and noticed a small bug below 32f.

I’m assuming since the device reports in C but we convert to F, a negative C value throws off the calculation. But I can’t seem to figure out how to fix it.

Existing Code:

private String parseValue(String description) {

if (description?.startsWith("temperature: ")) {
	def value = ((description - "temperature: ").trim()) as Float 
    
    if (getTemperatureScale() == "C") {
    	if (tempOffset) {
			return (Math.round(value * 10))/ 10 + tempOffset as Float
        } else {
			return (Math.round(value * 10))/ 10 as Float
		}            	
	} else {
    	if (tempOffset) {
			return (Math.round(value * 90/5))/10 + 32 + offset as Float
        } else {
			return (Math.round(value * 90/5))/10 + 32 as Float
		}            	
	}

I searched around and it seems there is a celsiusToFahrenheit() function available that might help. I found some custom ZigBee DTH code that @JohnR wrote and posted about on the Parallax Inc forum in 2015.

Here’s the relevant code that uses the function:

Example code (click triangle to view)
private Map parseCatchAllMessage(String description) {
    Map resultMap = [:]
    def cluster = zigbee.parse(description)

    switch(cluster.clusterId) {
        case 0x0402:
        // temp is last 2 data values. reverse to swap endian
        String temp = cluster.data[-2..-1].reverse().collect { cluster.hex1(it) }.join()
        def value = getFahrenheit(temp)
        resultMap = getTemperatureResult(value)
        break

        case 0x0406:
        log.debug 'motion'
        resultMap.name = 'motion'
        break
    }

    return resultMap
}

def getFahrenheit(value) {
	def celsius = Integer.parseInt(value, 16)
	return celsiusToFahrenheit(celsius) as Integer
}

private Map getTemperatureResult(value) {
	def linkText = getLinkText(device)
	if (tempOffset) {
		def offset = tempOffset as int
		def v = value as int
		value = v + offset
	}
	def descriptionText = "${linkText} was ${value}°${temperatureScale}"
	return [
		name: 'temperature',
		value: value,
		descriptionText: descriptionText
	]
}

So if celsiusToFahrenheit() is available in the Groovy library, and it can handle Float data type, we should be able to insert it into the Temp / Humidity sensor’s code like this:

private String parseValue(String description) {

  if (description?.startsWith("temperature: ")) {
	def value = ((description - "temperature: ").trim()) as Float 
    
    if (getTemperatureScale() == "C") {
    	def tempValue = tempOffset ? value + tempOffset : value
    } else {
		def fahrenheit = celsiusToFahrenheit(value) as Float
		def tempValue = tempOffset ? fahrenheit + tempOffset : fahrenheit
	}
return (Math.round(tempValue * 10)/10 as Float
}

Please note I can’t test this code because I don’t have any Temp / Humidity sensors (yet.) Feel free to beta test it though!

If it works, then we can suggest a change to @ArstenA

Have you found the solution to keep them connected? Do you use a Hub or Samsung connect/Shield Link? :D. I am using a SS connect and all my xiaomi sensors drop connection after 1-2 hours.

Well, I found out these don’t work with hub v 1,which is what I am using. So I guess they will never work since they don’t have (will never have) the firmware upgrade.

So I messed with this code for about an hour and finally got it to work. One unintented side effect was I now had 2-3 decimal points of precision. The bad news, still the same effect. Instead of reading 31f, it jumps to 212f.

So I decided to go back to the original and set my app to use C instead. Once it got down to 0, and went negative it jumped up to 100c. I’m suspecting 104 is actually -4, 108 is actually -8.

@mieomeo No issues with connectivity here. I’m using a v2 ST hub if that makes any difference. I know I’ve heard some folks having issues with v1 hubs. Also some other devices cause zigbee instability I’ve read.

-----------EDIT--------------------------
Did a little more googling on how to handle 101 being -1 and found @ArstenA already figured it out and fixed it. Playing with his device handler now.

So as far as I can see, only V2 Hub with higher firmware than Samsung connect, Shield Link anh V1 Hub can work with those sensors without disconnection :slight_smile:, need more confirmation :blush:

I think it’s hub isssue then, since v2 hub has the much newer version firmware than others