Merge 2 items in one sitemap selection

Hello,

I’m trying to combine two items in a selection but I can’t. I am using OH 3.4.

I am using a modbus device to control a fan.

The fan speed command is on a first modbus 40001 holding register (values to send: 3 = HIGH, 2 = MID, 3 = LOW).
The fan start/stop command is on a second 40002 holding register (values to send: 0 = OFF, 1 = ON).

My current working items in sitemap:

Selection icon=“fan” item=DATA__FAN_level_Value_as_Number label=“FAN []” mappings=[3=“10%”,2=“50%”,1=“100%”]
Switch icon=“fan” item=DATA__FAN_power_Value_as_Switch

I would like to have everything in a single selection of this style:

Selection icon=“fan” item=DATA__FAN_level_Value_as_Number label=“FAN []” mappings=[4=“OFF”,3=“10%”,2=“50%”,1=“100%”]

“4” send the command 0 to DATA__FAN_power_Value_as_Switch to power off the device. When we select one of the speed, we send the speed command to DATA__FAN_level_Value_as_Number and the command “1” to DATA__FAN_power_Value_as_Switch if it was “0”.

I searched but I don’t really understand how to do it.

Thank you in advance for your help.

Use a proxy Item:

Maybe consider to use a more simple naming for Items:

Switch Fan_Power "Fan switch" {channel="modbus:...:40002"}
Number Fan_Speed "Fan Speed"  {channel="modbus:...:40001"}
Number Fan_Proxy "Fan"        // no link, it's a proxy

Sitemap:

Switch item=Fan_Proxy mappings=[0="OFF",3="LOW",2="MID",1="HIGH"]

rule:

rule "control fan"
when
    Item Fan_Proxy received command
then
    switch(receivedCommand.toString) {
        case "0" : Fan_Power.sendCommand(OFF)
        case "1" : {
            Fan_Power.sendCommand(ON)
            Fan_Speed.sendCommand(1)
        }
        case "2" : {
            Fan_Power.sendCommand(ON)
            Fan_Speed.sendCommand(2)
        }
        case "3" : {
            Fan_Power.sendCommand(ON)
            Fan_Speed.sendCommand(3)
        }
        default  : logWarn("fan","command not recognised: {}",receivedCommand)
    }
end

If the Fan is also switched independendly from openHAB, you will need a second rule to update the state of the Proxy item.

Hi Udo,

Thanks for the answer.

I implemented the solution you gave me. It more or less seems to work. I had managed to do almost the same thing with a proxy item using a rule with “if” command but he “cases” are much shorter and more effective. Great.

I have two concerns.

  • In the sitemap, I use a selection not a switch. Switch use too more space and don’t fit on the smartphone screen.
  • When I click on the “OFF” box in the sitemap, the command OFF is sent but immediately it returns to ON (see below). I don’t understand why. I’m using “switch” in sitemap for the test.

Sorry, for the long item names but as I will have more than one fan and room, I used a more complex naming.

2023-04-01 10:57:20.373 [INFO ] [openhab.event.ItemCommandEvent ] - Item ‘proxy_extracteur_control’ received command 0
2023-04-01 10:57:21.664 [INFO ] [openhab.event.ItemCommandEvent ] - Item ‘DATA__controleur_extracteur_power_local_2_Value_as_Switch’ received command OFF
2023-04-01 10:57:21.678 [INFO ] [penhab.event.ItemStatePredictedEvent] - Item ‘DATA__controleur_extracteur_power_local_2_Value_as_Switch’ predicted to become OFF
2023-04-01 10:57:21.685 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item ‘DATA__controleur_extracteur_power_local_2_Value_as_Switch’ changed from ON to OFF
2023-04-01 10:57:22.472 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item ‘DATA__controleur_extracteur_power_local_2_Value_as_Switch’ changed from OFF to ON

The modbus fan controler has a LCD screen where I can locally modify the settings and the feedback channel is not managed (as you suggested at the end of your post). I’ll also need the corresponding script.

Sorry for my first comment, I corrected the errors. I wrote a wrong address in the thing and I was updating the wrong register. Everything works including the “Selection” in the sitemap.

Only the feedback from the device to update the proxy is not working as I need a rule for that.

Easy :slight_smile:

rule "state of fan"
when
    Item Fan_Speed changed or
    Item Fan_Power changed
then
    var iStep = 0
    if(Fan_Power.state != OFF) {
        iStep = if(Fan_Speed.state instanceof Number) (Fan_Speed.state as Number) else -1
    }
    if(Fan_Proxy.state.toString != iStep.toString)
        Fan_Proxy.postUpdate(iStep)
end

If there is any issue with the reported value (i.e. no valid number), the Proxy Item will be set to -1, which can be used with a mapping (display only) to tell there’s an error.

Easy for your. :wink:

Me, I’ll still learning. :wink:

Still an error with the new rule.

Type mismatch: cannot convert from Number to int; line 3, column 191, length 77

Rule:

var iStep = 0
if(DATA__controleur_extracteur_power_local_2_Value_as_Switch.state != OFF) {
    iStep = if(DATA__controleur_extracteur_vitesse_local_2_Value_as_Number.state instanceof Number) (DATA__controleur_extracteur_vitesse_local_2_Value_as_Number.state as Number) else -1
}
if(proxy_extracteur_control_l2.state.toString != iStep.toString)
    proxy_extracteur_control_l2.postUpdate(iStep)

Oh, sorry, forgot an .intValue…

rule "state of fan"
when
    Item Fan_Speed changed or
    Item Fan_Power changed
then
    var iStep = 0
    if(Fan_Power.state != OFF) {
        iStep = if(Fan_Speed.state instanceof Number) (Fan_Speed.state as Number).intValue else -1
    }
    if(Fan_Proxy.state.toString != iStep.toString)
        Fan_Proxy.postUpdate(iStep)
end

Great!! It works both way.

The device is here. Very nice device. Several different models exist with various input/output configurations. It’s a bit of a jungle to find the right model for that matter.

They could have put the FAN “OFF” in the same register as the speed. It would have been easier. Currently, I have to use the general ON/OFF of the device for stopping the fan.

Thanks for your help.

Have nice week-end.