NEW: Tuya ZigBee Smart Battery Switch

Sharing for others. Feel free to improve.

https://www.aliexpress.com/i/4001217084809.html

1 Like

The instruction

  1. Add this as a new device handler via the web interface https://graph.api.smartthings.com/

Will only work for some people, since different accounts are on different parts of the cloud.

Instead, use the universal sign in ID:

https://account.smartthings.com

And that will automatically direct people to the correct cloud shard for their account.

Otherwise, people may still be able to sign in, but they will only see a blank device list and anything they add won’t be available for their devices. :disappointed_relieved:

1 Like

I see you are using the “number of buttons“ parameter which is no longer used in the new architecture.

I can’t read all of the code (I depend on text to speech software), but my guess would be this will only work with smartlighting or Webcore. It won’t work correctly with Automations created through the + in the upper right of the new V3 app. The first button will be recognized, but the second or third gang would be ignored.

If I’m wrong on that, could you post a screenshot of an automation created through the + for the second button which is working correctly?

Thanks! There have been a lot of changes in the architecture for multi button devices over the last year or so and it can be very confusing. :thinking:

Hi,

For each button the respective actions appear to be working (although the battery value isn’t correct as the device doesn’t appear to report that).


I hadn’t tried anything from an automation perspective but here goes. See the screen shot below.

On a first attempt I selected the second button to send me a notification when pressed. This does appear to work as expected.

On a separate note, when I select device status from the automation menu smartthings seems to be a bit temperamental on what devices it actually shows here. Sometimes it shows a reduced set. I’m not sure why this is the case however and doesn’t seem to be specific to this device handler.

1 Like

Hi, You may be right here as I have noticed that the primary button isn’t showing up in the automation but the child button is. This suggests to me its something to do with the code in the device manager itself. I’ll need to investigate some more but I’m not sure where to start in relation to your comments on the use of “number of buttons”. Do you have a link to the alternative implementation of this?

1 Like

There hasn’t been any official documentation on this but you can see it in some of the newer DTHs where capabilities button now assumes there is only one button but it has five taps and six taps as options, which you never used to have before.

I’m not going to be able to explain it very well because since I rely on text to speech, I don’t really read code much anymore.

Either @johnconstantelo or @mwav3 might know of a link to some examples.

1 Like

Unfortunately I don’t have an example of something like that, and documentation lacking for sure. Even ST’s Zigbee multi button DTH still uses number of buttons, as do the ones I’ve posted below, which you may have already come across.

Maybe @nayelyz has something available?

Here’s a Zigbee example using Ecosmart’s 4 button device:

Here’s a Zigbee example using the IKEA and SOMFY devices:

Here’s a a Zwave example using the old Aeotec Minimote 4 button device:

https://raw.githubusercontent.com/SmartThingsCommunity/SmartThingsPublic/master/devicetypes/smartthings/aeon-minimote.src/aeon-minimote.groovy

See the capabilities documentation here - SmartThings Developers | Documentation

I’ve been fighting this button issue ever since the new app was released and the classic app was phased out. The way legacy device handlers dealt with buttons on devices was assigning each button as a number, and then the classic app supported “pushed” and “held” values. Custom smartapps could also be written to support additional values, including “released”.

This broke when the new V3 app was released. The app will only recognize one button per device. New standardized supported button values were added, there are 22 possible supported button values that can be assigned to “button 1” now. Button 2 and on will not be recognized at all in the new app unless they are added somehow as child devices.

Child devices have a limit though (can’t remember how many), and I’ve found for most devices it is just easier to assign everything to button 1 but assign each button from the list of the 22 button values listed in the capabilities documentation (pushed_2x, down_4x, down_hold, etc). You can do a “button mapping” list somewhere that maps these values if the names don’t exactly make sense for your device.

The DTH must also send a list of supported button values as a JSON object to the app at some point during initialization, or the app will crash with an error when you try to access a smart app like smartlighting, or the device doesn’t show at all in the automations or the button values don’t show. Since the device handler is written in Groovy you need an import to send the necessary JSON values (I think there’s some other way too without the import but that’s what I’ve done). I’ve included this code in the initialize() method, and also the updated() method. I’ve added a toggle to “force settings update/refresh” to trigger the updated() method by toggling that in preferences. There was a refresh button in the classic app that isn’t in the new app, so this toggle replaces that.

I don’t know why Smartthings did it this way, and it’s made a mess of multibutton devices. My best guess is that the new API based app heavily relies on JSON values to work, and they’re trying to jam a square peg in a round hole to make the new app work with a legacy system that relies on Groovy coding.

Some examples of multi button handlers I’ve updated that work are here - smartthingscode/devicetypes/mwav3 at master · mwav3/smartthingscode · GitHub

