Convert Wind Direction (in degrees) to Compass Points

Trying to convert my Wind Direction Item to display Compass Points (text) rather than Degrees (number.
Running 4.1.1 and configured entirely using MainGUI.

I searched and found many threads on this and I understand I should be using a Scale function but the thing that confusing is how to do this with Item State Description Metadata. All the examples I’ve found are showing Items with textual configuration.

What I need is a clear explanation. Do I put something in the Pattern, or in the Option, or both?

I tried putting the lines below in the Options but no luck.
[0.00…11.24]=N
[11.25…33.74]=NNE
[33.75…56.24]=NE
[56.25…78.74]=ENE
[78.75…101.24]=E
[101.25…123.74]=ESE
[123.75…146.24]=SE
[146.25…168.74]=SSE
[168.75…191.24]=S
[191.25…213.74]=SSW
[213.75…236.24]=SW
[236.25…258.74]=WSW
[258.75…281.24]=W
[281.25…303.74]=WNW
[303.75…326.24]=NW
[326.25…348.74]=NNW
[348.75…360.00]=N
NaN=Non-numeric state

Hi,
What you are asking is referred to as a conditional transformation rule.

You want to transform a range of numbers (integers) to be reflected as a text value (string)
Have a look at this documentation and examples

Put what you see in all the examples into the “pattern” field of state description without the and :%s. For example if you see [SCALE(uvindex.scale):%s] you’d put in SCALE(uvindex.scale).

Those need to go into a separate file under $OH_CONF/transformations (uvindex.scale in the example above) or you can define it through the UI at Settings → Transformations. And of course you need to install the Scale add-on.

@justaoldman thanks for the reference in the Docs. Unfortunately the docs only explain it with text examples with no relevance to the MainGUI.

@rlkoshak Thanks for the hint. When I created a Transformation of Type Scale I got this.
image

I guess I expecting a Wind_Direction.scale file to be created in the transform folder of Conf but there is nothing there.

It’s also not clear how to refer to it in the Pattern of the Item.
image

Clearly doesn’t work.

And yes, I installed the Scale Add-on.

This whole thing would benefit from some documentation. If it exists, I haven’t found it

No, nothing you do through the UI ends up in the $OH_CONF folder. This got saved to $OH_USERDATA/jsondb/org.openhab.core.transform.Transformation.json.

I’ve personally never worked with the Scale transform so don’t know if there is a trick or syntax error or something that needs to be dealt with.

Do you see any errors in the logs?

I think I may have steered you wrong above too. You do need the “:%s” part.

Scale(config:scale:Wind_Direction):%s

We do our best but there are definitely many holes in the docs. Volunteers to help are always most welcome.

Added the “:%s” and at now get an error.

Failed transforming the state '49 °' on item 'WeatherStation_WindDirection' with pattern 'Scale(config:scale:Wind_direction):%s': Couldn't transform value because transformation service of type 'Scale' is not available.

bundle:list shows it loaded

The examples on the add-on readme show all uppercase. SCALE instead of Scale.

changing to SCALE eliminates the error in the log.

The Item is still defined as a number:degrees.

Do I need some expression to format the output? And where to put it?

Where are you looking?

The State Description only changes how the Item’s state is rendered in a MainUI Widget. It doesn’t change the actual state of the Item and it certainly doesn’t change the type of the Item.

On the Settings ->Items page, you will always see the Item’s actual state.

On the individual Item’s page you should see the state as it’s transformed by the State Description pattern, but that itself could be overridden by a the “default stand-alone widget” if there is one on that Item.

On the MainUI Overview pages you should see the transformed state as well. But again, this could be overridden by the the “default list item widget” on the Item.

On sitemaps I’m not sure if it uses the state description pattern or not. But you would put the same information into the “label” field of the sitemap element (this time with the [ ]).

In events.log and in rules you will only ever see the actual Item’s state.

I was wrong, still have a log error but it’s different.

Failed transforming the state '59 °' on item 'WeatherStation_WindDirection' with pattern 'SCALE(config:scale:Wind_direction):%s': Could not find configuration 'config:scale:Wind_direction' or failed to parse it.

So I’m thinking it’s a syntax error?

For what it is worth.
Your example at begin of post did not have 2 valid periods separating the range.
If you create a flat file and give it a extension of .scale place it /conf/transform folder and you create a string item that is linked to the channel you are reading then you need nothing in state description or any where else all you need to do is on the item choose SCALE for profile and point to the flat file . you event logs will show your final transform and you will have a item that reflects the final transform everywhere you need it.
If you do all this through the MainUI then all the extra stuff you have been told are required.

I should have noticed this before. Your Item’s state is 40 °, not just 40. It carries units.

The docs do not mention support for units of measurement but it also doesn’t say they are not supported.

You have two options:

  1. Try the transform using the units.
[0.00 °..11.24 °]=N
[11.25 °..33.74 °]=NNE
[33.75 °..56.24 °]=NE
...
  1. Get rid of the unit by defining the Item as just a Number, not a Number:angle. In the UI that means setting the “Dimension” on the Item to nothing. The unit will be stripped from the value before the Item is updated.

If option 1 doesn’t work, please file an issue on openhab-addons to add support for UoM for the SCALE transform.

Having said all that, I’m not certain that’s the source of your error though. There might be something else wrong with the SCALE config.

Looking closely you have three dots between the numbers and the docs only show two:

[0.00…11.24]=N

compared to

]3..6]=2

