Rule / Persistence: Shut off Power after 5min of low Power-Consumption

  • Platform information:

    • Hardware: RPi 4 B
    • OS: openhabian
    • openHAB version: openHAB 2.5.3-1
  • Issue of the topic:

Goal: I am using another Rpi3+ as Kodi Media-Player, which is connected to openhab as a thing. It is plugged into a Devolo Smart Plug 2 (Manual). The Smart Plug is capable of metering the actual power consumption and reporting it. I set the reporting period (parameter 1) to 3min. By turning on the power outlet, I make the Kodi-Player start the boot process. Therefore, it is necessary to turn off the power after shutting down the Kodi-Player so that it is ready to start a new boot process next time.

That’s why I want to create a rule which shuts down the power outlet of the smart plug after 5 minutes, if the average of power consumption reported was lower than 3 Watts. The calculation of the average power consumption over 5 min is necessary and can not be changed to only a single measuring point, because the RPi sometimes has fluctuations in power consumption or needs to be restarted without the power being cut off.

Therefore, I started with setting up persistence (rrd4j) for the smart plug.

Problems:

  1. Even though I changed the reporting period in the Device Settings to 3min, I only get reports every hour (see logs below). That’s why I guess something is misconfigured with the persistence or I need to somehow tell openhab to get the actual power consumption in the right interval.
    Can someone tell me how to do this?

  2. Even if the change in power consumption is recorded once every hour, my rule seems not to trigger a switch of the power outlet. I see one problem: If the item measuring the power consumption changes and I use this event as a trigger to calculate the average consumption over the last minutes, the outcome will most probably be over the 3 Watts, because the RPi just turned off. But I have no idea to avoid this problem. Maybe with a timer triggered first?

So far I created the following:

  • Please post configurations (if applicable):

    • rrd4j.persist:

      Strategies {
      // for rrd chart cron strategy every minute
      everyMinute : “0 * * * * ?”
      every3Minutes : “0 */3 * * * ?”
      // get data reduced for older values to keep database small
      everyHour : “0 0 * * * ?”
      everyDay : “0 0 0 * * ?”
      default = everyChange
      }
      Items {
      Verbrauch_Kodi : strategy = everyUpdate, everyMinute, every3Minutes : “0 */3 * * * ?”, restoreOnStartup
      }

    • Rules code related to the issue:
      kodi.rules

      rule “Kodi Stromzufuhr abschalten wenn ungenutzt”
      when
      Item Verbrauch_Kodi changed
      then
      // Add persistence to Verbrauch_Kodi item
      if(Verbrauch_Kodi.averageSince(now.minusMinutes(10)) > 2){ // adjust time (1st) and watts (after >) as needed
      // Thread::sleep(1000) // un-comment if needed to allow switch state to update
      }
      else if(Verbrauch_Kodi.averageSince(now.minusMinutes(10)) < 3){
      Kodi_Energiezufuhr.sendCommand(OFF) // turn power off if kodi used less than XW in next X minutes
      }
      end
      rule “Fernsehathmosphäre abends gemütlich, wenn Kodi an”
      when
      Item Kodi_Energiezufuhr changed from OFF
      then
      var Number hour = now.getHourOfDay
      if ((hour >= 19) || (hour < 7 )) { // either its later than X OR earlier than X
      sendCommand(KrakeImWohnzimmer_Helligkeit, 30)
      }
      end

  • If logs where generated please post these here using code fences:

I would be very happy for support for both problems or one of them.

Thank you in advance.

Do you mean the configuration parameters or the poll interval? You should be able to correct #1 by configuring the zwave device.

What is the type:id of your device (look in the Thing under Attributes in Habmin)? I ask so that it can be looked up in the device database.

Please post your logs for help with #2. Also, please edit your post and use code fences (</>) rather than a quote.

From rrd4j persistence doc

The strategy everyMinute (60 seconds) MUST be used, otherwise no data will be persisted (stored).

