If ... then ... else rule is not working as expected

Hi,
I try to make a rule to prevent that I use to much of my electrical power.

Meaning:

I need to prevent that I use both my draying machine and a close-in-boiler at the same time.

I use for both a powerplug, For the dryer to measure the power and for the Close-in-Boiler to switch it on and off.

I created a rule bot it seems that the rule runs both “if” statements. This results in a quick switching of the Close-in Boiler (on-off …… on – off).

I can’t find what is wrong. Who can help me out?

configuration: {}
triggers:
  - id: "1"
    configuration:
      itemName: AchterbouwDrogerN018_Electricmeterwatts
    type: core.ItemStateChangeTrigger
  - id: "3"
    configuration:
      itemName: AchterbouwDrogerN018_Electricmeterwatts
    type: core.ItemStateUpdateTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      type: application/javascript
      script: >
        
        var logger = Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.' + ctx.ruleUID); var thread = Java.type('java.lang.Thread')

        if (itemRegistry.getItem('AchterbouwDrogerN018_Electricmeterwatts').getState() > '500') {
          events.sendCommand('KeukenBoilerN005_Switch', 'OFF');
          logger.info('Boiler UIT');
          }
        else if (itemRegistry.getItem('AchterbouwDrogerN018_Electricmeterwatts').getState() < '1'); {
                java.lang.Thread.sleep(3000);
                events.sendCommand('KeukenBoilerN005_Switch', 'ON');
                logger.info('Boiler AAN');
                }
    type: script.ScriptAction

the Log:
2022-06-22 22:35:17.442 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item ‘AchterbouwDrogerN018_Electricmeterwatts’ changed from 0.9 to 233.3
2022-06-22 22:35:17.601 [INFO ] [openhab.event.ItemCommandEvent ] - Item ‘KeukenBoilerN005_Switch’ received command ON
2022-06-22 22:35:17.604 [INFO ] [penhab.event.ItemStatePredictedEvent] - Item ‘KeukenBoilerN005_Switch’ predicted to become ON
2022-06-22 22:35:18.442 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item ‘AchterbouwDrogerN018_Electricmeterwatts’ changed from 233.3 to 1651.5
2022-06-22 22:35:19.442 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item ‘AchterbouwDrogerN018_Electricmeterwatts’ changed from 1651.5 to 2627.1
==> /var/log/openhab/openhab.log <==
2022-06-22 22:35:20.609 [INFO ] [org.openhab.rule.ef634fd10f ] - Boiler AAN
2022-06-22 22:35:20.625 [INFO ] [org.openhab.rule.ef634fd10f ] - Boiler UIT
==> /var/log/openhab/events.log <==
2022-06-22 22:35:20.609 [INFO ] [openhab.event.ItemCommandEvent ] - Item ‘KeukenBoilerN005_Switch’ received command ON
2022-06-22 22:35:20.612 [INFO ] [penhab.event.ItemStatePredictedEvent] - Item ‘KeukenBoilerN005_Switch’ predicted to become ON
2022-06-22 22:35:20.620 [INFO ] [openhab.event.ItemCommandEvent ] - Item ‘KeukenBoilerN005_Switch’ received command OFF
2022-06-22 22:35:20.623 [INFO ] [penhab.event.ItemStatePredictedEvent] - Item ‘KeukenBoilerN005_Switch’ predicted to become OFF
2022-06-22 22:35:20.626 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item ‘KeukenBoilerN005_Switch’ changed from ON to OFF
2022-06-22 22:35:20.972 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item ‘KeukenBoilerN005_Switch’ changed from OFF to ON
2022-06-22 22:35:21.330 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item ‘KeukenBoilerN005_Switch’ changed from ON to OFF
==> /var/log/openhab/openhab.log <==
2022-06-22 22:35:23.630 [INFO ] [org.openhab.rule.ef634fd10f ] - Boiler AAN
2022-06-22 22:35:23.635 [INFO ] [org.openhab.rule.ef634fd10f ] - Boiler UIT
==> /var/log/openhab/events.log <==
2022-06-22 22:35:23.631 [INFO ] [openhab.event.ItemCommandEvent ] - Item ‘KeukenBoilerN005_Switch’ received command ON
2022-06-22 22:35:23.636 [INFO ] [penhab.event.ItemStatePredictedEvent] - Item ‘KeukenBoilerN005_Switch’ predicted to become ON
2022-06-22 22:35:23.639 [INFO ] [openhab.event.ItemCommandEvent ] - Item ‘KeukenBoilerN005_Switch’ received command OFF
2022-06-22 22:35:23.642 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item ‘KeukenBoilerN005_Switch’ changed from OFF to ON
2022-06-22 22:35:23.644 [INFO ] [penhab.event.ItemStatePredictedEvent] - Item ‘KeukenBoilerN005_Switch’ predicted to become OFF
2022-06-22 22:35:23.647 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item ‘KeukenBoilerN005_Switch’ changed from ON to OFF
2022-06-22 22:35:23.732 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item ‘KeukenBoilerN005_Switch’ changed from OFF to ON
2022-06-22 22:35:23.898 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item ‘KeukenBoilerN005_Switch’ changed from ON to OFF

