today I’m trying to replace a slider with two buttons in an OH3 custom widget…
The volume item is persisted and I can get the actual state, but how do I tell the item to increase/decrease without the slider?
and what would the syntax for that look like?
but this is a volume item, and I dont understand how to tell the actionCommand to get the actual volume, add or subtract 1 percent, and send this command back to the item…
edit:
I think the according channel ony accepts numbers, not +1 or -1
the slider does this “automatically” but I dont know how to acchive this behavior from a button…
Your volume channel may accept INCREASE and DECREASE commands, in which case you won’t have to dynamically define anything. If the channel does not, or you want increments that are a different size than the binding defined increases and decreases, then you’ll need to use an expression to build the numerical command.
To build the command you only need to remember one important thing: the state that’s returned from the items object is a string not a number (so you can’t do math until you fix that).
but this is still a string, so you can’t add anything to it yet. So, you use the js Number() object to convert that state string to a number. There are a couple of different ways to do this, and I don’t know the details of your item but I suspect the easiest will be to use the parseInt method:
parseInt returns an object that the expression parser recognizes as a number suitable for whatever further math you wish to do. For example:
12 + 1
the parser understand as simple math and returns 13. However if one of the inputs is a string:
"12" + 1
the parses interprets it as a command to concatenate the strings and returns "121". So if you have a string that represents a number (such as from a call to items[some_item_name].state) then you have to turn that string into a number to get back to something the parser understands as a math command:
Number.parseInt("12") + 1
Now the parser first convertes "12" into 12 and now it looks just like the original form and it knows you want to do math and not concatenation and so returns 13.
Yep, your example is equivalent to the final example in my post; you just don’t need the extra (...) around the whole expression, but they don’t hurt.
I initially opened this thread because my approach to use the DECREASE INCREASE commands for the buttons was not working.
After trying @JustinG suggestions without luck I had a look at my item’s definition and it’s not a NUMBER or STRING type - no, its actually a DIMMER…
Now I tried to write a test rule which executes:
SqueezeLivingroomVolume.sendCommand(DECREASE)
and this works unfortunately… I tried to change back to my previous approach with DECREASE INCREASE but this is still not working in yaml… I tried different things:
actionCommand: DECREASE
actionCommand: “DECREASE”
actionCommand: ‘DECREASE’
and even tried to add a props item with DECREASE and linked it with
actionCommand: =props.volumedown
but nothing changes - still not working - any ideas?
openHAB Items of type Dimmer accept commands INCREASE/DECREASE. But not all of the devices that might be linkd to a Dimmer Item actually respond or do anything with commands like that. It’s up to your binding/device whether they have any effect.
If you look in your events.log, you should be able to see commands arriving from your UI, whether they get actioned or not.
now everything is looking good - tried both and both working
but like @JustinG mentioned the DECREASE INCREASE uses bigger steps in my case …so I stick with the conversion and math approach