Design Pattern: Proxy Item

No, it isn’t :slight_smile:

switch (receivedCommand) {
    default : {
    // here some code
    }
}

is exactly the same as

// here some code

as switch has only one (the default) option.

Understand more if the code now. I think I got it right now. Seems to work just fine now. I have the following

.items

Group:Switch:OR(ON, OFF) Power_Switches
Group:String Proxy_Group
Group:String Brightness_Group

Switch Office_Power (Power_Switches)
Number Office_Brightness (Brightness_Group) {autoupdate="false"}
Number Office_Proxy (Proxy_Group)

dimmer_in.rules

import org.eclipse.smarthome.model.script.ScriptServiceUtil 
rule "Dimmer In Rule" 
when
    Member of Power_Switches received command
then
    val CMDItem = ScriptServiceUtil.getItemRegistry.getItem(triggeringItem.name.replace("Power", "Proxy"))
    val BRGItem = ScriptServiceUtil.getItemRegistry.getItem(triggeringItem.name.replace("Power", "Brightness"))
    if (receivedCommand == OFF) CMDItem.postUpdate(1)
    else if (receivedCommand == ON) CMDItem.postUpdate(BRGItem.state)
end

dimmer_in_brightness.rules

import org.eclipse.smarthome.model.script.ScriptServiceUtil 
rule "Dimmer In Brightness Rule" 
when
    Member of Brightness_Group changed
then
    val BRTItem = ScriptServiceUtil.getItemRegistry.getItem(triggeringItem.name.replace("Brightness", "Proxy"))
    if(triggeringItem.state instanceof Number) BRTItem.postUpdate(triggeringItem.state as Number)

end

dimmer_out.rules

import org.eclipse.smarthome.model.script.ScriptServiceUtil 
rule "Dimmer Out Rule" 
when
    Member of Proxy_Group received command
then
    val PWRItem = ScriptServiceUtil.getItemRegistry.getItem(triggeringItem.name.replace("Proxy", "Power"))
    val BRTItem = ScriptServiceUtil.getItemRegistry.getItem(triggeringItem.name.replace("Proxy", "Brightness"))
        if (receivedCommand == 0 && PWRItem.state != OFF) PWRItem.sendCommand(OFF)
        else if (receivedCommand > 0 && PWRItem.state != ON) PWRItem.sendCommand(ON)
        BRTItem.sendCommand(receivedCommand)
end

One thing I just notice now is that I have not even added the groups to my items file and it still works. Do I not have to define the group anymore in the item file? Strange,

Here is the output of openhab log, I think something is still not right.

2019-10-17 12:34:04.519 [ome.event.ItemCommandEvent] - Item 'Office_Proxy' received command 34
2019-10-17 12:34:04.528 [vent.ItemStateChangedEvent] - Office_Proxy changed from 1 to 34
2019-10-17 12:34:04.592 [ome.event.ItemCommandEvent] - Item 'Office_Power' received command ON
2019-10-17 12:34:04.615 [ome.event.ItemCommandEvent] - Item 'Office_Brightness' received command 34
2019-10-17 12:34:04.619 [nt.ItemStatePredictedEvent] - Office_Power predicted to become ON
2019-10-17 12:34:04.649 [vent.ItemStateChangedEvent] - Office_Power changed from OFF to ON
2019-10-17 12:34:04.652 [vent.ItemStateChangedEvent] - Office_Proxy changed from 34 to 100
2019-10-17 12:34:04.772 [vent.ItemStateChangedEvent] - Office_Brightness changed from 100 to 34
2019-10-17 12:34:04.782 [vent.ItemStateChangedEvent] - Office_Proxy changed from 100 to 34

Well, you can make it more and more complex… :wink:
Why building Groups for one Item? Do you have more than one Dimmer? Would have been worth to tell about…
Why using the ItemRegistry?

Why using 1 instead of 0 to switch OFF? In your first posting, you defined dimming light from 1 to 100, 0 should be OFF.

Let me clarify,

Well I have many lights (dimmers) about 20 to control, so with groups/ItemRegistry it makes for less and shorter rules

I am still using the 1 instead of 0 in my actual configuration
My lights have this strange behavior when the brightness channel reaches 0 they actually return to the previous value. The light only accepts 1 to 100. If the light is actually on or off is controlled by the power channel.

Take a look at this:

Group gDimmer_Proxy
Group gDimmer_Dimm
Group gDimmer_Switch

Dimmer MyDimmer_1 "Dimmer 1" (gDimmer_Dimm)
Dimmer MyDimmer_2 "Dimmer 2" (gDimmer_Dimm)
Dimmer MyDimmer_3 "Dimmer 3" (gDimmer_Dimm)

Switch MySwitch_1 "Switch 1" (gDimmer_Switch)
Switch MySwitch_2 "Switch 2" (gDimmer_Switch)
Switch MySwitch_3 "Switch 3" (gDimmer_Switch)

Dimmer ProxyDimmer_1 "Dimmer 1" (gDimmer_Proxy)
Dimmer ProxyDimmer_2 "Dimmer 2" (gDimmer_Proxy)
Dimmer ProxyDimmer_3 "Dimmer 3" (gDimmer_Proxy)
rule "Dimmer Dimm In"
when
    Member of gDimmer_Dimm changed
then
    val myName = triggeringItem.name.split("_").get(1)
    gDimmer_Proxy.members.filter[i|i.name.split("_").get(1) == myName].head.postUpdate(triggeringItem.state as Number)
end

rule "Dimmer Switch In"
when
    Member of gDimmer_Switch changed to OFF
then
    val myName = triggeringItem.name.split("_").get(1)
    gDimmer_Proxy.members.filter[i|i.name.split("_").get(1) == myName].head.postUpdate(OFF)
end

rule "Dimmer out"
when
    Member of gDimmer_Proxy received command
then
    val myName = triggeringItem.name.split("_").get(1)
    if(receivedCommand == 0)
        gDimmer_Switch.members.filter[i|i.name.split("_").get(1) == myName].head.sendCommand(OFF)
    else if(receivedCommand instanceof Number) {
        gDimmer_Switch.members.filter[i|i.name.split("_").get(1) == myName && i.state == OFF].head.sendCommand(ON)
        gDimmer_Dimm.members.filter[i|i.name.split("_").get(1) == myName].head.sendCommand(receivedCommand)
    }
end

No ItemRegistry at all, and the rules are quite simple.

First off I want to thank you for taking the time to teach me some more about coding rules for openhab.
Your last advice is tested and now works perfectly it’s also simple and effective!

I am now going to convert my whole naming scheme as you have shown me to make things more simple!

Thank you again for taking the time to learn a “non coder” and openhab noob a few new tricks

Lights in my home automation setup may be controlled multiple ways: by a physical switch hardwired to the light, by a button on a UI screen, or by a rule in response to some other events.
To keep it simple, I combine the design patterns for Proxy Items, for Groups and for Associated Items. I define rules for the desired behavior at the level of a group, and then assign the lights to that group.

With this setup, the proxy item will always correctly reflect the status of the light, independent of what caused that status (command from a rule, gesture on a physical control, gesture on a UI element).

A more detailed description can be found here.

I wrote small js lib to utilize that pattern in js automatization - openhab-proxy-pattern - npm (npmjs.com)