Ensuring "CoAP Transport" is installed after openHAB upgrade

I know it’s frowned upon to have some sort of automated updates, but my Linux knowledge is very limited, so I would just install any update regardless. So whether it’s me updating everything every day, or a script, the result is the same. I’ve also never had issues.

That is, except for the fact that if openHAB is upgraded, “CoAP Transport” is uninstalled (or not installed again in the new version - that’s maybe more accurate). My openHAB system needs “CoAP Transport” for the Shelly binding (since I use the jar file, because it’s more up-to-date than the one in the marketplace). Because I run my automated updates during the night, I discover the non-working Shelly binding in the morning, which means some of my lights don’t turn on as expected.

So I created a rule that automatically installs “CoAP Transport” if it’s not installed. It runs after startup has been completed (“start level 100”). This is the rule code:

var coapinstalled = actions.Exec.executeCommandLine(time.Duration.ofSeconds(300), "/bin/bash", 'bin/bashscripts/openhabcoap.sh')
// Geen idee of 300 seconden genoeg is...


// Pogingen om "sudo" toe te laten:
//var coapinstalled = actions.Exec.executeCommandLine(time.Duration.ofSeconds(300), "/bin/bash", 'sudo', 'bin/bashscripts/openhabcoap.sh')
//var coapinstalled = actions.Exec.executeCommandLine(time.Duration.ofSeconds(300), "/usr/bin/sudo", 'bin/bashscripts/openhabcoap.sh')
//var coapinstalled = actions.Exec.executeCommandLine(time.Duration.ofSeconds(300), "sudo", 'bin/bashscripts/openhabcoap.sh')
//var coapinstalled = actions.Exec.executeCommandLine(time.Duration.ofSeconds(300), "/usr/bin/sudo", '/var/lib/openhab/bin/bashscripts/openhabcoap.sh')

// Uiteindelijk heb ik "openhab ALL=(ALL) NOPASSWD: /bin/systemctl restart openhab.service" toegevoegd aan "/etc/sudoers.d/1openhabsudo"

// Locatie: "/var/lib/openhab/bin/bashscripts/"
//console.log("coapinstalled = "+coapinstalled)

This is /var/lib/openhab/bin/bashscripts/openhabcoap.sh:

#!/bin/bash

UPGRADE=$(openhab-cli console -p habopen "feature:list -i | grep openhab-transport-coap")
TEZOEKENTEKST="openhab-transport-coap"

if [[ "$UPGRADE" =~ "$TEZOEKENTEKST" ]]
then
        echo "was al geïnstalleerd"
else
        openhab-cli console -p habopen feature:install openhab-transport-coap
        echo "net geïnstalleerd"
        sudo systemctl restart openhab.service
        echo "opnieuw opgestart"
fi

(The echo parts are useless of course, I plan to replace them by mail commands.)

I had to do some fiddling with the Linux part of it all, since user openhab wasn’t allowed to do sudo stuff. So, as the comment states, I created /etc/sudoers.d/1openhabsudo and added the following to it:

openhab ALL=(ALL) NOPASSWD: /bin/systemctl restart openhab.service

It now works, but I assume there are tweaks that could be made to improve it… If anyone has any, feedback is welcome!

Maybe you can use a script as execStartPre from the openHAB service.
Then a script will be started before openhab gets started.
I use this to rename my rules files before start of openhab
Greets

You can install coap services with this command from command line interface:

/usr/bin/ssh -p 8101 openhab@localhost feature:install openhab-transport-coap

How ever this requires to enter a password. There is a way to overcome this and there are instructions for it. It is not that easy.

I solved the problem by simply installing the tradfri release binding additionally as this requires also coap services.

This sounds like the best solution :smile::smile:

1 Like

:smile::smile:

You mean that since the Trådfri binding requires Coap, and it’s a binding in the add-on store, it makes sure Coap is installed after an openHAB upgrade?

But what would it have to do with cleaning the cache (as you referenced here)?

Correct.
And as long it is the tradfri release binding from official addons store it does not get uninstalled during update or clean cache as well as the coap service.

Edit

To be more precise, coap does not get uninstalled in this scenario

Gotcha.

But do you think that was what caused openHAB to fail its startup?

Very likely, yes. Maybe openhab tried to restart itself and got stuck in a loop. Happened to me, too.
To me, the better solution is to force coap being installed by this tradfri binding so that I am flexible and more safe when using Shelly snapshot binding