Need assistance with a calculation using a temperature item

OpenHAB 2.5.3
Raspbian 9

I have a Philips Hue motion sensor that also provides the temperature. However, it is reporting about 3 °F too high so I want to create a rule that will update a second item with the corrected temperature for use in my sitemap, etc. However, I am getting odd results from what I thought would be an easy task. Can anyone help me figure out why this is not working?

rule "MB Temp Sensor Correction"
when
	Item PC_LastSeen changed
then
	logInfo("MBTempSensor", MasterBedroomTemperatureSensorTemperature.state.toString)
	var Number MBTemp1 = MasterBedroomTemperatureSensorTemperature.state as Number
	var Number MBTempAdj = MBTemp1 - 3
	logInfo("MBTemp1", MBTemp1.toString)
	logInfo("MBTempAdj", MBTempAdj.toString)
end

This is what I get in the logs when checking the values above.

==> /var/log/openhab2/openhab.log <==
2020-05-07 18:26:20.006 [INFO ] [.smarthome.model.script.MBTempSensor] - 76.406 °F
2020-05-07 18:26:20.023 [INFO ] [lipse.smarthome.model.script.MBTemp1] - 76.406 °F
2020-05-07 18:26:20.029 [INFO ] [pse.smarthome.model.script.MBTempAdj] - 294.820

MasterBedroomTemperatureSensorTemperature is using UoM and its state is a QuantityType. To add 3 °F to it, just do…

var adjusted_temperature = MasterBedroomTemperatureSensorTemperature.getStateAs(QuantityType).add(3|°F)

Oops… you wanted to subtract…

var adjusted_temperature = MasterBedroomTemperatureSensorTemperature.getStateAs(QuantityType).subtract(3|°F)
1 Like

Have you seen the offset profile?

This works! How did you know that the state is a QuantityType? When I look at the item and when I referred to the binding info, they both say the temperature is a Number item.

I never knew what profiles were or how they worked. This is great info. In this case, it is a little more complicated for Celsius, but I can see uses for profiles in the future.

That was from logging out your Item.state.toString. The °F is a part of the state.

1 Like

I don’t understand what you mean. The whole point of QuantityTypes is that you may add 2mm and 3 miles without worrying about unit conversions, the framework sorts it out for you. For that to work, you do have to supply the units for each number … and we call that the QuantityType, a number-with-units.

In your case you want to subtract 3 °F, it doesn’t matter if that is from something in °C or K

When I tried using the offset profile for the channel link, I got this message in the OpenHAB logs:

2020-05-08 13:36:09.698 [WARN ] [nternal.profiles.SystemOffsetProfile] - Received a QuantityType state ‘23.12 °C’ with unit, but the offset is defined as a plain number without unit (-3), please consider adding a unit to the profile offset.

Doing the adjustment via a rule is easier as that is Fahrenheit. Trying to figure out an offset for Celsius to equal 3.5 °F was harder. :slight_smile:

Now, I have it figured out. Using the offset profile is nice. I was able to enter the offset as -3.5 °F, and it is working fine now. No need for the rule at this point, but I am also glad to have more knowledge about how to do temperature calculations in rules, too.

2 Likes