Channel from xml file behaves different than channel created from code

Hi,

I am developing an addon and have a problem with channels and their properties:

If I link a channel, that was defined in the XML file, to a new item, openHab populates “Type”, “Dimension” and “State Description Pattern” of the item with the proper values from the XML file.

If I do the same on a channel, that was created at runtime via editThing(), openHab tries to create a “Switch” item instead of “Number:ElectricPotential” and the state pattern also stays empty.

What confuses me, is that both channels use the same ChannelType. And openHab reads the “label” from the ChannelType, but not the other information.

This is the relevant part of the thing description:

	<thing-type id="sunspec-mppt">
		<channels>
			<channel id="mppt-dc-voltage" typeId="mppt-dc-voltage-type"/>
		</channels>

with this channel type configuration:

	<channel-type id="mppt-dc-voltage-type">
		<item-type>Number:ElectricPotential</item-type>
		<label>DC Voltage</label>
		<state readOnly="true" pattern="%.1f %unit%"/>
	</channel-type>

and here is the code, with which I am creating a new channel, which references the same ChannelType, but still behaves differently:

ChannelBuilder.create(new ChannelUID(getThing().getUID(), "module-" + moduleNumber, "mppt-dc-voltage"))
    .withType(new ChannelTypeUID(BINDING_ID, "mppt-dc-voltage-type")).build());

The upper channel is from the XML file, the lower created by the code above:

Linking the first one the a new item:

Linking the second one to a new item:

What am I missing?

Can you request the thing from the REST API and share the response? I have the feeling that the item-type is not properly populated in the case of the programatically configured channel. Is the thing itself configured in UI or in a .things file? Does the situation improve if you add .withAcceptedItemType("Number:ElextricPotential") to the channel builder?

The thing is configured via the UI. It is indeed not populated properly:

{
  "channels": [
    {
      "linkedItems": [],
      "uid": "modbus:sunspec-mppt:bridge2:948eda58ea:mppt-dc-voltage",
      "id": "mppt-dc-voltage",
      "channelTypeUID": "modbus:mppt-dc-voltage-type",
      "itemType": "Number:ElectricPotential",
      "kind": "STATE",
      "label": "DC Voltage",
      "defaultTags": [],
      "properties": {},
      "configuration": {}
    },
    {
      "linkedItems": [],
      "uid": "modbus:sunspec-mppt:bridge2:948eda58ea:module-1#mppt-dc-voltage",
      "id": "module-1#mppt-dc-voltage",
      "channelTypeUID": "modbus:mppt-dc-voltage-type",
      "kind": "STATE",
      "defaultTags": [],
      "properties": {},
      "configuration": {}
    }
  ],
  "statusInfo": {
    "status": "ONLINE",
    "statusDetail": "NONE"
  },
  "editable": true,
  "label": "Multiple MPPT Inverter Extension",
  "bridgeUID": "modbus:tcp:bridge2",
  "configuration": {
    "length": 68,
    "refresh": 5,
    "maxTries": 3,
    "address": 40239
  },
  "properties": {
    "uniqueAddress": "modbus:tcp:bridge2:40239"
  },
  "UID": "modbus:sunspec-mppt:bridge2:948eda58ea",
  "thingTypeUID": "modbus:sunspec-mppt"
}

Interesting: The second channel (the one created by code) does not even have a “label”, although the UI displays “DC Voltage”.

With .withAcceptedItemType():

        thingBuilder.withChannel(
                ChannelBuilder.create(new ChannelUID(getThing().getUID(), "module-" + moduleNumber, "mppt-dc-voltage"))
                        .withType(new ChannelTypeUID(BINDING_ID, "mppt-dc-voltage-type"))
                        .withAcceptedItemType("Number:ElectricPotential").build());

does not change anything (I deleted and recreated the thing ever time).

Sorry: My test was invalid, I forgot one “mvn clean” between the tests. With .withAcceptedItemType("Number:ElectricPotential") does change the behavior. Now the item gets created with the same information as from the channel, which was defined in XML.

Here the information from the REST API with .withAcceptedItemType("Number:ElectricPotential") added:

{
  "channels": [
    {
      "linkedItems": [],
      "uid": "modbus:sunspec-mppt:bridge2:3a87339a9e:mppt-dc-voltage",
      "id": "mppt-dc-voltage",
      "channelTypeUID": "modbus:mppt-dc-voltage-type",
      "itemType": "Number:ElectricPotential",
      "kind": "STATE",
      "label": "DC Voltage",
      "defaultTags": [],
      "properties": {},
      "configuration": {}
    },
    {
      "linkedItems": [],
      "uid": "modbus:sunspec-mppt:bridge2:3a87339a9e:module-0#mppt-dc-voltage",
      "id": "module-0#mppt-dc-voltage",
      "channelTypeUID": "modbus:mppt-dc-voltage-type",
      "itemType": "Number:ElectricPotential",
      "kind": "STATE",
      "defaultTags": [],
      "properties": {},
      "configuration": {}
    }
  ],
  "statusInfo": {
    "status": "ONLINE",
    "statusDetail": "NONE"
  },
  "editable": true,
  "label": "Multiple MPPT Inverter Extension",
  "bridgeUID": "modbus:tcp:bridge2",
  "configuration": {
    "length": 68,
    "refresh": 5,
    "maxTries": 3,
    "address": 40239
  },
  "properties": {
    "uniqueAddress": "modbus:tcp:bridge2:40239"
  },
  "UID": "modbus:sunspec-mppt:bridge2:3a87339a9e",
  "thingTypeUID": "modbus:sunspec-mppt"
}

The “label” is still missing, but openHab displays the channel with the label “DC Voltage”.

I hope my outputs are not confusing: The channel without group is always the one which is defined in the xml file, the channel with “module-1” or “module-0” is the one defined programmatically.