Rule not executed - Power Consumption

Hi Guys,
I have installed a measurement socket so that the TV is switched off at night. This should happen at 0 WATT consumption.

2023-07-13 18:21:16.881 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'TV_Schlafzimmer_Stromverbrauch' changed from 29.3 W to 13.5 W

2023-07-13 18:21:18.879 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'TV_Schlafzimmer_Stromverbrauch' changed from 13.5 W to 0 W
var thread = Java.type('java.lang.Thread')

var logger = Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.' + ctx.ruleUID);


if (itemRegistry.getItem('TV_Schlafzimmer_Stromverbrauch').getState() == '0') {
  thread.sleep(5000);
  logger.error('TV geht nun aus');
  events.sendCommand('TV_Schlafzimmer_Betrieb', 'OFF');
}


The thing is the Watt is 0 but nothing happens can some help me?

Regards
Simon

You don’t show the rule trigger.

Add logging to the rule’s action to show whether or not the rule is triggered.

Use a Timer, not a sleep.

ok i will use Timer

I set logger, but nothing happend.

  logger.error('TV geht nun aus');


When posting about a rule, or anything else really, be sure to click on the “Code” tab and paste that into the post using code fences.

```
code goes here
```

That gives all the information about the rule, in context including the full trigger config, conditions, etc.

About all I can say is that the rule only triggers when TV_Schalfzimmer_Stromverbrauch changes. That’s good.

Based on your events.log, this Item is carrying units. 0 is not the same things as 0 W.

You didn’t mention but I’m assuming OH 3.4 is the version.

How do you know? You put the log statement inside/after a bunch of other code. If TV_Schalfzimmer_Stromverbrauch doesn’t equal “0” the log statement will never run and you can’t tell the difference between the rule not running or the condition not matching.

Yeah the code from the Blockly is crap.
So i generated simple rule:


rule "Shelly TV change/update"
when
    Item TV_Schlafzimmer_Stromverbrauch changed to "0.0 | W"
then
    TV_Schlafzimmer_Betrieb.sendCommand( OFF )
    logInfo("test", "TV geht aus")
end


also tried with changed to 0

I have found with measurement at or around zero there are stray wattages, try less than 1. Also of concern at this level is frequent reporting as changes in the micro watt level can happen in milliseconds. Don’t use % change as a trigger.

There’s nothing wrong with the code from Blockly. You just need to put a log statement before the if block so it always logs something when triggered.

Once you confirm whether or not it’s triggering we can figure out whether the problem is the logic (which appears to be the case but I’m not positive) or the triggering of the rule.

I am about 90% certain that won’t work.

However, in the UI you could move the if statement to be a Condition.

conditions:
  - id: "3"
    configuration:
      itemName: HomeEnergyMeter_ElectricmeterkWh
      operator: =
      state: 0 W
    type: core.ItemStateCondition

Obviously use your Item name.

Then your Blockly becomes just the stuff next to the “do”.

The “cancel timer” should not be there though. That immediately cancels the timer you just created so nothing will happen.

tl;dr:

  • we don’t know if the rule is actually triggering because the log only happens when everything works
  • I suspect the if statement isn’t working because the state of the Item is “0 W”. not “0”
  • you create a timer and immediately cancel it so nothing will happen anyway

This helped me! Thanks for the hint :slight_smile:

rule "Shelly TV change/update"
when
    Item TV_Schlafzimmer_Stromverbrauch changed
then
if (TV_Schlafzimmer_Stromverbrauch.state < 1 | W  )
{TV_Schlafzimmer_Betrieb.sendCommand( OFF )
 logInfo("test", "TV geht aus")
}
end

2023-07-13 19:51:47.747 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'TV_Schlafzimmer_Stromverbrauch' changed from 64.5 W to 44.2 W

2023-07-13 19:51:48.738 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'TV_Schlafzimmer_Stromverbrauch' changed from 44.2 W to 30.5 W

2023-07-13 19:51:50.741 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'TV_Schlafzimmer_Stromverbrauch' changed from 30.5 W to 21.7 W

2023-07-13 19:51:52.738 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'TV_Schlafzimmer_Stromverbrauch' changed from 21.7 W to 0 W

==> /var/log/openhab/openhab.log <==

2023-07-13 19:51:52.739 [INFO ] [org.openhab.core.model.script.test  ] - TV geht aus

==> /var/log/openhab/events.log <==

2023-07-13 19:51:52.741 [INFO ] [openhab.event.ItemCommandEvent      ] - Item 'TV_Schlafzimmer_Betrieb' received command OFF

2023-07-13 19:51:52.741 [INFO ] [penhab.event.ItemStatePredictedEvent] - Item 'TV_Schlafzimmer_Betrieb' predicted to become OFF

2023-07-13 19:51:52.742 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'TV_Schlafzimmer_Betrieb' changed from ON to OFF

2023-07-13 19:51:57.082 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'WLAN_SeitenSettings_Total_Clients' changed from 26 to 25