I’m not sure that’s entirely true if you tinker with other settings, but you’d need a very good reason not to do everyMinute here.

The configuration parameter. I changed it already in the zwave device configuration.

I am not sure if I found what you are asking for. The deviceType is Binary Power Switch. The node ID is 2.

Which logs do you mean exactly?
This is cat /var/log/openhab2/events.log | grep Kodi for today.

2020-04-27 00:22:50.278 [vent.ItemStateChangedEvent] - Verbrauch_Kodi changed from 3.9 to 0.8
2020-04-27 02:22:50.925 [vent.ItemStateChangedEvent] - Verbrauch_Kodi changed from 0.8 to 0.9
2020-04-27 03:22:51.196 [vent.ItemStateChangedEvent] - Verbrauch_Kodi changed from 0.9 to 0.8
2020-04-27 06:22:52.199 [vent.ItemStateChangedEvent] - Verbrauch_Kodi changed from 0.8 to 0.9
2020-04-27 07:22:52.483 [vent.ItemStateChangedEvent] - Verbrauch_Kodi changed from 0.9 to 0.8
2020-04-27 08:22:52.821 [vent.ItemStateChangedEvent] - Verbrauch_Kodi changed from 0.8 to 0.9
2020-04-27 10:22:53.430 [vent.ItemStateChangedEvent] - Verbrauch_Kodi changed from 0.9 to 0.8
2020-04-27 10:52:42.318 [ome.event.ItemCommandEvent] - Item 'Kodi_Energiezufuhr' received command OFF
2020-04-27 10:52:42.328 [vent.ItemStateChangedEvent] - Kodi_Energiezufuhr changed from ON to OFF
2020-04-27 10:52:48.377 [ome.event.ItemCommandEvent] - Item 'Kodi_Energiezufuhr' received command ON
2020-04-27 10:52:48.390 [vent.ItemStateChangedEvent] - Kodi_Energiezufuhr changed from OFF to ON
2020-04-27 11:22:53.804 [vent.ItemStateChangedEvent] - Verbrauch_Kodi changed from 0.8 to 3.9
2020-04-27 11:29:00.333 [temChannelLinkRemovedEvent] - Link 'Kodi_Energiezufuhr => zway:zwayDevice:192_168_178_32:2:switchPowerOutlet-ZWayVDev_zway_2-0-37' has been removed.
2020-04-27 11:29:00.336 [temChannelLinkRemovedEvent] - Link 'Kodi_FavoritAbspielenOderFfnen => kodi:kodi:fbf9b143-2783-0c91-2dac-b91681ca9338:playfavorite' has been removed.
2020-04-27 11:29:00.339 [temChannelLinkRemovedEvent] - Link 'Verbrauch_Kodi => zway:zwayDevice:192_168_178_32:2:sensorMeterW-ZWayVDev_zway_2-0-50-2' has been removed.
2020-04-27 11:30:00.434 [vent.ItemStateChangedEvent] - Kodi_FavoritAbspielenOderFfnen changed from NULL to UNDEF
2020-04-27 11:30:04.743 [vent.ItemStateChangedEvent] - Verbrauch_Kodi changed from NULL to 3.9
2020-04-27 11:30:11.160 [vent.ItemStateChangedEvent] - Kodi_Energiezufuhr changed from NULL to ON
2020-04-27 12:30:11.719 [vent.ItemStateChangedEvent] - Verbrauch_Kodi changed from 3.9 to 0.8
2020-04-27 13:18:21.389 [ome.event.ItemCommandEvent] - Item 'Kodi_Energiezufuhr' received command OFF
2020-04-27 13:18:21.402 [vent.ItemStateChangedEvent] - Kodi_Energiezufuhr changed from ON to OFF
2020-04-27 13:18:26.287 [ome.event.ItemCommandEvent] - Item 'Kodi_Energiezufuhr' received command ON
2020-04-27 13:18:26.307 [vent.ItemStateChangedEvent] - Kodi_Energiezufuhr changed from OFF to ON
2020-04-27 13:30:12.118 [vent.ItemStateChangedEvent] - Verbrauch_Kodi changed from 0.8 to 4.7
2020-04-27 14:30:12.589 [vent.ItemStateChangedEvent] - Verbrauch_Kodi changed from 4.7 to 0.8
2020-04-27 15:32:56.369 [ome.event.ItemCommandEvent] - Item 'Kodi_Energiezufuhr' received command OFF
2020-04-27 15:32:56.408 [vent.ItemStateChangedEvent] - Kodi_Energiezufuhr changed from ON to OFF
2020-04-27 16:30:13.591 [vent.ItemStateChangedEvent] - Verbrauch_Kodi changed from 0.8 to 0
2020-04-27 16:30:13.631 [ome.event.ItemCommandEvent] - Item 'Kodi_Energiezufuhr' received command OFF
2020-04-27 17:45:17.954 [ome.event.ItemCommandEvent] - Item 'Kodi_Energiezufuhr' received command ON
2020-04-27 17:45:17.963 [vent.ItemStateChangedEvent] - Kodi_Energiezufuhr changed from OFF to ON
2020-04-27 18:30:14.407 [vent.ItemStateChangedEvent] - Verbrauch_Kodi changed from 0 to 3.9
2020-04-27 18:30:14.445 [ome.event.ItemCommandEvent] - Item 'Kodi_Energiezufuhr' received command OFF
2020-04-27 18:30:14.455 [vent.ItemStateChangedEvent] - Kodi_Energiezufuhr changed from ON to OFF
2020-04-27 19:30:14.742 [vent.ItemStateChangedEvent] - Verbrauch_Kodi changed from 3.9 to 0
2020-04-27 21:24:25.474 [ome.event.ItemCommandEvent] - Item 'Kodi_Energiezufuhr' received command ON
2020-04-27 21:24:25.509 [vent.ItemStateChangedEvent] - Kodi_Energiezufuhr changed from OFF to ON

