RULES: set value of a Dimmer Device from Dimmer Switch - SOLVED

  • Platform information:
    • OS: Ubuntu LTS - Debian bullseye/sid
    • Java Runtime Environment: 8u201-1~webupd8~1
    • openHAB version: 2.5.9-1

PROBLEM: I am unable to set value of Dimmer Device from Dimmer Switch in Rule
Could someone help me with the correct syntax (to write a dimmer value to a Dimmer)

I have a Phillips HUE Dimmer switch working (Zigbee)
PaperUI shows the dimmer status is working - 1 produces 100% and dimmer up/down works, 0 = Off

I have a Smart Home Zigbee Dimmer Device - Zigbee (connected to some downlights)
PaperUI shows the device is controllable - sliding the Dimmer 0 to 100% is working perfectly.

Now to connect the Dimmer switch to the Dimmer Unit using a rule

I understand my problem is a mismatch of types, as a test, I was originally trying to control my LIFX lights which want HSB and soon realised I was out of my depth (thank you @rlkoshak for all your tutorials). I have forged ahead as I assumed connecting Dimmer to Dimmer I would not have to deal with transforming the data…but…

Following is my rule which at least Visual Studio seemed happy with, but still produces an error …

rule "React on Level Control (PhilipsRWL02123736_LevelControl) change/update"
when
    Item PhilipsRWL02123736_LevelControl changed
then    
    SmartHomeDELXN56DS27LX13_LevelControl.sendCommand(PhilipsRWL02123736_LevelControl)
end

Pressing the button on the HUE Switch, produced the following log…

==> /var/log/openhab2/events.log <==
2020-11-23 22:14:39.098 [vent.ItemStateChangedEvent] - PhilipsRWL02123736_LevelControl changed from 6 to 0

==> /var/log/openhab2/openhab.log <==
2020-11-23 22:14:39.099 [WARN ] [rthome.model.script.actions.BusEvent] - Cannot convert ‘PhilipsRWL02123736_LevelControl’ to a state type which item ‘SmartHomeDELXN56DS27LX13_LevelControl’ accepts: [PercentType, OnOffType, UnDefType].

I initially tried to set the Dimmer form the Switch by the action:

SmartHomeDELXN56DS27LX13_LevelControl = PhilipsRWL02123736_LevelControl

But this produces…
==> /var/log/openhab2/events.log <==
2020-11-23 22:33:32.459 [vent.ItemStateChangedEvent] - PhilipsRWL02123736_LevelControl changed from 8 to 9

==> /var/log/openhab2/openhab.log <==
2020-11-23 22:33:32.459 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule ‘React on Level Control (PhilipsRWL02123736_LevelControl) change/update’: An error occurred during the script execution: Cannot assign a value in null context.

I apologise, but this is a bit beyond me. I have tried combinations of sendCommand postUpdate. The most simplest action produces a similar error.

How would I read the Dimmer switch and postUpdate that in the correct format?
The switch gives me a 0-100 which I think openhab treats as a percentage
The Dimmer requires…
SmartHomeDELXN56DS27LX13_LevelControl’ accepts: [PercentType, OnOffType, UnDefType].

Instead of…

`postUpdate(SmartHomeDELXN56DS27LX13_LevelControl, PhilipsRWL02123736_LevelControl)`

Is it something like

`postUpdate(SmartHomeDELXN56DS27LX13_LevelControl, [PhilipsRWL02123736_LevelControl,,])`

Thanks in advance for some help
Ian

Please use code fences. It’s very difficult to read any code without them. Just select the text and press the </> button.

rule “React on Level Control (PhilipsRWL02123736_LevelControl) change/update”
when
Item PhilipsRWL02123736_LevelControl changed
then
SmartHomeDELXN56DS27LX13_LevelControl.sendCommand(PhilipsRWL02123736_LevelControl)
end

The problem is that you’re not sending the state of your Philips items, you’re sending the item.

From the Rules documentation:

In openHAB, every item carries a state. The state of an Item is an Object itself and can be accessed with MyItem.state. A complete and up-to-date list of item types are currently allowed in OpenHAB and the command types each item can accept is given in the openHab documentation for items. To use the state of an Item in rules it is often necessary to know what type of state the Item is carrying and how to convert it into types that can be used in such operations. Conversely, to use the result of a calculation to modify the state of an item may require its transformation into a suitable type.

So, what you need to send is the item state, which you can convert to a string.

SmartHomeDELXN56DS27LX13_LevelControl.sendCommand(PhilipsRWL02123736_LevelControl.state.toString)

I believe this should work so long as you’re correct that the values sent by the switch are what the device wants to see. However, if that’s the case then I think you’re actually better off using the Follow profile. Basically, you tell the device item that it should always follow changes to the switch item. So if the switch changes, the device changes.

Note that in both cases (rule or follow profile), the updates are going in one direction. If you change the device state from a sitemap or other UI, then it will be out of sync with your switch. You can synchronize in both directions, but you have to be careful not to create an endless loop.

Absolutely correct, this has solved my problem. My humble thanks. I will study this so that I may understand the process in the future. Your comments about a device “following” an item makes total sense, I will investigate implementing this, for now, your code will be in my rule!!

rule "React on Level Control (PhilipsRWL02123736_LevelControl) change/update"
when
    Item PhilipsRWL02123736_LevelControl changed
then
 SmartHomeDELXN56DS27LX13_LevelControl.sendCommand(PhilipsRWL02123736_LevelControl.state.toString) 
end

Good stuff. Instead of rewriting the title, you can click “Solution” on one of the posts to mark it as solved. That helps people in the future who might have a similar issue.