Code for import:

import groovy.transform.Field
import groovy.json.JsonOutput

Code for buttonvalues to be sent to include in initialize() and updated() methods:

sendEvent(name: "numberOfButtons", value: 1, displayed: false)
sendEvent(name: "supportedButtonValues", value:JsonOutput.toJson(["up","down","up_hold","down_hold","up_2x","down_2x","up_3x","down_3x"]), displayed:false)
1 Like

OK so what’s confusing me with tis is the fact I seem to have created a main device and a child. They both work as independent devices with, push, held and double however only one (the child) shows up as available in the automations. This seems to be the opposite to what you and JDRobers suggest.

The line of code for the child device:

addChildDevice("Child Button", "${device.deviceNetworkId}:0${i}", device.hubId, [completedSetup: true, label: "${device.displayName[0..-2]}${i}", isComponent: false])

I was assuming at this point it was the way I setup the main device in the code but from what you are saying this isn’t the case.

Can I ask, when you set this up then do you just have one device showing in the app with many possible values representing the various button states?

That’s exactly how I’ve done it. But you’re way with child devices should work too I’ve just never done it that way. I believe you will have to create two separate handlers ( one parent one child) though and both will need to report the supported button values as a JSON object. When the values are reported they show in the device settings in the IDE like the screenshot in this linked post. If those supported button values are missing or not a JSON object you can’t do automations with them. [RELEASE] HomeSeer Dimmer and Switch (HS-WD100+ / HS-WS100+) Device Handlers - #175 by nickha

Thanks for tagging me, @johnconstantelo.
For this cases, it’s important to consider:

  1. The property numberOfButtons is used as a reference only, it doesn’t create the number of buttons specified, it is correct to use child devices for that purpose.

  2. The DTH “Tuya Battery Switch” creates a child using the predefined child button handler and, as @Martin_Rourke said, they are considered separated devices because the isComponent property was set to false. I can see both parent and child devices available from the Automations.


    @Martin_Rourke, from the first time you installed this DTH, you weren’t able to see the parent there?

  3. The content on each view (Dashboard - main, Detail, and Automation) depends on the device presentation, one is generated by default when the device is created, but you can create a custom one where you can remove/add the capabilities you want to appear on each view. This also includes:
    a. Changing the capability values range
    b. The options available from a list (no matter they have a supportedValues attribute or not)
    c. Display order the capabilities cards
    d. Presentation for Multi-Component Devices (when you have child devices as components)

Note: Not all standard capabilities have the Automation View configured.
Up to now, only one standard capability can be shown at the Dashboard View.

  1. @mwav3, the supportedButtonValues needs an extra configuration on the Automations View because not all the devices use this property. This post has an example of this.

Feel free to post further questions.

3 Likes

@Martin_Rourke, from the first time you installed this DTH, you weren’t able to see the parent there?

The 2 devices show up as separate devices in the app however only the child device shows up in the automations device list.

  1. the supportedButtonValues needs an extra configuration on the Automations View because not all the devices use this property. This post has an example of this.

I’m not sure I understand the format of this. where would you add the following as per the example in your link and what attribute is controlling the addition to the automations screen. Where I’m a bit lost is to why the child device is working however the main one is not.