Thank you. I had it in there before and added it again.

Using Habmin, Paper UI, or another zwave tool? Either way, it did not update if the device is still reporting every 1h. Debug logs for the binding will show a lot and may explain why it did not update. The reason you see the update every hour is due to the device being polled by the binding (default poll is 1h).

No, Habmin> Configuration> Things> select your Thing> Attributes> Type:ID…

This is a unique identifier used to pull in the Thing type definition.

The openhab.log where you would likely see an error and where you would be monitoring the log statements from your rule, if you had any :wink:.

Using the Z-Wave>Me Smart Home Configuration tool. If I use the get command for parameter 1 (reporting time) it answers it is set to 3min (36x5s).
grafik

Great. How can I access these debug logs for the binding?

It look a bit different in my case. Therefore I attached this screenshot:
grafik

I did grep for Kodi, rule and Verbrauch, but nothing specific was found.

[09:55:19] ***@***:~$ cat /var/log/openhab2/openhab.log | grep Kodi [09:57:06] ***@***:~$ cat /var/log/openhab2/openhab.log | grep rule 2020-04-27 21:21:56.300 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'kodi.rules' [09:57:18] ***@***:~$ cat /var/log/openhab2/openhab.log | grep Verb

Ah… you are using Z-Wave.me! I have no first hand experience with that and you will need to contact their support. I thought you were using the Z-Wave binding, which reportedly works much better than Z-Wave.me.

Thank you for your reply.

As from my noobish understanding I thought it obligatory to install z-wave.me to get the RaZberry Device running I am using to connect to my Z-Wave devices.

In openhab I also have the zwave binding installed. And it looks like the Plug in question is used through the binding as it’s address is zway:zwayDevice:192_168_178_32:2:switchPowerOutlet-ZWayVDev_zway_2-0-37

To be honest now I do not know if I should uninstall one or how to continue.

They are not compatible to run on the same system, so you should remove one of them.

Thanks. I removed z-way binding and z-wave.me.

