Remote dimmer (control with UP and DOWN mqtt messages)

  • Platform information:
    • Hardware: Intel
    • OS: Ubuntu
    • Java Runtime Environment: Zulu 17
    • openHAB version:3.4

Hi community!
Yeah, I’m still stuck with OH3. :slight_smile:

I’ve got a device which is controlled via MQTT. It has it’s internal brightness value, which is publishes every time it’s switched on and off or when the brightness changes.
It has onboard controls, and I have now implemented mqtt commands to be sent outta OH to switch it on and off.
The brightness is a different story.
Typical dimmer item, by default, just sends a raw value to a specified mqtt command channel when the value changes.
That’s not suitable, as my device basically expects “UP” or “DOWN” text commands, changes the brightness and then publishes its current values to a dedicated channel.

How can I implement something like that? It feel like there must be some sort of elegant solution, possibly not even requiring rules…

What I’ve tried so far is:

  1. Trying to catch a receivedCommand in rules, as in here - didn’t work. In fact no matter what I did, receivedCommand always equals to current item value, and not ON or OFF or INCREASE or DECREASE etc.
  2. Tried smarter approach, like:
	when
		Item BRIGHTNESS received command
	then
		if (receivedCommand > BRIGHTNESS.state)
		{
			logInfo("TEST_DEVICE", "UP")
 
		}
		else
		{
			logInfo("TEST_DEVICE", "DOWN")
		}
	end

** I’ve omitted the mqtt commans being send after those log commands. There’s nothing interesing there.*
That works, but item BRIGHTNESS stops getting updated with actual brigtness from the divice, and only shows the value from BasicUI, where it is defined as follows:

Setpoint  item=BRIGHTNESS label="Light brightness [%d %%]" minValue=20.0 maxValue=100.0 step=1.0

Please help. Regards!

Well, the easiest way would be, to use a suitable widget, which will ensure to send ON/OFF if it’s a short push but INCREASE/DECREASE for a long push.
As there is no option to translate INCREASE/DECREASE commands within the channel directly, you could use a string channel and some scripted transformation, e.g.

DSL:|if(input.toString == "INCREASE") "UP" else "DOWN"

EDIT: I missed the OH3 part. Inline Script transformation is not available, so you will have to use a JavaScript script instead. /EDIT

Of course it’s also possible through a rule.

If you want to use the value command, you have to ensure that the command is of type number:

rule "translate number command to up or down"
when
    Item BRIGHTNESS received command
then
    if(!(receivedCommand instanceof Number)) {
        logInfo("dimmer", "Command is not a number ({})",receivedCommand)
        // maybe do something for ON/OFF
        return;
    }
    val iCommand = (receivedCommand as Number).intValue
    val iState = (BRIGHTNESS.state as Number).intValue
    if(!Command == iState) {
        logInfo("dimmer", "state is already {}, do nothing",iState)
        return;
    }
    if(iCommand > iState) {
        logInfo("dimmer", "iCommand more than iState -> command is UP")
        myDimmer.sendCommand(UP)
     } else {
        logInfo("dimmer", "iCommand less than iState -> command is DOWN")
        myDimmer.sendCommand(DOWN)
    }
end

BUT: what kind of dimmer is this? I’m pretty sure there is a way to send an absolute dimming position to the device as well.