# Need assistance with a calculation using a temperature item

**URL:** <https://community.openhab.org/t/need-assistance-with-a-calculation-using-a-temperature-item/98413>\
**Category:** Scripts & Rules\
**Created:** [May 8, 2020, 6:22pm UTC](https://community.openhab.org/t/need-assistance-with-a-calculation-using-a-temperature-item/98413 "2020-05-08T18:22:11Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![richard.w](https://community.openhab.org/letter_avatar_proxy/v4/letter/r/b782af/32.png) [@richard.w](https://community.openhab.org/u/richard.w)\
**Post date:** [May 8, 2020, 6:22pm UTC](https://community.openhab.org/t/need-assistance-with-a-calculation-using-a-temperature-item/98413/1 "2020-05-08T18:22:11Z")

</div>

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?

```auto
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

---

<div class="post-metadata">

**Author:** ![5iver](https://community.openhab.org/user_avatar/community.openhab.org/5iver/32/21201_2.png) [@5iver](https://community.openhab.org/u/5iver)\
**Post date:** [May 8, 2020, 6:41pm UTC](https://community.openhab.org/t/need-assistance-with-a-calculation-using-a-temperature-item/98413/2 "2020-05-08T18:41:14Z")

</div>

MasterBedroomTemperatureSensorTemperature is using UoM and its state is a [QuantityType](https://github.com/openhab/openhab-core/blob/master/bundles/org.openhab.core/src/main/java/org/openhab/core/library/types/QuantityType.java). 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)

```

---

<div class="post-metadata">

**Author:** ![rossko57](https://community.openhab.org/user_avatar/community.openhab.org/rossko57/32/70263_2.png) [@rossko57](https://community.openhab.org/u/rossko57)\
**Post date:** [May 8, 2020, 7:00pm UTC](https://community.openhab.org/t/need-assistance-with-a-calculation-using-a-temperature-item/98413/3 "2020-05-08T19:00:27Z")

</div>

> [@richard.w](#):
>
> reporting about 3 °F too high

Have you seen the **offset profile**?

> **[Items](https://www.openhab.org/docs/configuration/items.html#profiles)**
>
> a vendor and technology agnostic open source automation software for your home

---

<div class="post-metadata">

**Author:** ![richard.w](https://community.openhab.org/letter_avatar_proxy/v4/letter/r/b782af/32.png) [@richard.w](https://community.openhab.org/u/richard.w)\
**Post date:** [May 8, 2020, 8:36pm UTC](https://community.openhab.org/t/need-assistance-with-a-calculation-using-a-temperature-item/98413/4 "2020-05-08T20:36:31Z")

</div>

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.

> [@5iver](#):
>
> MasterBedroomTemperatureSensorTemperature.getStateAs(QuantityType).subtract(3|°F)

---

<div class="post-metadata">

**Author:** ![richard.w](https://community.openhab.org/letter_avatar_proxy/v4/letter/r/b782af/32.png) [@richard.w](https://community.openhab.org/u/richard.w)\
**Post date:** [May 8, 2020, 8:38pm UTC](https://community.openhab.org/t/need-assistance-with-a-calculation-using-a-temperature-item/98413/5 "2020-05-08T20:38:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rossko57](https://community.openhab.org/user_avatar/community.openhab.org/rossko57/32/70263_2.png) [@rossko57](https://community.openhab.org/u/rossko57)\
**Post date:** [May 8, 2020, 8:55pm UTC](https://community.openhab.org/t/need-assistance-with-a-calculation-using-a-temperature-item/98413/6 "2020-05-08T20:55:28Z")

</div>

> [@richard.w](#):
>
> How did you know that the state is a QuantityType?

> [@richard.w](#):
>
> [.smarthome.model.script.MBTempSensor] - 76.406 °F

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

---

<div class="post-metadata">

**Author:** ![rossko57](https://community.openhab.org/user_avatar/community.openhab.org/rossko57/32/70263_2.png) [@rossko57](https://community.openhab.org/u/rossko57)\
**Post date:** [May 8, 2020, 9:26pm UTC](https://community.openhab.org/t/need-assistance-with-a-calculation-using-a-temperature-item/98413/7 "2020-05-08T21:26:44Z")

</div>

> [@richard.w](#):
>
> In this case, it is a little more complicated for Celsius

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

---

<div class="post-metadata">

**Author:** ![richard.w](https://community.openhab.org/letter_avatar_proxy/v4/letter/r/b782af/32.png) [@richard.w](https://community.openhab.org/u/richard.w)\
**Post date:** [May 9, 2020, 12:59am UTC](https://community.openhab.org/t/need-assistance-with-a-calculation-using-a-temperature-item/98413/8 "2020-05-09T00:59:45Z")

</div>

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. 🙂

---

<div class="post-metadata">

**Author:** ![richard.w](https://community.openhab.org/letter_avatar_proxy/v4/letter/r/b782af/32.png) [@richard.w](https://community.openhab.org/u/richard.w)\
**Post date:** [May 9, 2020, 1:37am UTC](https://community.openhab.org/t/need-assistance-with-a-calculation-using-a-temperature-item/98413/9 "2020-05-09T01:37:15Z")

</div>

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.
