Recreate selector switch with Zigbee2MQTT Auto Discovery

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
User avatar
jjnh
Posts: 33
Joined: Sunday 09 December 2018 14:06
Target OS: Linux
Domoticz version: 2023.1
Location: Netherlands
Contact:

Recreate selector switch with Zigbee2MQTT Auto Discovery

Post by jjnh »

Hi,

One reason why I have not moved my Domoticz tot the latest version is that I would need to move to Zigbee2MQTT Auto Discovery.
The main issue I have with that switch, is that Z2M AD does not create selector switches, but new devices per state of the input device.

In my examplte below I use a Trust ZYCT-202. To Z2M AD it has 6 buttons x 5 functions = 30 devices ... :shock:
This would significantly clog up my already busy device list in Domoticz.

The example below is only for button 5 of my ZYCT-202 remote, but you can easily configure it for all buttons by changing the wildcard on the "on =" section and configure names you like (i.e. "Btn 1", "Btn 2", etc.)

My solution:
1. Make sure all the device states have been detected by the Z2M AD
2. Add the script below into the "Events" in Domoticz
3. Configure hidden devices for all the states ("$" starting on the name)
4. Make sure they alle have a name with the syntax {devicename}_{state}. I used "_" as the separator, but you can use others if you want
5. Make sure you have a dummy selector switch with the same name as the hidden devices, but without the "$"
6. Make sure the selector switch state names match the hidden device state names
7. Configure the device name in "on =" portion in the script below

In my case I have devices from different hardware in my system which I include in the device name.
"(ZB)" is for the Zigbee2MQTT and "(ZBAD)" is for Zigbee to MQTT Auto Discovery.

These are the devices I created:
2024-11-17 14_39_54-Domoticz.png
2024-11-17 14_39_54-Domoticz.png (52.5 KiB) Viewed 719 times

This is the configuration of the selector switch. Make sure the level names match the names of the hidden devices.
2024-11-17 14_46_16-Domoticz.png
2024-11-17 14_46_16-Domoticz.png (36.69 KiB) Viewed 719 times

This is the script I use:

Code: Select all

-- ########################################################
-- #                                                      #
-- #  Version 0.1 of Zigbee Auto Discovery Translator     #
-- #  John Huppertz 17-11-2024                            #
-- #                                                      #
-- #  Find a way to translate separate devices into       #
-- #  selector switches as used by zigbee2mqtt plugin     #
-- #  with minimal device configuration effort            #
-- #                                                      #
-- #  17-11-2024 - Start                                  #
-- #    - Using wildcard "*" to trigger group of          #
-- #      devices                                         #
-- #    - Using the device name syntax {name}_function    #
-- #      the result van be split into parts where _ is   #
-- #      the separator                                   #
-- #                                                      #
-- ########################################################

return {

    on = {
       
        devices = { 
            '$Trust Btn5_*'
        },

    },
   
    logging =    {   
                        level   =   domoticz.LOG_INFO,
                        marker  =   "ZBAD_Translate" 
                    },
               
    execute = function(dz, device )
        
        
        devName = device.name
        devState = device.state

        dz.log("Start triggered by " .. devName, dz.LOG_INFO)
        dz.log("State presented  = " .. devState, dz.LOG_INFO)
        
        -- split the devicename into fields based on seprator character "_"
        local splittedResult = dz.utils.stringSplit(devName,'_')
        
        -- Read the device name and remove the "$" form the device name
        devNameSplit = splittedResult[1]:gsub("[$),]","")
        -- Read the state from the devicename
        devStateSplit = splittedResult[2]
        
        dz.log("Device Name     = " .. devNameSplit, dz.LOG_INFO)
        dz.log("Device State    = " .. devStateSplit, dz.LOG_INFO)
        
        -- Make sure you create a dummydevice that has the correct name and that states match (case sensitive)
        dz.devices(devNameSplit .. " (ZBAD)").switchSelector(devStateSplit)
        
    end

}
Adding additional devices is just a matter of renaming the found devices, creating a dummy selector switch with the correct name and states, and adding a trigger in the script.
You can reconfigure the device wild card in the "on =" section to suite your needs. You could use a wild card that matches all devices detected by Z2M AD.
I hope this helps other who are dealing with the same challenge ;)

Cheers,
John
Odroid N2+ 4GB, with an RfxTrx433E, Zigbee with Slaesh's CC2652RB, Z-Wave, BroadLink IR using RM mini 3 and P1 connected.
User avatar
waltervl
Posts: 5148
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Recreate selector switch with Zigbee2MQTT Auto Discovery

Post by waltervl »

Does the config topic of a MQTT AD device that should be a Selectorswitch device have all the values in the config message? If that is available then it should be possible to create a multiselector switch by MQTT AD.

But when Zigbee2MQTT or Zwave-JS-UI sends a config topic for each device it will be hard to combine those. The Zigbee2MQTT python plugin you are using has specific brand/device adapter configuration scripts to do this. So this needs a lot of maintenance.
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
User avatar
jjnh
Posts: 33
Joined: Sunday 09 December 2018 14:06
Target OS: Linux
Domoticz version: 2023.1
Location: Netherlands
Contact:

Re: Recreate selector switch with Zigbee2MQTT Auto Discovery

Post by jjnh »

Hi Waltervl,

I don't understand what you mean with "the config topic of a MQTT AD device"?
Can you elaborate?
Odroid N2+ 4GB, with an RfxTrx433E, Zigbee with Slaesh's CC2652RB, Z-Wave, BroadLink IR using RM mini 3 and P1 connected.
User avatar
waltervl
Posts: 5148
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: Recreate selector switch with Zigbee2MQTT Auto Discovery

Post by waltervl »

The MQTT config topic is the messages that Domoticz uses to create a device. See also wiki for some background. https://wiki.domoticz.com/MQTT_AD_Report_Problems

So you can look into the config topic in MQTT (default in homeassistant\switch) to see if the switch levels are defined. Probably these config topics does not contain the switch levels. That is why the plugin uses those configuration scripts.
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest