historicState-rule or "robby is fully loaded" - Experience Report from a Newbie


I am far away of being a programmer and I do not have too much time to spend ith with my smart-homesystem (RPI 3 with OH2) - still I like it.

And creating new rules still is quite a challenge for me (most of the time it is a combination of copy&paste and search&replace from other rules that I find here in the forum).

Still I just managend it to finish a (very small) rule which even my wife finds cool.
I wanted to get an acoustic feedback from habpanel when my garden-robot-mower has finished its loading-process (this mean: that it is fully loaded and ready to mow the lawn)

At the beginning I thougth that this must be very easy to solve (via the energy-counter of the z-wave-wall-plug which is connected with the loading-station of my robby), however I realized that it was more difficult than I thought (again: I am no programmer and have no clue of eclipse and Java and those things).

To make it short:
I managed it to create a rule which exatly does what it should do:
When my robby drives back into his loading-station and starts loading its battery, the panel on the wall (running habpanel) tells me - as soon as the loading-process has finished - “Your robby is fully loaded”

Not more, not less, discritley and quite cool.


And here’s the rule (has surly a lot of room for improvement - but it seems to work)

import org.openhab.core.library.types.*
import org.openhab.model.script.actions.*

rule "Visualisiere Roboterstatus"
    Item HM_Zwischenstecker_Garten_akt_Verbrauch_W changed
    val verbrauch = (HM_Zwischenstecker_Garten_akt_Verbrauch_W.state as DecimalType).intValue
    val verbrauch_alt = (HM_Zwischenstecker_Garten_akt_Verbrauch_W.historicState(now.minusSeconds(100)).state as DecimalType)
    if (verbrauch <= 10.000) {
        if (verbrauch_alt > 20.000) say("Robby ist vollständig geladen")   

Hey Schrödinger!
I love these kinds of small automations. Are they important for the operation of your smart home? No. Are they just awesome to have? Hell yeah! :smile:

A while back I’ve posted a tutorial with the same problem in mind. You’ll find the same methods and a bit more, maybe it’s still an interesting read for you: Washing Machine State Machine

Best! Thomas

Btw. Always underrated. Why not add an image to your article? Even just a picture of your Robby would be enough to give everyone an instant idea what the article is about.

1 Like

There really is no better feeling. Congratulations and thank you for sharing. Reports like these helps to encourage the community at large and you should rightly feel proud of your accomplishment. This is hard stuff to get working.

In my opinion, all rules should be like this. :smiley:

Not really. It’s short and to the point. Easy to understand and relatively maintainable.

There are some bits that are not strictly required but they certainly don’t hurt the functionality one bit:

  • if you are running on OH 2.x the import statements are not necessary
  • you probably don’t need the .intValue to get verbrauch; simply use HM_Zwischenstecker_Garten_akt_Verbrauch_W.state as DecimalType
  • there are some weird cases where using DecimalType causes problems (not here) so I usually recommend casting to Number instead, as in HM_Zwischenstecker_Garten_akt_Verbrauch_W.state as Number. The same goes for verbrauch_alt
  • this is a stylistic thing and I’m just listing this so you and other newbies know, you can combine the two conditions in the if into one: if(verbrauch <= 10 && verbrauch_alt > 20) say(...
  • as seen above, you don’t need the decimal places in your if statement.

I should think about this for the Design Pattern posts. Come up with some sort of icon graphic image to represent each DB. That might be hard for some of the more abstract ones though (what exactly does a Proxy look like?) :smiley:



Yeah finding an image for a proxy might be tricky but in general it’s pretty easy to post an image. Almost any image creates some understanding in the reader and spices up the text at the same time. Imagine no pictures here or here, next try to find details in the images that are not also available in the text :boom: :smiley:

thanks for your nice comments and advices. I will try them.

Best regards