Simple-sleep-wait-example-with-ecma-script

Hello Rich…

here are some extract from the logfile:

2023-05-01 17:02:29.206 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'WeihnachtsbeleuchtungInnen_Betrieb' received command ON

2023-05-01 17:02:29.208 [INFO ] [penhab.event.ItemStatePredictedEvent] - Item 'WeihnachtsbeleuchtungInnen_Betrieb' predicted to become ON

2023-05-01 17:02:29.214 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'WeihnachtsbeleuchtungInnen_Betrieb' changed from OFF to ON


2023-05-01 17:02:29.224 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'a52a367337' failed: <eval>:6:85 Expected an operand but found )


2023-05-01 17:02:32.471 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'WeihnachtsbeleuchtungInnen_Leistung' changed from 0 W to 10.2 W

2023-05-01 17:02:42.780 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'WeihnachtsbeleuchtungInnen_Leistung' changed from 10.2 W to 10.63 W

2023-05-01 17:02:47.494 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'WeihnachtsbeleuchtungInnen_Leistung' changed from 10.63 W to 10.56 W

2023-05-01 17:02:57.474 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'WeihnachtsbeleuchtungInnen_Leistung' changed from 10.56 W to 0 W


2023-05-01 17:04:58.485 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'WeihnachtsbeleuchtungInnen_LetzteAktualisierung' changed from 2023-05-01T17:04:44.000+0200 to 2023-05-01T17:04:59.000+0200

2023-05-01 17:05:12.492 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'WeihnachtsbeleuchtungInnen_LetzteAktivitat' changed from 2023-05-01T17:04:58.000+0200 to 2023-05-01T17:05:12.000+0200

2023-05-01 17:05:13.518 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'WeihnachtsbeleuchtungInnen_LetzteAktivitat' changed from 2023-05-01T17:05:12.000+0200 to 2023-05-01T17:05:13.000+0200

2023-05-01 17:05:13.523 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'WeihnachtsbeleuchtungInnen_Laufzeit' changed from 34842 s to 34857 s

2023-05-01 17:05:13.528 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'WeihnachtsbeleuchtungInnen_LetzteAktualisierung' changed from 2023-05-01T17:04:59.000+0200 to 2023-05-01T17:05:14.000+0200

Section1: Switch the plug on by “hand”
Section2: Directly it seems again an error (I search for the ruleID)
Section3: Power goes up. And after 20 sec I removed my mobile from the plug and the power come done to 0W
Section4: The Plug is still alive, but it not sending anymore the value about the power, because it is not change, because the mobile is unplugged.

Here is the current script and a screenshot from the rule.

var currState = Quantity(event.itemState.toString());
var threshold = Quantity('1.5 W');

// If the current state < 1.5 W create the timer if it doesn't exist
if(currState.lessThan(threshold) && cache.private.get('timer') === null) {
  cache.private.put('timer', actions.ScriptExecution.createTimer(time.toZDT(120000), () => {
    console.log('switch power off');
    items.WeihnachtsbeleuchtungInnen_Betrieb.sendCommand('OFF');
  }  
}  
// If the current state is above the threshold and the timer exists, cancel the timer
else if(cache.private.get('timer') !== null) {
  cache.private.get('timer').cancel();
}

I assune that the trigger to start the rule shoud be the change from the status from OFF to ON, I’m right?

BR
Jochen

Screen shots are usually all but useless. Click on the “Code” tab and paste the contents of the text into a post, not the screen shot.

However, in this case it does show what’s wrong.

This rule does not care when the switch turned ON. So why trigger it on that event? As I said above, the rule needs to be triggered based on WeihnachtsbeleuchtungInnen_Leistung changes.

The rule triggers when a new W is reported. Then the rule does what it needs to do based on that W.

Hello Rich, Thanks.

I changed now the rule, that the “trigger” is only, when the "WeihnachtsbeleuchtungInnen_Leistung changed.
Thanks for that.

But I still have in the logfile this error:

2023-05-01 17:41:45.650 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'a52a367337' failed: <eval>:6:85 Expected an operand but found )

  cache.private.put('timer', actions.ScriptExecution.createTimer(time.toZDT(120000), () => {

                                                                                     ^ in <eval> at line number 6 at column number 85


If you are more comfortable in Blockly, this is definitely possible in Blockly too (*any of the rules languages can do this really). There is nothing special about this code. It should be straight forward to translate to Blockly.

  1. if the timer doesn’t exist and the current state is less than 1.5 W
    a. create a timer for two minutes that turn off the outlet
  2. else if the timer does exist
    b. cancel the timer
  3. else (i.e. timer doesn’t exist and current is greater than equal to 1.5 W)
    c. do nothing

I say this because if you plan on keeping this rule, you should at least understand the basics on how JS works and be able to do some basic debugging on your own. Otherwise this rule will not be maintainable by you in the long run.

No matter, the problem appears to be a typo. It’s missing a couple of closing parens.

var currState = Quantity(event.itemState.toString());
var threshold = Quantity('1.5 W');

// If the current state < 1.5 W create the timer if it doesn't exist
if(currState.lessThan(threshold) && cache.private.get('timer') === null) {
  cache.private.put('timer', actions.ScriptExecution.createTimer(time.toZDT(120000), () => {
     console.log('switch power off');
     items.WeihnachtsbeleuchtungInnen_Betrieb.sendCommand('OFF');
   }));  
}  
// If the current state is above the threshold and the timer exists, cancel the timer
else if(cache.private.get('timer') !== null) {
  cache.private.get('timer').cancel();
}

Hello Rich,

have you now corrected the script with the “closing parens.”??
I have copied you last feedback to the script, and have the same issue.

As said before, I have no idea how to develop.
But I you feel nice to make a Blockly for me, this would be also OK. At least a screenshot of it…
Thanks in advanced
BR
Jochen

Why should Rich do your work?
Give Blockly a try on your own to see how it works.

If you want me to do it, it’ll be a few weeks. My OH is completely dead and needs to be rebuilt from scratch and that’s definitely going take precidence.

But the above code is very simple. I also provided an outline in prose of what the Blockly code needs to do. You should be able to easily match up the names of the blocks with the words in that outline.

I’m hesitant to spend much more time on this because in the end you will have to maintain it. I’m not signing up to maintain this rule for you forever and no one else on this forum is either. If you can’t do the simplest of debugging on your own, you won’t be able to maintain it in the long run.

But, if you build the rule in Blockly and run into problems, I’m willing to help you figure out the problems.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.