Restart Tradfri Gateway from rule or Binding

When a Tradfri Bulb gets disconnected from the power line (by pressing the wall switch), it randomly happens, that the gateway itself looses connection to the bulbs. So the bulb isn’t controllable through openhab anymore. It remains in its last state and commands aren’t passed to the bulb anymore (all without any error in the logs). Restarting the binding didn’t resolve the problem, but restarting the gateway fixes it.

Google told me that it is possible to restart the gateway over the network:

REBOOT:
coap-client -m post -u “Client_identity” -k " key " “coaps://192.168.178.71:5684/15011/9030”

Is it possible to execute the coap command with some tools of openhab (script, http binding, or whatever)?
Or even better: it is possible to add a channel to the binding that provides a switch to restart the gateway?
So it would by possible to restart the gateway every night, to revivify all bulbs that might got lost over the day.

Thanks in advance!

rule "Restart Tradfi gateway"
when
    Time cron "15 0 0 ? * * *" // 15 seconds after midnight
then
    executeCommandLine("coap-client -m post -u 'Client_identity' -k ' key ' 'coaps://192.168.178.71:5684/15011/9030'")
end
2 Likes

Thank you for your answer. I’m nearly there… This is what I’ve done so far:

  1. Installed the coap-client on my raspberry, following this guide.
  2. Created a new DTLS Identity for accessing the gateway, following this guide.

Now I’m able to restart the Gateway from the command line:

 **16:22:53]** **openhabian@openHABianPi** : **~** $ coap-client -m post -u 'openhab_commandline' -k 'supersafekey' 'coaps://192.164.158.70:5684/15011/9030'
v:1 t:CON c:POST i:219a {} [ ]
decrypt_verify(): found 24 bytes cleartext
decrypt_verify(): found 4 bytes cleartext

But when i use it in a rule

rule "Restart Tradfi gateway"
when
    Time cron "0 15 17 1/1 * ? *"
then
    var String result = executeCommandLine("coap-client -m post -u 'openhab_commandline' -k 'supersafekey' 'coaps://192.164.158.70:5684/15011/9030'")
    logInfo("Test", result)
end

nothing happens, the log says:

2018-11-13 17:15:02.381 [INFO ] [lipse.smarthome.io.net.exec.ExecUtil] - executed commandLine 'coap-client -m post -u 'openhab_commandline' -k 'supersafekey' 'coaps://192.164.158.70:5684/15011/9030''
2018-11-13 17:15:02.387 [INFO ] [.eclipse.smarthome.model.script.Test] - null

I tried allowing openhab to execute it as root by adding following to /etc/sudoers

openhab ALL=(root) NOPASSWD: /usr/local/bin/coap-client

But that doesn’t help. Any ideas?

Edit: fixed it:

rule "Restart Tradfi gateway"
when
    Time cron "0 39 17 1/1 * ? *"
then
    var String result = executeCommandLine("coap-client -m post -u 'openhab_commandline' -k 'supersafekey' 'coaps://192.164.158.70:5684/15011/9030'", 5000)
    logInfo("Test", result)
end

results in a restart of the gateway and this output:

2018-11-13 17:39:02.169 [INFO ] [.eclipse.smarthome.model.script.Test] - v:1 t:CON c:POST i:fbcf {} [ ]
decrypt_verify(): found 24 bytes cleartext
decrypt_verify(): found 4 bytes cleartext

Does anybody have a similar solution on a Windows 10 openHAB server?
I have looked for a similar coap-client executable, tried to make it work many months ago, but was unsuccessful.
Thanks in advance!

I’d suggest just putting the Tradfri on a smart plug and toggling it on a schedule. Less elegant, but easier to set up and maintain in the long run.

1 Like