I installed Z-Wave Binding and added the plug again. Now in Habmin my type:id is

0001:0012

I also configured the “1: Watt counter reporting period” to 180sec.

Cool… is it reporting every 3 minutes? Here is the entry in the device db… https://www.cd-jackson.com/index.php/zwave/zwave-device-database/zwave-device-list/devicesummary/618.

I had to remove it and add it again, and now: Yes! Thank you!

But now of course it becomes clear, that my rule is a desaster. :slight_smile:

If the power consumption changes and in the same moment I want to calculate the average power consumption over the last ten minutes of course this number is below any threshold because the media player just started.

Is it possible to use:
if(Verbrauch_Kodi.averageSince(now.plusMinutes(10)) > 2)
Instead of minusMinutes in this situation? I guess no because persistence has no numbers for the future, only for the past.

So what would be the best attempt here? Put a timer to wait for 10 minutes after a change in powerconsumption?

Well yes, persistence is not prediction :wink:

The usual approach, which you will find talked about here in the context of monitoring e.g. washing machine cycle, would be to detect when power has reduced to below a threshold.
By implication, the interest in the past when power changes is therefore “was it just working?” and so maybe it’s now stopped.
At that point you might start a timer, or count some more below-threshold reports, before deciding that yes it really has stopped.

That’s what I want to achieve. How do I count more reports for let’s say 10 more minutes, before deciding to switch off the plug?

As mentioned I want to make sure the Mediaplayer had really shut down and not only performed e restart or had a fluctuation in Power Consumption. Therefore, the trigger should be a reduced power consumption as in my rule example. But instead of immediately shutting it down if consumption is below the threshold of 3 Watts, I want the rule to check for 10 minutes after the initial loss in power consumption (trigger) if the consumption stayed below the threshold. How can I achieve this?

One approach, using a timer.

Have a rule triggered by power update

If power below threshold -
If your timer already exists, do nothing
Otherwise, make a new timer.
The timer is scheduled for ten minutes time to do your “off” actions.

If power above threshold, cancel any existing timer.

Thank you for your advise. Please find below my proposal and feel free to comment. :slight_smile:

rule "Kodi Stromzufuhr abschalten wenn ungenutzt"

when
Item Verbrauch_Kodi changed
then
// Add persistence to Verbrauch_Kodi item
if(Verbrauch_Kodi. < 2 ){
if (timer===null) {
timer = createTimer(now.plusMinutes (10))
if(Verbrauch_Kodi.averageSince(now.minusMinutes(10)) < 2){
Kodi_Energiezufuhr.sendCommand(OFF)
}
}
}
else if(Verbrauch_Kodi > 2){
timer.cancel
timer = null // cancel any timer if power consumption raises
}
end

Use three ticks ``` before and after code or logs…

```
code or logs
```

I guess that is your instantaneous power measurement Item?
It’s the .state you are interested in.

Be careful about what you compare it with, if your Item has UoM units.

You don’t need the persistence/average; if the reading rises above threshold it cancels the timer.
If it hasn’t done that in ten minutes, it commands OFF.

Use a unique name for your timer handle variable, so that you don’t accidentally introduce a clash when you write some other timer-using rule later.
Don’t forget to declare it in global namespace.

Yes. Thanks will change accordingly.

Yes makes sense. More simple is better.

How would I do that?

And how is that achieved?

Are the following modifications right? Can I use it like this?

var Timer timerKodiabschalten = null

rule “Kodi Stromzufuhr abschalten wenn ungenutzt”

when
Item Verbrauch_Kodi changed
then
if(Verbrauch_Kodi.state < 2 ){
if (timerKodiabschalten===null) {
timerKodiabschalten = createTimer(now.plusMinutes (10))
if(Verbrauch_Kodi.state < 2){
Kodi_Energiezufuhr.sendCommand(OFF)
}
}
}
else if(Verbrauch_Kodi.state > 2){
timerKodiabschalten.cancel
timerKodiabschalten = null // cancel timer if power consumption raises
}
end