{
    ...
    "automation": {
        "conditions": [
            {
                "component": "button1",
                "capability": "button",
                "version": 1,
                "patch": [
                    {
                        "op": "replace",
                        "path": "/0/list/supportedValues",
                        "value": "supportedButtonValues.value"
                    }
                ]
            }
    ...
}

That’s odd, I can see both correctly and I installed your DTH directly. Please help me verify the following:

  1. Getting the device list, you can find both of them, they should have these values in manufacturerName and presentationID:
 //Parent device
"manufacturerName": "SmartThings"
"presentationId": "SmartThings-smartthings-Tuya_Battery_Switch"
 //Child device
"manufacturerName": "SmartThings"
"presentationId": "SmartThings-smartthings-Child_Button"
  1. In the case of the parent, if it appears only at the Detail and Dashboard View, it might have the Automations View missing. To verify this, you can get the content of the presentation it’s using the /presentation endpoint of the API. Eg. using Postman, the request would be:

Oh, sorry. I verified again and it seems it doesn’t apply in this case, you can omit that.

Took me a while to get Postman working but I can confirm that the presentation view has the same as what you can see above ( removed the battery section as I don’t think its relevant). The child button does appear to be missing the key “double” form the code but I wouldn’t have thought this would make it fail to be in the list for the automation. The supportedValues is also missing form the child button.

Main Button…

      "automation": {
    "conditions": [
        {
            "capability": "button",
            "version": 1,
            "label": "___PO_CODE_CAPABILITY.SMARTTHINGS.BUTTON_DEFAULT",
            "displayType": "list",
            "list": {
                "alternatives": [
                    {
                        "key": "pushed",
                        "value": "___PO_CODE_CAPABILITY.SMARTTHINGS.BUTTON_DEFAULT_ATTRIBUTES_BUTTON_PUSHED",
                        "type": "active"
                    },
                    {
                        "key": "double",
                        "value": "___PO_CODE_CAPABILITY.SMARTTHINGS.BUTTON_DEFAULT_ATTRIBUTES_BUTTON_DOUBLE",
                        "type": "active"
                    },
                    {
                        "key": "held",
                        "value": "___PO_CODE_CAPABILITY.SMARTTHINGS.BUTTON_DEFAULT_ATTRIBUTES_BUTTON_HELD",
                        "type": "active"
                    },
                    {
                        "key": "pushed_2x",
                        "value": "___PO_CODE_CAPABILITY.SMARTTHINGS.BUTTON_DEFAULT_ATTRIBUTES_BUTTON_PUSHED_2X",
                        "type": "inactive"
                    }, code repeats for all 6 conditions/states for each key
                ],
                "supportedValues": "supportedButtonValues.value",
                "value": "button.value"
            },
            "emphasis": true,
            "exclusion": [],
            "component": "main"
        }
    ],
    "actions": []
},

Child Button…

    "automation": {
    "conditions": [
        {
            "capability": "button",
            "version": 1,
            "label": "___PO_CODE_CAPABILITY.SMARTTHINGS.BUTTON_DEFAULT",
            "displayType": "list",
            "list": {
                "alternatives": [
                    {
                        "key": "pushed",
                        "value": "___PO_CODE_CAPABILITY.SMARTTHINGS.BUTTON_DEFAULT_ATTRIBUTES_BUTTON_PUSHED",
                        "type": "active"
                    },
                    {
                        "key": "held",
                        "value": "___PO_CODE_CAPABILITY.SMARTTHINGS.BUTTON_DEFAULT_ATTRIBUTES_BUTTON_HELD",
                        "type": "active"
                    }
                ],
                "value": "button.value"
            },
            "emphasis": true,
            "exclusion": [],
            "component": "main"
        }
    ],
    "actions": []
},

Ok, thanks for the info.
As the child button device is a predefined handler, it also has a fixed device presentation which sets only “held” and “pressed” as supported. That’s why even initializing the supportedButtonValues for this device doesn’t work.
Therefore, I suggest you copy the handler’s code and create another DTH with a different name (so it won’t take the predefined presentation). At the parent handler, you can initialize the supportedButtonValues property, eg.

private void createChildDevices() {
	if (!childDevices) {
		def x = getChildCount()
		for (i in 2..x) {
            def d = addChildDevice("Tuya Child Button", "${device.deviceNetworkId}:0${i}", device.hubId, [completedSetup: true, label: "${device.displayName[0..-2]}${i}", isComponent: false])
            d.sendEvent(name: "supportedButtonValues", value: ["pushed", "held", "double" ].encodeAsJSON(), displayed: false)
		}
	}
}

I made those changes and reinstalled a device using your handler; both devices appear at the Automations tool.

1 Like

I seem to get the following error when using the code you suggested. Line 202 is “def d = addChil…”

physicalgraph.app.exception.UnknownDeviceTypeException: Device type 'Tuya Child Button' in namespace 'smartthings' not found. @line 202 (createChildDevices)

Are you suggesting I need to create another DTH to handle the child button? I’m assuming this is why I get the error above?

What puzzles me however is why this would help both devices show up in the automations section? I understand that the default handler “Button” would only allow the 2 states but I’m not too worried about that for the moment.

Yes, you need to create a new handler in your account (a copy of “child button”).

I haven’t been able to replicate the missing parent, and apparently, there’s no problem with the device presentation.
I believe there could be a caching issue (it’s common on DTHs and it’s limited to each account), this means, your DTH was cached at some point and the new configuration added is not taken into consideration.
What we do in that case is following these steps:

  1. Close the SmartThings mobile app
  2. Create a new DTH (the parent only), same configuration but a different name (so it’s not related to the previous one) and self-publish it.
  3. Assign it to your Zigbee device (devices > edit > type)
  4. Delete the SmartThings app cache (only for android devices, settings > apps > ST app > storage > delete cache)
  5. Reopen the ST app

Note: This is not always effective, so, it’s suggested to reinstall the device and select the new DTH.

1 Like

Just a note to say that the automation part seems to have been solved after a restart f the hub. As you said it must be a cache issue.

Do you know how I can change the icon of the primary switch to match the child button?

image