I don’t think IFTTT is currently allowing new rules. I’m currently in the process of converting to Amazon Alexa Smart Home Skills

This question doesn’t have anything to do with IFTTT. The OP just wants to know why their rule isn’t working.

@Fox27, please post logs in code fences. They’re too hard to read without.

There’s a lot going on with your rule, and I think it’s a little overcomplicated. From what I can tell:

  • You’re triggering on state changes and state updates.
  • Whenever the dryer plug is below 1 Watt, the rule is sending an ON command to your boiler.
  • But, it’s waiting for three seconds before it sends that command, due to your Thread.Sleep.
  • While the rule is sleeping, the rule can be triggered again by a state change/update.

So, this rule is running every time the dryer plug’s wattage is updated, even if there’s no change to the state. Every time it, it waits for three seconds and then sends an ON commands to the boiler. You just don’t notice since an ON command is being sent to something that’s already turned on.

When you start the dryer, the meter quickly climbs over 500W and the rule sends an OFF command to the boiler. But I suspect that just before that happens, the rule had triggered an ON command with a three-second delay (due to a state update that was below 1W). So you’re getting real-time commands jumbled up with commands from three seconds in the past.

Note that I’m guessing a little, because I don’t know how frequently your dryer plug updates its state.

I suggest:

  1. Removing the state update trigger. You only need to know when the state changes.
  2. Removing the Thread.Sleep, which I think is causing most of your confusion.
  3. Adding nested IF statements to check if the boiler is on before sending an OFF command, or off before sending an ON command.

This will trigger the rule less often, remove the delay in ON commands, and only send commands when they’re necessary.

I’m guessing that the three-second delay is to allow for the dryer to come to rest at less than 1 Watt after it’s been running. However, the dryer plug is reporting less than 1W after it has already happened, so there shouldn’t be any harm in turning the boiler on immediately.

the timer is needed because the dryer stops with heating and the motor stops and change direction and then start again. this takes 3 minutes. i used 3 seconds for testing :slight_smile:
i made some changes but now get an error in the code.

configuration: {}
triggers:
  - id: "1"
    configuration:
      itemName: AchterbouwDrogerN018_Electricmeterwatts
    type: core.ItemStateChangeTrigger
conditions:
  - inputs: {}
    id: "3"
    configuration:
      itemName: AchterbouwDrogerN018_Electricmeterwatts
      state: 0,5
      operator: ">"
    type: core.ItemStateCondition
actions:
  - inputs: {}
    id: "2"
    configuration:
      type: application/javascript
      script: >
        
        var logger = Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.' + ctx.ruleUID); var thread = Java.type('java.lang.Thread')

        if (itemRegistry.getItem('AchterbouwDrogerN018_Electricmeterwatts').getState() > '200') {
          events.sendCommand('KeukenBoilerN005_Switch', 'OFF');
          logger.info('Boiler UIT');
          }
        else if ((itemRegistry.getItem('AchterbouwDrogerN018_Electricmeterwatts').getState() > '0,5') &&
          (itemRegistry.getItem('AchterbouwDrogerN018_Electricmeterwatts').getState() < '1') &&
          (itemRegistry.getItem('KeukenBoilerN005_Switch').getState() = 'OFF'))  {
              java.lang.Thread.sleep(5000);
              events.sendCommand('KeukenBoilerN005_Switch', 'ON');
              logger.info('Boiler AAN');
          }
    type: script.ScriptAction

and the log file:

==> /var/log/openhab/openhab.log <==
2022-06-23 18:38:20.989 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'ef634fd10f' failed: <eval>:9:59 Invalid left hand side for assignment
  (itemRegistry.getItem('KeukenBoilerN005_Switch').getState() = 'OFF'))  {
                                                           ^ in <eval> at line number 9 at column number 59
2022-06-23 18:38:20.996 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'ef634fd10f' failed: <eval>:9:59 Invalid left hand side for assignment
  (itemRegistry.getItem('KeukenBoilerN005_Switch').getState() = 'OFF'))  {
                                                           ^ in <eval> at line number 9 at column number 59

The error is telling you exactly where the problem is. Line 9, column 59.

There’s an extra parenthesis.

If you check a condition, sleep, and then send commands, you don’t know if that condition is still true when the commands are sent. And while the rule is sleeping, you may be spawning new instances every time the state changes (with more delayed commands). I would try to avoid this. Instead, identify the conditiions that indicate the dryer is in the final stages, so that you can prime your rule to turn the boiler back on. Then turn it on when you’re absolutely certain the dryer has finished.

It might help to put openHAB aside and just write down what you want to happen in plain language. I do that sometimes when I’m trying to wrap my head around a complex behaviour, and then I use that to write the actual rule.