# Updating Units of Measurement items via API (and display the right one)

**URL:** <https://community.openhab.org/t/updating-units-of-measurement-items-via-api-and-display-the-right-one/98897>\
**Category:** Items & Sitemaps\
**Tags:** restapi, units\
**Created:** [May 16, 2020, 8:34am UTC](https://community.openhab.org/t/updating-units-of-measurement-items-via-api-and-display-the-right-one/98897 "2020-05-16T08:34:02Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![binderth](https://community.openhab.org/user_avatar/community.openhab.org/binderth/32/56257_2.png) [@binderth](https://community.openhab.org/u/binderth)\
**Post date:** [May 16, 2020, 8:34am UTC](https://community.openhab.org/t/updating-units-of-measurement-items-via-api-and-display-the-right-one/98897/1 "2020-05-16T08:34:02Z")

</div>

I’m having difficulties to understand the UoM-concept in terms of items not attached to a binding and things. As I understand, if a binding is configured and updates states, you can define the “origin” unit for the binding and then I can decide in the item-definition, if I want another unit. So e.g. my binding collects sensors in imperial measures, I can easily display them in the item as metric measures.

So far so good. I use a weatherstation (see [Outside WiFi weather station "WS3500" and local customized upload (no cloud needed) - #2 by binderth](https://community.openhab.org/t/outside-wifi-weather-station-ws3500-and-local-customized-upload-no-cloud-needed/98756/2)) and the WS3500 automatically sends the corresponding values in imperial measurement to the openHAB REST-API and updates my items.

So now: can I tell openHAB, that the source is Fahrenheit and the value should be Celsius?

---

<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 16, 2020, 9:01am UTC](https://community.openhab.org/t/updating-units-of-measurement-items-via-api-and-display-the-right-one/98897/2 "2020-05-16T09:01:55Z")

</div>

There are system default units.

Who cares, because when you send a value to your Item, you will give units.

When you display, you select the unis you want to see in the state formatting part.

---

<div class="post-metadata">

**Author:** ![binderth](https://community.openhab.org/user_avatar/community.openhab.org/binderth/32/56257_2.png) [@binderth](https://community.openhab.org/u/binderth)\
**Post date:** [May 16, 2020, 10:45am UTC](https://community.openhab.org/t/updating-units-of-measurement-items-via-api-and-display-the-right-one/98897/3 "2020-05-16T10:45:55Z")

</div>

I know, but how do I tell the REST-API that I _update_ with Fahrenheit and _read_ with Celsius? That’s what I’m missing somehow… 😉

---

<div class="post-metadata">

**Author:** ![mhilbush](https://community.openhab.org/user_avatar/community.openhab.org/mhilbush/32/56108_2.png) [@mhilbush](https://community.openhab.org/u/mhilbush)\
**Post date:** [May 16, 2020, 10:54am UTC](https://community.openhab.org/t/updating-units-of-measurement-items-via-api-and-display-the-right-one/98897/4 "2020-05-16T10:54:37Z")

</div>

> [@binderth](#):
>
> how do I tell the REST-API that I _update_ with Fahrenheit and _read_ with Celsius

When you update your item you specify the units (e.g. `21|°F`) and when you display the item, you specify the unit in the item or sitemap label (e.g. `"Temp [%.1f °F]"`). Alternatively, if you have the openHAB regional settings configured to use Metric, you should just be able to do `"Temp [%.1f %unit%]"`

Edit: I apologize if there are any typos in the above. It’s still early and I haven’t had much coffee yet). 😉

---

<div class="post-metadata">

**Author:** ![binderth](https://community.openhab.org/user_avatar/community.openhab.org/binderth/32/56257_2.png) [@binderth](https://community.openhab.org/u/binderth)\
**Post date:** [May 16, 2020, 11:03am UTC](https://community.openhab.org/t/updating-units-of-measurement-items-via-api-and-display-the-right-one/98897/5 "2020-05-16T11:03:19Z")

</div>

So I’ll have a look into the “77|°F” option, because right now, I have the locale set to metric - so an update is supposed to be in Celsius. So my PHP-Script would be a way more complex - so I’ll guess I stick to having double items and update the metric ones “onUpdate”…

---

<div class="post-metadata">

**Author:** ![mhilbush](https://community.openhab.org/user_avatar/community.openhab.org/mhilbush/32/56108_2.png) [@mhilbush](https://community.openhab.org/u/mhilbush)\
**Post date:** [May 16, 2020, 11:07am UTC](https://community.openhab.org/t/updating-units-of-measurement-items-via-api-and-display-the-right-one/98897/6 "2020-05-16T11:07:28Z")

</div>

> [@binderth](#):
>
> I have the locale set to metric

That’s fine. You can still update an item by specifying Fahrenheit explicitly.

> [@binderth](#):
>
> So my PHP-Script would be a way more complex

I’m not following. Doesn’t your script just take the weather station data and update the items through the REST API? Why would it be more complex?

---

<div class="post-metadata">

**Author:** ![binderth](https://community.openhab.org/user_avatar/community.openhab.org/binderth/32/56257_2.png) [@binderth](https://community.openhab.org/u/binderth)\
**Post date:** [May 16, 2020, 11:11am UTC](https://community.openhab.org/t/updating-units-of-measurement-items-via-api-and-display-the-right-one/98897/7 "2020-05-16T11:11:57Z")

</div>

> [@mhilbush](#):
>
> I’m not following. Doesn’t your script just take the weather station data and update the items through the REST API? Why would it be more complex?

see [Outside WiFi weather station "WS3500" and local customized upload (no cloud needed) - #2 by binderth](https://community.openhab.org/t/outside-wifi-weather-station-ws3500-and-local-customized-upload-no-cloud-needed/98756/2)  
presently I just grab the GET-Values and throw them blindly at the REST-API (with a prefix). Someplace I have to have some logic… So either in the PHP, so I have explicit actions on the GET-variable (like adding |°F or |inch), or I just add the logic in rules… 🤷‍♂️

---

<div class="post-metadata">

**Author:** ![mhilbush](https://community.openhab.org/user_avatar/community.openhab.org/mhilbush/32/56108_2.png) [@mhilbush](https://community.openhab.org/u/mhilbush)\
**Post date:** [May 16, 2020, 11:16am UTC](https://community.openhab.org/t/updating-units-of-measurement-items-via-api-and-display-the-right-one/98897/8 "2020-05-16T11:16:38Z")

</div>

Ok. I don’t know PHP, but can’t you just create another key-value pair containing the units. Then concatenate the value and unit when you call the REST API?

---

<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 16, 2020, 11:16am UTC](https://community.openhab.org/t/updating-units-of-measurement-items-via-api-and-display-the-right-one/98897/9 "2020-05-16T11:16:50Z")

</div>

> [@binderth](#):
>
> So I’ll have a look into the “77|°F” option, because right now, I have the locale set to metric - so an update is supposed to be in Celsius.

The update will be in whatever units you supply. That’s what you tell it, that’s what goes into the state.  
When you come to read out the state, for display or for processing in a rule etc., it is up to you to take note of whatever units it is in -or- to request it in the units of your choice (for auto conversion).

For REST API, you’ll probably have to study how a UI does it using [label format] options.

---

<div class="post-metadata">

**Author:** ![binderth](https://community.openhab.org/user_avatar/community.openhab.org/binderth/32/56257_2.png) [@binderth](https://community.openhab.org/u/binderth)\
**Post date:** [May 16, 2020, 12:49pm UTC](https://community.openhab.org/t/updating-units-of-measurement-items-via-api-and-display-the-right-one/98897/10 "2020-05-16T12:49:32Z")

</div>

> [@mhilbush](#):
>
> When you update your item you specify the units (e.g. `21|°F` )

played around a bit, but adding “|°F” to my API-call just threw this warning:

```auto
2020-05-16 13:50:38.785 [WARN] [rest.core.internal.item.ItemResource] - Received HTTP POST request at 'items/Weatherstation_indoortempf' with an invalid status value '77.4|°F'.

```

But just to get this straight in my head:

1. my locale is set to metric
2. in my items, I defined the “Number:Temperature” as “…%unit%”
3. if I update the item via REST with the “|°F” suffix (?), it should automatically update the value to Celsius?

---

<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 16, 2020, 12:59pm UTC](https://community.openhab.org/t/updating-units-of-measurement-items-via-api-and-display-the-right-one/98897/11 "2020-05-16T12:59:36Z")

</div>

> [@binderth](#):
>
> adding “|°F” to my API-call just threw this warning

What is your Item type?

I’m not sure of the syntax to pass units via API, you might have to examine what the UIs do.

> [@binderth](#):
>
> if I update the item via REST with the “|°F” suffix (?), it should automatically update the value to Celsius?

I don’t think so. You’re telling it you want F. It’s a dumb machine and will do what you tell it.

---

<div class="post-metadata">

**Author:** ![mhilbush](https://community.openhab.org/user_avatar/community.openhab.org/mhilbush/32/56108_2.png) [@mhilbush](https://community.openhab.org/u/mhilbush)\
**Post date:** [May 16, 2020, 1:35pm UTC](https://community.openhab.org/t/updating-units-of-measurement-items-via-api-and-display-the-right-one/98897/12 "2020-05-16T13:35:37Z")

</div>

> [@binderth](#):
>
> “|°F”

Try it without the pipe symbol.

```auto
71°F

```

---

<div class="post-metadata">

**Author:** ![binderth](https://community.openhab.org/user_avatar/community.openhab.org/binderth/32/56257_2.png) [@binderth](https://community.openhab.org/u/binderth)\
**Post date:** [May 16, 2020, 4:04pm UTC](https://community.openhab.org/t/updating-units-of-measurement-items-via-api-and-display-the-right-one/98897/13 "2020-05-16T16:04:24Z")

</div>

> [@mhilbush](#):
>
> Try it without the pipe symbol.
> 
> ```auto
> 71°F
> 
> ```

That worked! 😉 thanks.

---

<div class="post-metadata">

**Author:** ![binderth](https://community.openhab.org/user_avatar/community.openhab.org/binderth/32/56257_2.png) [@binderth](https://community.openhab.org/u/binderth)\
**Post date:** [May 16, 2020, 4:53pm UTC](https://community.openhab.org/t/updating-units-of-measurement-items-via-api-and-display-the-right-one/98897/14 "2020-05-16T16:53:41Z")

</div>

> [@rossko57](#):
>
> I don’t think so. You’re telling it you want F. It’s a dumb machine and will do what you tell it.

so it works! 😉  
I just did this:

```auto
curl -X POST --header "Content-Type: text/plain" --header "Accept: application/json" -d "14°F" "http://192.168.178.10:8080/rest/items/Weatherstation_indoortempf"
=> resulted in:
2020-05-16 18:03:28.523 [vent.ItemStateChangedEvent] - Weatherstation_indoortempf changed from 74.8 °C to -10.00 °C

```

of course, it’s not freezing cold inside =\> but you can see, it worked. Thing is… Now I already rewrote everything to fit - but I won’t need the wrong units anyways, so I just refactored everything from scratch… 😉

one thing needed a bit tweaking, though. The “base unit” for “length” for metric is “meter”, for imperial is “inch”. So if the weatherstation sends “0.252 inch” of rain, it gets translated to “0.0 meter” of rain. Which should be “0.6mm” of rain. So I hat to change the Unit in the items from %unit% to mm - and it worked also…
