How to trigger a rule upon change of a Power (W) value

Hi Guys,

I am trying to switch off a washing machine upon power consuption goes to zero.

Item configuratiion of the power outlet:

Number:Energy MQTT_ZBNeoPlug01_Power "Power [%.2f W]" ( g_MQTT_ZBNeoP_01_INFO ) {channel="mqtt:topic:MQTT_NeoP_01:Power"}

In the VS Code extension I see this value:
image
The rule what is acceptable looks like this:

rule “React on Power (MQTT_ZBNeoPlug01_Power) change/update”
when
Item MQTT_ZBNeoPlug01_Power changed to 0.0
then …

But it does not trigger the rule :frowning_face:.

If I generate the code from VS Code Editor (right click o the item in teh OH extension and “Create Rule”):

rule “React on Power (MQTT_ZBNeoPlug01_Power) change/update”
when
Item MQTT_ZBNeoPlug01_Power changed from 0.0 W
then
// your logic here
end

But after saving the rule I got this error in the log file:

2020-08-20 11:36:07.618 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model ‘tested.rules’ has errors, therefore ignoring it: [393,50]: extraneous input ‘W’ expecting ‘then’

Any idea how to fix it?

You cannot have the “W” in your rule trigger, as stated in the log.

Maybe have a look at what I suggested in another thread, and how I check for when my washing machine has finished:

I.e. trigger the rule when your Item MQTT_ZBNeoPlug01_Power changed in general, then check for the values in the rule.

In your case you will have to convert the state to a quantity type for the comparisons though

if (MQTT_ZBNeoPlug01_Power.state == 0.0 | "W") {
…

Great washing machine by the way. Most modern ones I come across still have a small standby power consumption :frowning: or was your machine switched off already when you got the 0.0 reading and therefor the initial rule was never triggered as the power might be slightly above 0 and needs adjusting?

1 Like

You’ve focused on the right feature here, but Quantity Types with units were introduced partway through openHAB2 life, and not all parts of it yet work as fully as might be expected. Persistence for example, stores without units.

Rule triggers is another area. The pipe character | can be used in rules to indicate units - but that does not work in triggers.

Item MQTT_ZBNeoPlug01_Power changed from 0.0 | W

ought to be the “correct” way to do this, but the rules parser chokes on the pipe.

Item MQTT_ZBNeoPlug01_Power changed from "0.0 | W"

hides the pipe from the parser, but of course it doesn’t actually work because we’re now comparing Quantity Type to string.

The only workaround I know is to trigger on any change, and then sort out within the rule body if it was a change that you were interested in. That’s less overhead than it sounds, don’t worry about efficiency here.

when
   Item MQTT_ZBNeoPlug01_Power changed
then
   if ( MQTT_ZBNeoPlug01_Power.state > 0 | W) {
      // do stuff
   }

In this particular case, there is actually a slight advantage to doing it this way. Your Item state could be 0W or 0.0W or 0.00001W, and give you trouble with the exact match needed for a rule trigger. instead, you can choose your > threshold value freely, and this part will sort itself out.

But it is a deficiency in DSL rules triggers that is not likely to get fixed at this stage in OH2 life.

1 Like

I think he wants to see if it’s below a certain value though, to see when it has finished, so

when
   Item MQTT_ZBNeoPlug01_Power changed
then
   if ( MQTT_ZBNeoPlug01_Power.state < 1 | W) {
      // do stuff
   }

lower than 1 or some other appropriate low value, which should be double checked with his machine. But as I stated in the other thread, a timer is probably needed as well, to catch low power readings during the cycles.

1 Like

Thanks for the responses.
Forgot to mention that I tried the trigger

Item MQTT_ZBNeoPlug01_Power changed from 0.0 | W

but without success.

Honestly I did not want to fire the rule on all change of power value so I find another way chaning the item definition (remove the UM) and the rule works well.

My items:

Number MQTT_ZBNeoPlug01_Power "Power" ( g_MQTT_ZBNeoP_01_INFO ) {channel="mqtt:topic:MQTT_NeoP_01:Power"}
Number Daily_ZBNeoPlug01_Power "Napi_Power [%.2f]" ( g_MQTT_ZBNeoP_01_INFO, g_Startup )
Switch AutoSw_ZBNeoPlug01_PowerSwitch "AutoSw_ZBNeoP01" ( g_MQTT_ZBNeoP_01 ) { expire="30s,command=OFF" }

And this is my new rule:

// Local variables
var Number val_ZBNeoPlug01_Power = 0

rule "React on Power (MQTT_ZBNeoPlug01_Power) change/update"
when
    Item MQTT_ZBNeoPlug01_Power changed to 0.0
then
    AutoSw_ZBNeoPlug01_PowerSwitch.sendCommand( ON )
    val_ZBNeoPlug01_Power = Daily_ZBNeoPlug01_Power.state
    //logInfo("test", "Counter starts at " + val_ZBNeoPlug01_Power)
end

rule "React on AutoSw_ZBNeoP01 (AutoSw_ZBNeoPlug01_PowerSwitch) change/update"
when
    Item AutoSw_ZBNeoPlug01_PowerSwitch changed to OFF 
then
    var Number fogy = 0 
    //logInfo("test", "COunter stops at "+ val_ZBNeoPlug01_Power)
    fogy = Daily_ZBNeoPlug01_Power.state as Number - val_ZBNeoPlug01_Power 
    val_ZBNeoPlug01_Power = 0
    //logInfo("test", "Used power: " + fogy)
    if ( MQTT_ZBNeoPlug01_Power.state == 0|W || fogy < 0.02) { 
            MQTT_ZBNeoPlug01_PowerSwitch.sendCommand (OFF) 
            //logInfo("test", "Washing Machine off - Used power.: " + fogy)
            say("Mosás befejeződött.", new PercentType(99))
            sendNotification("edit.alakszai@gmail.com", "Mosás befejeződött. Napi fogyasztás" +    Daily_ZBNeoPlug01_Power.state)    
    }
end

My washing machine is a AAA Samsung model but I was also surprised that it does not use any power in standby mode or my Neo Plug smart outlet not able to sense it :slight_smile:

As I said, it costs you nothing.

This is not always wise (although in this case, it will probably do no harm for an MQTT linked Item)
Example of the troubles that can arise, for later readers -