Best way to write repeating stuffs

I have few lights: milight, hue, wifi lamp, nanoleaf and I like to control them with different buttons. But I have rule for every button and if I change something I have to change it on every button rule - if long press go all to night mode, if one pres turn on etc.

What is the best way to do it? Have similar command on one place like function “turn all light in room into nightmode” (each light do it deferent way) and this call with different buttons, so If i need change something, I just do it on one place…

Thnx

Thnx a lot to pointing me to this. Very interesting. So do I understand correctly, that ProxyItem can help me with this?

Proxy switch ProxyNightmode in items and extra rule when received command do everything to switch every light into mightmode.
Than in buttons rules just use ProxyNightmode ON and OFF?

Why don’t you post a couple of your items and rules and I’ll show you what to do to simplify your code

Oh, OK. For example:

milights.item,nanoleaf.item, mii.item with switch and dimmers item

and all rules in xiaomi.rules as all are connected with switch or sensors xiaomi

two switch rules example:

rule "Xiaomi Switch button 1"

when
    Channel "mihome:sensor_switch:1:2:button" triggered
then
    var actionName = receivedEvent.getEvent()
        
    switch(actionName) {
        case "SHORT_PRESSED": {
             gLights_TRoom_all_switch.sendCommand(OFF)
             gLights_TRoom_all_night_switch.postUpdate(OFF)
             gLights_TRoom_table_switch.postUpdate(OFF)
             
             PZBL_power.sendCommand(OFF)
             
             Gateway_LightSwitch.sendCommand(OFF)             
             Thread::sleep(1000)
             TRoomNanoleafPower.sendCommand(OFF)  
        }
        case "DOUBLE_PRESSED": {                     
             Bulb_TRoom_01_sw.sendCommand(ON)
             TRoom_GDO_Bulb_01_dm.sendCommand(100)
             Bulb_TRoom_02_sw.sendCommand(ON)
             TRoom_GDO_Bulb_02_dm.sendCommand(100)
             Bulb_TRoom_03_sw.sendCommand(ON)
             TRoom_GDO_Bulb_03_dm.sendCommand(100)
             Bulb_TRoom_04_sw.sendCommand(ON)
             Bulb_TRoom_05_sw.sendCommand(ON)
             Bulb_TRoom_06_sw.sendCommand(ON)
             Bulb_TRoom_07_sw.sendCommand(ON)
             Bulb_TRoom_08_sw.sendCommand(ON)
             
             PZBL_power.sendCommand(ON)
             
             Thread::sleep(1000)
             TRoomNanoleafEffect.sendCommand("Snowfall")
             TRoomNanoleafBrightness.sendCommand(50)
        }
    }
end

rule "Xiaomi Aqara Battery Powered 2 Button Switch 1"
when
    Channel "mihome:86sw2:3:2:ch1" triggered SHORT_PRESSED
then
    if (gLights_TRoom_all_switch.state == ON || gLights_TRoom_all_night_switch.state == ON || gLights_TRoom_table_switch.state == ON) {
      gLights_TRoom_all_switch.sendCommand(OFF)
      gLights_TRoom_all_night_switch.postUpdate(OFF)
      gLights_TRoom_table_switch.postUpdate(OFF)
                  
      PZBL_power.sendCommand(OFF)
      
      Thread::sleep(1000)
      TRoomNanoleafPower.sendCommand(OFF)
    }
    else if (gLights_TRoom_all_switch.state == OFF || gLights_TRoom_all_switch.state == NULL) {
      gLights_TRoom_table_switch.sendCommand(ON)
      gLights_TRoom_all_night_switch.postUpdate(OFF)
      gLights_TRoom_all_switch.postUpdate(OFF)   
             
      PZBL_power.sendCommand(ON)
             
      Thread::sleep(1000)
      TRoomNanoleafEffect.sendCommand("Snowfall")
      TRoomNanoleafBrightness.sendCommand(50)                
    } 
end

Bsically this two switch should do the same think. U press once it turn different light in room on, u pres twce (or again once) and it turn off different lights in room. I removed all options/actions of buttons and left just few for this example.

I tryied over groups as u see … but need to put delay as some lights react fast and some slow to make them on/off at same time. Also I add different lights and they can not accept same command as other for example switch them into nightmode, so can not use group (gLights_TRoom_all_night_switch) for them.

Than I was also trying use group to find out if any light in room/group is on/off so I can use single press for both - when lights on switch them off and when lights off swith them on

Btw using proxy, where to put proxy item? create some dummy.item file or just into any .item file?