How do I convert numeric miles data to "km" in a binding?

I’m adding an enhancement to the tesla binding to report distance to travel to the destination.

The distance is reported by the upstream API as miles, though. I would like to properly annotate this with an unit and keep also the SI unit (kilometers in this case) in the item. The channel configuration is as follows

		<channels>
			<channel id="ar_destination" typeId="ar_destination"/>
			...
		</channels>

	...
	<channel-type id="ar_distancetoarrival">
		<item-type>Number</item-type>
		<label>Distance to arrival</label>
		<description>Distance to drive to the destination</description>
		<state pattern="%.1f %unit%" readOnly="true"></state>
	</channel-type>
	...

How would I go about converting the incoming data? Simply multiplying the number 1.6 does not feel like the proper way to go.

For the record - here is the full pull request:

Regards,
Hakan

Binding can report data as-is, the conversion is done above it. It means that all you need to do is pushing QuantityType using miles. Then, user can define which unit he wants to use through system configuration.

ah cool, this makes things even easier.

May be off-topic in this thread but there seem to be knowledgabe people here :wink:

The Tesla binding gives all values in miles and miles/h. How to convert/display them as km and km/h in a .sitemap?
Thanks!

If I understand the inner workings of the binding correctly, you would need to set your preferences in “System” / “Settings” / “System Settings” and klick on “Advanced” on top right.

I’m actually debugging whether my enhancement will work correctly with the approach above, so take this information with a pinch of salt :slight_smile:

Regards,
Hakan

I am pretty sure it will work. Critical part is use of quantity types to report states.
If binding uses raw decimal without unit information (units placed in state description) it will fail.

Good hint actually. After the current PR is merged, I will go over the existing code and see in which places quantity information might need to be added.

Regards,
Hakan