Restart/Shutdown openHAB 3 using ECMAScript 2021

  • Platform information:
    • Hardware: Raspberry Pi 3 B
    • OS: openHABian
    • openHAB version:3.3.0 M5

Beginner’s guide to an openHABian reboot/shutdown switch - Tutorials & Examples / Solutions - openHAB Community

Hi all,

I’ve implemented the reboot/shutdown logic mentioned in the above guide. I’m able to successfully restart openHAB with the openhab user via SSH.

However, I’m having difficulties restarting using rules.

Now, my logs show the following error for either reboot or shutdown rules (notice the last line):

Restart:

2022-05-18 15:55:43.138 [INFO ] [utomation.script.file.maintenance.js] - {
  "oldState": "NULL",
  "newState": "Reboot",
  "state": "null",
  "receivedCommand": null,
  "receivedState": null,
  "receivedTrigger": null,
  "itemName": "openHABMaintenance_Action",
  "module": "c8152a90-7315-40f6-b562-bb1a8a07e648",
  "payload": {
    "type": "String",
    "value": "Reboot",
    "oldType": "String",
    "oldValue": "NULL"
  },
  "eventType": "change",
  "triggerType": "ItemStateChangeTrigger"
} "New State:" "Reboot"
2022-05-18 15:55:43.142 [INFO ] [utomation.script.openhab.maintenance] - Rebooting openHAB
2022-05-18 15:55:43.150 [ERROR] [e.automation.internal.RuleEngineImpl] - Failed to execute rule 'openHABMaintenance': Fail to execute action: 1

Shutdown:

2022-05-18 15:56:21.488 [INFO ] [utomation.script.file.maintenance.js] - {
  "oldState": "NULL",
  "newState": "PowerOff",
  "state": "null",
  "receivedCommand": null,
  "receivedState": null,
  "receivedTrigger": null,
  "itemName": "openHABMaintenance_Action",
  "module": "c8152a90-7315-40f6-b562-bb1a8a07e648",
  "payload": {
    "type": "String",
    "value": "PowerOff",
    "oldType": "String",
    "oldValue": "NULL"
  },
  "eventType": "change",
  "triggerType": "ItemStateChangeTrigger"
} "New State:" "PowerOff"
2022-05-18 15:56:21.493 [INFO ] [utomation.script.openhab.maintenance] - Shutting Down openHAB
2022-05-18 15:56:21.501 [ERROR] [e.automation.internal.RuleEngineImpl] - Failed to execute rule 'openHABMaintenance': Fail to execute action: 1

The rule commands are:

executeCommandLine("sudo","reboot");
executeCommandLine("sudo","poweroff");

Any help would be greatly appreciated :slight_smile:

Many thanks in advanced!

Jeevs :slight_smile:

Hi Jeevs

Please forgive me if I’m wrong. But for me a restart of openHAB is a different thing than reboot.
Are you able to issue a “sudo reboot” under openhab user?
And what is shown on the shell (i.e. session should get closed after a while)?

Stefan

Have you tried using RulesDSL actions? The original rules in the tutorial are RulesDSL, and I don’t know if these commands work in ECMAScript (which I know nothing about). You didn’t mention ECMA when you messaged me directly, so it didn’t occur to me to suggest this.

Hi Stefan,

If I use the following commands (based on the guide) in SSH openHAB does restart. Please forgive me if that is different from a reboot.

sudo -u openhab /bin/bash
sudo shutdown -r

Thanks

Apologies I thought I had mentioned it. Though, I’ve made a conscious decision to move away from RulesDSL. I’m happy to create a temporary RulesDSL file to ensure that this isn’t a permission error :slight_smile:

Hi Jeewan

Ok so that command works correctly.

The defined rules as we can see from the logs are triggered based on your activity.
The missing point is now. How the Rule issues the command to reboot (i.e. the action).

I suspect it would look like

** This is nothing real just an empty trial.
As in both cases your rule reports “Fail to execute action: 1”, i suspect the error here. Please provide the details in order to figure it out.

Regards
Stefan

Hi Stefan,

Thanks for this, so my complete rule is as follows:

// OH Maintenance
rules.JSRule({
    id: "openHABMaintenance",
    name: "openHAB Maintenance",
    description: "A rule to either restart or shut down the openHAB service",
    triggers: [triggers.ItemStateChangeTrigger("openHABMaintenance_Action")],
    execute: event => {
        
        // Declare all variables
        const logger = log("openHAB.Maintenance");

        logger.debug("openHAB Maintenance - Start");
        console.info(event, "New State:", event.newState);

        if(event.newState == "Reboot") {
            logger.info("Rebooting openHAB");
            executeCommandLine("sudo", "shutdown", "-r", "now");
        } else if (event.newState == "PowerOff") {
            logger.info("Shutting Down openHAB");
            executeCommandLine("sudo","poweroff");
        }
        
        logger.debug("openHAB Maintenance - End");
    }
});

As always your help if greatly appreciated! :slight_smile:

Many thanks,

Jeewan

Jeewan

I’m not 100% sure, but I think the: executeCommandLine itself is the result for the error. If you compare for instance here:

In your script you have four “text strings” that you give.
If i’m a dumb robot I connect this to: “sudoshutdown-rnow” and for sure I would reply, hmm I don’t understand.

Hope that helps :slight_smile:

1 Like

Hi Stefan,

You are absolutely correct it is in fact the executeCommandLine that is the result of the error.

I’m essentially trying to get openHAB to restart/shutdown based on a item that I have created. This logic is all based on this guide: Beginner’s guide to an openHABian reboot/shutdown switch - Tutorials & Examples / Solutions - openHAB Community

The only difference being that I’m attempting to use ECMAScript 2021 instead of RuleDSL…

Thanks,

@Jeevs

This works for me

actions.Exec.executeCommandLine(time.Duration.ofSeconds(1), 'sudo', 'reboot');

as does

let myAction = "sudo shutdown"
actions.Exec.executeCommandLine(time.Duration.ofSeconds(1), myAction);
1 Like

Works like an absolute charm - thanks so much @Tougharms!

As you can see was waiting for a resolution for a while :smiley:

Thanks once again for coming back and updating the thread :slight_smile: Very kind indeed!

@Jeevs You are welcome. I hate it when threads are left hanging with no solution. It took me about 5 days to work this out, the online documentation and forum all mix old ECMA and ECMA2021.

Another piece of advice I found for using actions.Exec.executeCommandLine
is to write your command in a short script eg “myscript.sh” in /home/openhabian then call the script with actions.Exec.executeCommandLine(duration, "/home/openhabian/myscript.sh")
Dont forget to make the script executable!

1 Like

Could not agree more @Tougharms!

Oh wow - that is certainly impressive, may well have to update the rule :smiley:

Thanks once again :slight_smile:

p.s. already marked your previous post as the solution :slight_smile: