I am trying to update the linked items for channels through the REST API using the PUT method. Below is a basic template of data being sent - ignore all the placeholder strings, everything is filled in correctly in the curl command, but you probably don’t want to read 12kb of text.
I can successfully execute the command without error, and also update fields like the labels, so I know that the command is being processed on the OH side.
The important part is that the ‘linkedItems’ array under the channels key. I’ve double checked that the correct item names are in the array, however the command wont remove existing channel links that are no longer in the array, nor add the new items in the array.
Is that the correct behaviour, or is there another way to make the updates?
That is the correct behavior. The thing endpoint is just reporting what links exist with its channels, it is not controlling them. You can modify links via that API, but the thing endpoint is not where you do that. There is a dedicated link endpoint that will allow you to link and un-link items with channels.
OK cool, I’ll just have to write some more scripting to send via the links api.
I have to send stuff via the REST API as I need to do bulk updating. I have multiple instances of a thing, each with about 30 channel links, associated items and rules. All my rules are script based. Using the REST API, I only need to modify 6 script prototypes which are then generated into 6x2x8 = 96 rules currently. (Just for the one thing template!)
In jruby this can be done without going through the REST API. The syntax is hopefully quite intuitive.
Example code:
# Apply the operations in this script to the Managed Items / Links
provider!(:persistent)
# Get all the links of an item
MyItem.links
# If it only has one link, a convenience method:
MyItem.link # This gives you an ItemChannelLink object
# Get all the links to a channel
things["mqtt:topic:mosquitto:mything"].channels["mychannel"].links
# if it only has one link, you can use:
things["mqtt:topic:mosquitto:mything"].channels["mychannel"].link
# To unlink an item from a channel:
MyItem.unlink("mqtt:topic:mosquitto:mything:mychannel")
# Or to unlink a channel from an item:
things["mqtt:topic:mosquitto:mything"].channels["mychannel"].unlink(MyItem)
# To clear ALL links from an item:
MyItem.links.clear
# To clear ALL links from a channel:
things["mqtt:topic:mosquitto:mything"].channels["mychannel"].links.clear
# To link an item to a channel:
MyItem.link("mqtt:topic:mosquitto:mything:mychannel")
# Add a link with a profile
MyItem.link("mqtt:topic:mosquitto:mything:mychannel", profile: "JSONPATH", function: "$.status.power")
# To link a channel to an item:
things["mqtt:topic:mosquitto:mything"].channels["mychannel"].link(MyItem)
Thanks, in future I might look towards managing things internally in OH as things develop. At the moment I’m generating a thing, associated items and scripts all as an instance of a class essentially. It’s a bit clunky through python externally, but does work fairly easily for small changes. For example the script templates are in external files, just change the file, rebuild the changes and update the rules through REST
The ideal way would be if OH recognised a collection of things/items/scripts so they can be updated, but that’s probably something for OH 6 or 7 onwards.