'Distance from' with Javascript

I’m migrating some DSL rules to javascript but can’t figure out the equivalent of

locationA.distanceFrom(locationB)

Can anyone help?

What does locationA and locationB come from? Item states? Local/global variables? cache?

states of Location items. Although I may want to use the other options you mention later.

This works fine in DSL, I’m looking for the javascript equivalent

Always refer to the reference docs for stuff like this. The JS Scripting docs are really good and complete.

In this particular case, it’s important to notice what Item.state returns a String. It didn’t matter what the item type is, it’s always a String. There is no distanceFrom on a String.

Also notice that Item.state is not the only way to get the state of an Item. There is Item.numericState (returns the state as a JS number you can do math with), Item.quantityState (returns the state as a Quantity with units to do math with UoM), and Item.rawState which returns the Java State Object.

It’s that last one you want because it’s the Java State Object that provides distanceFrom.

Thanks.

I did spend a lot of time with the docs, but couldn’t figure it out.

This is what got it working for me:

const PointType = Java.type('org.openhab.core.library.types.PointType')
const locationA = new PointType("58.345,-8.432")
const locationB = new PointType(event.newState)
const distance = locationA.distanceFrom(locationB)