Rule for Temperature Range

OH 3.4 on RPi
Moen UConnect
Alexa Control Binding

I have an automated bathtub that uses Moen UConnect. I currently have a virtual switch for “summertime” that triggers a variety of activities including the temperature of my bath.

If the vSummertime switch is ON, it draws my bath with a temperature to 106 F, if not, it draws the bath with a temperature to 112 F. (The rule does a bunch of other stuff including turning on/off lights, vents…, but the code below is what triggers two different routines in Alexa which set the different water temperatures for the bath.)

	If (vSummer.state == ON) {

	 BathroomEcho_StartaRoutine.sendCommand("Summer Bath")
	 BathroomEcho_Speak.sendCommand("Drawing Summer bath now.")

	} else {

	 BathroomEcho_StartaRoutine.sendCommand("Winter Bath")
	 BathroomEcho_Speak.sendCommand("Drawing Winter bath now.")

	}

I also have a temperature sensor in my bathroom, so instead of two different presets, I’d loke to create a rule that sets the bath temperature based on a range of temperatures. In other words:

If BathSensorTemp less than 65, draw “winter” bath
else if BathSensor Temp between 65 and 70, draw a “fall” bath
else if BathSensorTemp between 71 and 80, draw a “spring” bath
else draw a “summer” bath

Thanks in advance!

would be a nice job for scale transformation. first setup a file $OPENHAB_CODE/transform/temp2season.scale:

[..65]=winter
]65..70]=fall
]70..80]=spring
]80..]=summer

See Scale - Transformation Services | openHAB for reference.

And switch() in the rule:

switch(transform("SCALE","temp2season.scale",BathSensorTemp.state)) {
    case "winter" : {
    }
    case "fall"   : {
    }
    case "spring" : {
    }
    case "summer" : {
    }
    default : {logWarn("autobath","There was an issue with the automation.")}
}
2 Likes

It is not working. I’m getting the error below:

2023-01-08 16:17:13.007 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'testing-1' failed: An error occurred during the script execution: Could not invoke method: org.openhab.core.model.script.actions.Transformation.transform(java.lang.String,java.lang.String,java.lang.String) on instance: null in testing

I searched for that error and found where the ‘transform’ is looking for three STRINGS. I changed the line below:

switch(transform("SCALE","temp2season.scale",MasterBathSensor_Sensortemperature.state.toString)) {

and the error always falls to the ‘catch all’. Could it be caused by the fact that my bath temperature goes out another decimal? I wrote the value of the temp sensor to a log and it says it is reading 74.9 degrees.

Is MasterBathSensor_Sensortemperature a UoM Item? The state then would be 74.9 °F, which is not valid for Scale.
Try to get rid of the unit in this case:

switch(transform("SCALE","temp2season.scale",(MasterBathSensor_Sensortemperature.state as Number).floatValue.toString)) {

I’m not sure about the toString, openHAB should use .toString if needed.

JRuby’s case statement + range allows you to do this easily.

Assuming, BathSensorTemp is a dimensioned item:

unless BathSensorTemp.state
  BathroomEcho_Speak << "Bath temperature sensor value is undefined"
  return
end

season = case BathSensorTemp.state.to_unit("°F").to_i
         when -100...65 then "Winter"
         when 65...71 then "Fall"
         when 71...80 then "Spring"
         else "Summer"
         end

BathroomEcho_StartaRoutine << "#{season} Bath"
BathroomEcho_Speak << "Drawing #{season} bath now."

Well, there are other options in DSL as well :slight_smile:

if(!(MasterBathSensor_Sensortemperature.state instanceof Number)) {
    logWarn("autobath","Sensor state not valid")
}
val nTemp = (MasterBathSensor_Sensortemperature.state as Number).floatValue

var strSeason = "summer" // default
if(nTemp <= 65)      strSeason = "winter"
else if(nTemp <= 70) strSeason = "fall"
else if(nTemp <= 80) strSeason = "spring"
1 Like

I updated the line and I still get the same result where it falls into the catch all:

switch(transform("SCALE","temp2season.scale",(MasterBathSensor_Sensortemperature.state as Number).floatValue.toString)) { ...

Thank you! This worked perfeclty!