How can I unlink and relink items to channels using a script

Hi I have a Shelly relay used to control an outside socket.

In Winter, I use the socket to control christmas lights, and in sping/summer I use it to control a water feature.
I use a switch item (christmas_lights) to switch on at dusk and off at sunrise.

I use a seperate switch item for the water feature This is turned on when required and switches off (expires) after two hours

At the moment I have to unlink the christmas_lights item and link the water_feature item manually when the lights are no longer required (On 05 January )

Is there a way link and unlink an item to a thing using a script - My plan is to run a script via cron on 1st December unlinking water_feature and linking the shelly relay to christmas_lights, and then running again on 5th January to unlink Christmas _lights and relink water_feature


First of all, think outside the box. Instead of unlinking/relinking why not enable/disable the rules themselves? For example, I have a set of smart plugs that get used for Christmas lights during that time of year and drive humidifiers for the rest of the year. When Christmas time comes around I flip a Switch that disables the humidifier rule and enables the Christmas lights rule. At the end of the season, flip the Switch to OFF and the Christmas lights rule is disabled and the humidifier rule is reenabled.

So instead of messing with the links you can control the behaviors instead. That certainly would be easier, you don’t even need to write code. It can all be done with basic UI rules.

configuration: {}
  - id: "1"
      itemName: TisTheSeason
      state: OFF
    type: core.ItemStateChangeTrigger
conditions: []
  - inputs: {}
    id: "2"
      enable: false
        - christmas_lights
    type: core.RuleEnablementAction
  - id: "3"
      enable: "false"
        - humidifiers
    type: core.RuleEnablementAction

There’d be a corresponding rule for when TisTheSeason changes to ON.

Links are not typically exposed in rules so if you can’t control this via the rules, you will either have to use the REST API and sendHttpXRequest Actions or the Exec binding or executeCommandLine Action to manage the links through the Karaf Console.

Thanks for the quick reply.
I haven’t looked at rules in openhab - I use node- red flows but I guess the same thing would work here

The switches are named for use with Alexa, so having standalone switch items with rules to change the relay based on a christmas_season switch would be perfect

Thanks again!