# MQTT sensor offset in item def

**URL:** <https://community.openhab.org/t/mqtt-sensor-offset-in-item-def/4942>\
**Category:** Beginners\
**Created:** [December 10, 2015, 6:00pm UTC](https://community.openhab.org/t/mqtt-sensor-offset-in-item-def/4942 "2015-12-10T18:00:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nikoraes](https://community.openhab.org/letter_avatar_proxy/v4/letter/n/59ef9b/32.png) [@nikoraes](https://community.openhab.org/u/nikoraes)\
**Post date:** [December 10, 2015, 6:00pm UTC](https://community.openhab.org/t/mqtt-sensor-offset-in-item-def/4942/1 "2015-12-10T18:00:52Z")

</div>

I have a bunch of sensors with the mqtt binding that would need a certain offset. I’m now doing this with separate items and rules (many), but I’m wondering if I could do this in the item definition with a transformation.

Basically I just need to add or subtract a certain value from the value I get through mqtt.

Thank you.

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [December 10, 2015, 6:13pm UTC](https://community.openhab.org/t/mqtt-sensor-offset-in-item-def/4942/2 "2015-12-10T18:13:03Z")

</div>

If the data is XML, you can do some simple math [inside of an XSLT transform](http://www.java2s.com/Tutorial/XML/0100__XSLT-stylesheet/Calculatewithvariable.htm). You can also do some simple [math with regular expressions](http://stackoverflow.com/questions/5245087/math-operations-in-regex). I don’t know if you can do the same for JSON.

Of course, this assumes that the libraries openHAB uses for these transforms support these operations which is not guaranteed. Personally I try to go out of my way to avoid doing transforms. 😄

If the offset is the same for all Items, you can add your items to the same group and write a single rule to add the offset to the most recently changed Item. I’m not sure how well this works if you have lots of items changing around the same time. It requires persistence to work.

```
rule "Add offset"
when
    Item ItemOne changed or
    Item ItemTwo changed or
    ...
then
    Thread::sleep(100) // give persistence time to catch up, experiment to find the minimum amount of time needed
    val mostRecent = gMyItems.members.sortBy[lastUpdate].last as NumberItem
    mostRecent.postUpdate((mostRecent.state as DecimalType) + offset)
end

```

NOTE: I don’t trigger on updates to the group because the group will receive multiple updates for each change to its member items which would cause the offset to be added more than once.

---

<div class="post-metadata">

**Author:** ![watou](https://community.openhab.org/user_avatar/community.openhab.org/watou/32/70595_2.png) [@watou](https://community.openhab.org/u/watou)\
**Post date:** [December 10, 2015, 6:30pm UTC](https://community.openhab.org/t/mqtt-sensor-offset-in-item-def/4942/3 "2015-12-10T18:30:11Z")

</div>

If it were me, I would replace the `default` at the end of binding string with `JS(offset.js)`. Assuming the MQTT message is a number and you wanted 5 less that what MQTT reports, your `offset.js` file would look something like

```javascript
(function(i){ return i - 5; })(input)

```

You would need a different `.js` file for each different offset value.

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [December 10, 2015, 6:40pm UTC](https://community.openhab.org/t/mqtt-sensor-offset-in-item-def/4942/4 "2015-12-10T18:40:53Z")

</div>

Forgot about JS transforms. That is absolutely the way to go.

---

<div class="post-metadata">

**Author:** ![nikoraes](https://community.openhab.org/letter_avatar_proxy/v4/letter/n/59ef9b/32.png) [@nikoraes](https://community.openhab.org/u/nikoraes)\
**Post date:** [December 10, 2015, 7:45pm UTC](https://community.openhab.org/t/mqtt-sensor-offset-in-item-def/4942/5 "2015-12-10T19:45:39Z")

</div>

Thank you.  
The JS transform works great.
