ChannelType object from Channel object?

Looking for a code example that shows how to use a Channel object to get the Channel’s ChannelType attributes.

e.g. having the following defined thing-types.xml:

	<channel-type id="minTemperature">
		<label>Minimum Temperature</label>
		<description>The low temperature</description>
		<state readOnly="true" pattern="%.1f %unit%" />

How do I extract the “category” value? I assume I have to get a ChannelType object, but the Channel object has neither a “getType()” method nor a “getChannelType()” method.


You will get of these things on an item linked to that channel.

Hi @vzorglub, thanks for the reply!

I’m afraid I don’t follow, however, The Channel object doesn’t have any methods that return an Item.

I am afraid that is not possible. Those information are stored in the ChannelTypeRegistry which is not accessible from a binding.

What is your purpose to get the “category” of a channel type? Maybe there is a different way to reach your goal.

1 Like

I’ve used some code to get the ChannelType from the ChannelTypeRegistry in a test bundle. Though I still have plans to refactor it and remove such dependencies to make those tests consume less ESH interfaces. Such code shouldn’t be used in bindings.

Seemed like a natural way of doing UoM updates:

if( myChannel.getChannelType().getCategory().equals("Temperature") ) {
     QuantityType<Temperature> state = new QuantityType<>(tempDouble, SIUnits.CELSIUS);
     updateState(temperatureChannelUID, state);

Since the categories are already defined, it doesn’t make sense to have to create them again myself in a new mapping.

What about this? Never tried it. Just a guess.

if( myChannel.getAcceptedItemType().equals("Number:Temperature") ) {
    // do stuff
1 Like

That looks like it’ll work nicely. I’ll experiment with it.