Configurable buttons/switches: another good use of scripts

I have a number of Buttons around my house to trigger functions. But which functions at which locations?
That’s so difficult to plan apart from the obvious “turn the local light on”. The scene management provided me the idea how to simplify this.

I use the following keys/buttons, depending on location:

  • If the key is located near wall switches i use the homematic ones: HM-PB-2-WM55.

  • For buttons located e.g. under the table or somewhere else i use the Xiaomi Aqara switches.

Both switches expose 4 functions, the Xiaomi single to quadruple clicks, the Homematic up, down, long up, long down.

The idea is, to add a virtual string item which holds the name of the script to trigger for each function of the button/switch. The string item is configurable with a selection item in a sitemap.

It is important to persist the string items with restore on startup!
Otherwise you’ll have to start over on every restart.

You need to name the string items exactly as your button/switch items, but add an additional _script or _single, _double, _triple, _quadruple for Xiaomi. (I tried the Homematic PRESS-channel, to keep the strategy similar, but this didn’t work as expected.)

Xiaomi flavor:

String WZKEY1_CLICK_single "WZ 1x [%s]" <wallswitch> (gRestoreOnStartup)
String WZKEY1_CLICK_double "WZ 2x [%s]" <wallswitch> (gRestoreOnStartup)
String WZKEY1_CLICK_triple "WZ 3x [%s]" <wallswitch> (gRestoreOnStartup)
String WZKEY1_CLICK_quadruple "WZ 4x [%s]" <wallswitch> (gRestoreOnStartup)

Homematic flavor:

String WZKEY2_SHORT1_script "WZKEY2-1 SHORT [%s]" <wallswitch> (gRestoreOnStartup)
String WZKEY2_LONG_RELEASE1_script "WZKEY2-1 Longrelease [%s]" <wallswitch> (gRestoreOnStartup)
String WZKEY2_SHORT2_script "WZKEY2-2 SHORT [%s]" <wallswitch> (gRestoreOnStartup)
String WZKEY2_LONG_RELEASE2_script "WZKEY2-2 Longrelease [%s]" <wallswitch> (gRestoreOnStartup)

The rules

rule "Xiaomi keys"
when 
    Member of gXiKey received command
then
    if (receivedCommand.toString == "null") 
    {
        return
    }
    val script = ScriptServiceUtil.getItemRegistry.getItem(triggeringItem.name + "_" + receivedCommand.toString)

    logInfo(logger, "command: " + script.state.toString + ".script")
    callScript(script.state.toString + ".script")
   
end    

rule "Homematic keys"
when 
    Member of gHMKeys changed from OFF to ON
then
    logInfo(logger, "command: " +  triggeringItem.name)

    val script = ScriptServiceUtil.getItemRegistry.getItem(triggeringItem.name + "_script")    

    logInfo(logger, "command: " + script.state.toString + ".script")
    callScript(script.state.toString + ".script")
   
end    

The sitemap for configuration

sitemap test label="test" 
{				
    Frame label="WZ1" 
    {
        Selection item=WZKEY1_CLICK_single mappings=[""="-","<scriptname_1>"="scriptlabel_1","<scriptname_n>"="scriptlabel_n"]
        Selection item=WZKEY1_CLICK_double mappings=[""="-","<scriptname_1>"="scriptlabel_1","<scriptname_n>"="scriptlabel_n"]
        Selection item=WZKEY1_CLICK_triple mappings=[""="-","<scriptname_1>"="scriptlabel_1","<scriptname_n>"="scriptlabel_n"]
        Selection item=WZKEY1_CLICK_quadruple mappings=[""="-","<scriptname_1>"="scriptlabel_1","<scriptname_n>"="scriptlabel_n"]
    }	
    Frame label="WZ2" 
    {					
        Selection item=WZKEY2_SHORT1_script mappings=[""="-","<scriptname_1>"="scriptlabel_1","<scriptname_n>"="scriptlabel_n"]
        Selection item=WZKEY2_LONG_RELEASE1_script mappings=[""="-","<scriptname_1>"="scriptlabel_1","<scriptname_n>"="scriptlabel_n"]
        Selection item=WZKEY2_SHORT2_script mappings=[""="-","<scriptname_1>"="scriptlabel_1","<scriptname_n>"="scriptlabel_n"]
        Selection item=WZKEY2_LONG_RELEASE2_script mappings=[""="-","<scriptname_1>"="scriptlabel_1","<scriptname_n>"="scriptlabel_n"]
    }		
}

1 Like