Need advice to set toggle action multi commands on one item

I have item that send command as a number 1-4 and I’m trying to set it in a widget,
I managed to set command 1-2.
My question is if it is possible to set it to send also 3 and more…
This is how it looks to me:

image

                - component: oh-button
                  config:
                    action: toggle
                    actionCommand: "1"
                    actionCommandAlt: "2"
                    actionItem: =props.item_iterations
                    icon-f7: repeat
                    iconSize: 20
                    style:
                      height: auto
                      left: 100px
                      top: 20px
                    text: =(items[props.item_iterations].state)

Use Action: Options instead of toggle and have a look into the documentation

I know ‘options’ but I would prefer toggle if possible

For me toggle is like fliping a coin. You only have two options, how (even without any constraints from openhab) would you like to toggle four options?

Maybe you want to iterate through your options? E.g. clicking the button once will change from 1 to 2, clicking it again will change the state from 2 to 3, etc?

Indeed, a toggle has just two states, e.g. ON or OFF, otherwise it would be a multi state switch.

clicking the button once will change from 1 to 2, clicking it again will change the state from 2 to 3, etc?

This is what I want to do.

Maybe add a rule that will change your item accordingly and everytime you hit the button, the rule is executed

In OH terms, you want to send the item a command, but the command you want to send is determined by the current state of the item.

To translate that to a widget, you want the command action, but you need an expression for the actionCommand that includes takes includes the item’s current state.

                - component: oh-button
                  config:
                    action: command
                    actionCommand: =([0,2,3,4,1])[items[props.item_iterations].state]
                    actionItem: =props.item_iterations
                    icon-f7: repeat
                    iconSize: 20
                    style:
                      height: auto
                      left: 100px
                      top: 20px
                    text: =(items[props.item_iterations].state)

The items state is a number from 1 to 4. The expression ([0,2,3,4,1])[items[props.item_iterations].state] defines an array with 5 elements (because JS arrays are zero-indexed those elements are indexed 0 to 4) and then returns the element from this array that based on the current state of the item. We don’t care about the 0th element because the state of the item will never be 0, so we just set that to any old placeholder, in this case 0. But when the state of the item is 1 we want to return 2 as the command , so the element in the 1 position in the array is 2. Likewise for elements 2 and 3, but when the state of the item is 4 we want to wrap back around to 1.

2 Likes

Exactly what I was looking for, thank you!

  • component: oh-button
    config:
    action: command
    actionCommand: =([0,2,3,4,1])[items[props.item_iterations].state]
    actionItem: =props.item_iterations
    icon-f7: repeat
    iconSize: 20
    style:
    height: auto
    left: 100px
    top: 20px
    text: =(items[props.item_iterations].state)