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

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)) 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?

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.

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… :wink:

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). :wink:

1 Like

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”…

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

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)
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… :man_shrugging:

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?

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.

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

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?

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.

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

Try it without the pipe symbol.

1 Like

That worked! :wink: thanks.

so it works! :wink:
I just did this:

curl -X POST --header "Content-Type: text/plain" --header "Accept: application/json" -d "14°F" ""
=> 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… :wink:

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…

1 Like