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.)
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
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.")}
}
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:
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."