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