Constants in sitemap mappings

Hi,

my goal is to define constants to be used in a sitemap item’s mappings.

As is pretty common, I define a constant as an item which is inialized at system start:

my.items

Number DesiredLowTemp
Number DesiredHighTemp

my.rules

rule "SetConstants"
when
 System started
then
 DesiredLowTemp.postUpdate(14)
 DesiredHighTemp.postUpdate(18)
end

Now, I’d like to have a switch in my sitemap which allows me to choose between both temperatures:

Switch item=RoomTemp mappings=[DesiredLowTemp="low", DesiredHighTemp="high"]

However, openhab does not seem to recognize this syntax, i.e. pushing the “low” button or the “high” button does not change RoomTemp. (Also, using e.g. DesiredLowTemp.state=“low” does not do the trick.)

What am I doing wrong?

Thank you very much in advance!

I’d use a switch( a"virtual" one since it has no channel connected) and a rule which acts on “received comand” of that switch. In that rule you can set the temperature accordingly. I would keep the presettings as global variables saved in that rule.

To elaborate on why what you have doesn’t work.

mappings in the sitemap will set the Switch to use one or more buttons instead of a toggle to receive a command.

Each Item will have a button assigned to it. The left side of the “=” will be the label for the button and the right side of the “=” will be what state the item gets set to. Consequently you will end up with one button named “DesiredLowTemp” which when pressed will try to set RoomTemp to the String “low”.

Obviously this will generate errors because “low” is a String and RoomTemp requires a Number.

You either have to:

mappings=["Low"=14, "High"=18]

or you have to create an Unbound Item and a Rule like @opus describes. This would look something like:

Items

String DesireTemp
Number DesiredLowTemp
Number DesiredHighTemp
Number RoomTemp

Rules

rule "SetConstants"
when
 System started
then
 DesiredLowTemp.postUpdate(14)
 DesiredHighTemp.postUpdate(18)
end

rule "Apply temp"
when
    Item DesiredTemp received command
then
  if(receivedCommand.toString == "low") RoomTemp.sendCommand(DesiredLowTemp.state)
  else RoomTemp.sendCommand(DesiredHighTemp.state)
end

Sitemap

Switch item=DesiredTemp mappings=["Low"="low", "High"="high"]
1 Like

Hi to both of you,

and thank you for your suggestions!

Your solution works, basically. However, when rendered in a sitemap, a “virtual” switch does not show the chosen temperature, since its value is merely an additional representation of the according constant.

For rendering the sitemap, I would really prefer an item whose value is the chosen temperature: The goal is to apply e.g. custom colors to the temperature value based on whether a constant was chosen (using the switch) or not (e.g. using an additional selection).

Unfortunately, since I am still getting used to OH, I have not come up with a solution, yet…

Thanks again!
ub

PS @rlkoshak I have a sitemap working with some three way switches where in the mappings each left side is a value and each right side a label.

Hi again,

I solved the problem as follows.

The UI switch for preset temperatures operates on a String item RoomTempPresetSwitch, using switch states OFF, LOW, and HIGH. To actually set a preset temperature, a rule listens for commands to RoomTempPresetSwitch and then changes the actual Number item RoomTemp (which is bound to the heating control) accordingly, i.e. to constants OffTemp, LowTemp, resp. HighTemp.

Temperatures other than the three preset ones can be chosen via an additional selection, which is omitted from the below code for the sake of brevity.

For the UI switch to show actual chosen temperatures (other than preset ones), I use a rule that listens for updates to RoomTemp and changes RoomTempPresetSwitch

  • either to a preset state (OFF, LOW, or HIGH depending if the new temperature is one of OffTemp, LowTemp, resp. HighTemp)
  • or, if the chosen temperature is not a preset one, to the acutal temperature (or, really, a string representation of it).

Note: An infinite loop of RoomTempPresetSwitch and RoomTemp updating each other is avoided by making appropriate use of commands and updates.

This way, I can use dynamic sitemap coloring based on whether a preset temperature or another temperature was chosen, etc. In the example below I set orange color if the room is currently in need for heat.

my.items

String RoomTempPresetSwitch "MyRoom [%s]"
Number RoomTemp "MyRoom [%.1f]" {
 // binding definition
}

Number OffTemp  // this constant is initialized in a system startup rule
Number LowTemp  // this constant is initialized in a system startup rule
Number HighTemp // this constant is initialized in a system startup rule

my.sitemap

Switch item=RoomTempPresetSwitch  mappings=["OFF"="off", "LOW"="low", "HIGH"="high"] valuecolor=["OFF"="white","LOW"="white","HIGH"="white",CurrentlyHeatingRoom==ON="orange"]  labelcolor=[CurrentlyHeatingRoom==ON="orange"]

my.rules

rule "set preset temperature"
when
 Item RoomTempPresetSwitch received command
then
 if (RoomTempPresetSwitch.state == "OFF")
  RoomTemp.sendCommand(OffTemp.state)
 else if (RoomTempPresetSwitch.state == "LOW")
  RoomTemp.sendCommand(LowTemp.state)
 else if (RoomTempPresetSwitch.state == "HIGH")
  RoomTemp.sendCommand(HighTemp.state)
end

rule "set UI"
when
 Item RoomTemp received update
then
 if (RoomTemp.state == OffTemp.state)
  RoomTempPresetSwitch.postUpdate("OFF")
 else if (RoomTemp.state == LowTemp.state)
  RoomTempPresetSwitch.postUpdate("LOW")
 else if (RoomTemp.state == HighTemp.state)
  RoomTempPresetSwitch.postUpdate("HIGH")
 else
  RoomTempPresetSwitch.postUpdate(String::format("%.1f", (RoomTemp.state as DecimalType).floatValue))
end

Regards
ugh_bough