I’m sure you note that the direction of the [ ] has meaning for the config as well.

Note, when posting configs, use code fences so the forum software doesn’t replace stuff to pretty it up, and spaces are preserved.

```
code goes here
```

They say pictures speak louder then words.
here is a picture using flat file and mainUI transform and sourcing a temperature sender channels to transform using your values. for both temperature without stripping and UOM and humidity which is a number value.


and log snippets.

2024-03-09 16:01:38.425 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item ‘eWeLink_TH03_Humidity_cale_example’ changed from ENE to E

Removing the dimension on the Item, it now shows in the log as an integer

Failed transforming the state '123' on item 'WeatherStation_WindDirection' with pattern 'SCALE(config:scale:Wind_direction):%s': Could not find configuration 'config:scale:Wind_direction' or failed to parse it.

And I edited my transformation using two dots separating integer values.

[0..11]=N
[12..33]=NNE
[34..56]=NE
[57..79]=ENE
[80..102]=E
[103..125]=ESE
[126..147]=SE
[148..170]=SSE
[171..191]=S
[192..214]=SSW
[215..237]=SW
[238..260]=WSW
[261..283]=W
[284..306]=WNW
[307..329]=NW
[330..352]=NNW
[353..360]=N
NaN=Non-numeric state

So I think the error is related to the syntax of the Pattern expression

value: " "
config:
  options: ""
  pattern: SCALE(config:scale:Wind_direction):%s

N or NE or any other letter is not a number you want a string item if you want to have text.


That is it all you need a string item linked to what ever channel you want if you create a flat file with a .scale extension. place it in the transform folder (it will show up in transformations in the mainUI as locked).

Finally gave in and created a .scale file :wink:

debug log shows it working

16:48:45.087 [DEBUG] [e.internal.ScaleTransformationService] - Transformation resulted in 'ESE'
16:48:51.608 [DEBUG] [e.internal.ScaleTransformationService] - Transformation resulted in 'ESE'

But my Item still displays the number

image

you have an item type of number letters are not a number. You need a item of type string. letters (text) are strings
To be clear everything @rlkoshak told you is correct, and it will work using MainUI approach just a lot more involved and even there you have to have a string item once you put the values in state description.
I just do flat files because it is easy and quick and gives me what i want in the event logs as well as on the GUI.

No you don’t. The State Description controls how the state of the Item is displayed but it doesn’t require any changes to the Item type.

All it does is control how the Item appears in the places I outlined above. It is perfectly acceptable to have transform like this that converts a number to a string for display purposes. State Description only controls the display.

If you want to use a String Item that’s doable. You’d just have to use a transform profile instead of the State Description to transform the value before it gets to the Item. But that’s not what OP is asking for.

Whether it’s managed configs or text files is irrelevant.

Ok, not questioning your expertise
So let me clarify it this way for me at least any time I wanted a item to display a string value I have had to use an item of the type string. for it to work correctly for me I am sure I must be doing it all wrong then.
fixed it :wink:

I have a Wind Direction item in text files that does this, and just duplicated it in the UI.

  1. Under Settings → Transformations, added a scale transformation called compass_direction.
  2. Copied in the contents of my existing text file.
[..22.5]=North
]22.5..67.5]=Northeast
]67.5..112.5]=East
]112.5..157.5]=Southeast
]157.5..202.5]=South
]202.5..247.5]=Southwest
]247.5..292.5]=West
]292.5..337.5]=Northwest
]337.5..]=North
  1. Added a Number:Angle item. I tried adding the scale to the State Description Pattern when creating the item, but this did not work.

  1. Added State Description metadata to the item: SCALE(config:scale:compass_direction):%s

That’s all it took. The steps are exactly the same in the UI as they are in text files–you just need to know how to point to the transformation patterns, Unfortunately, the UI method is missing from the docs.

Admittedly, it feels like the UI isn’t fully baked since we have to rely on textual config in the state description. It would be more intuitive if we could choose the type of transformation (e.g. scale, map) and then choose from the available transformations.

@rlkoshak, perhaps this would be a worthwhile feature request?