Reset rain value


  • Platform information:
    version: 3.1.1
    buildString: Release Build
    locale: fr-FR
    configFolder: /etc/openhab
    userdataFolder: /var/lib/openhab
    logFolder: /var/log/openhab
    javaVersion: 11.0.12
    javaVendor: Azul Systems, Inc.
    javaVendorVersion: Zulu11.50+19-CA
    osName: Linux
    osVersion: 5.10.63-v7l+
    osArchitecture: arm
    availableProcessors: 4
    freeMemory: 17599816
    totalMemory: 102723584
    • meteoalerte
    • mqtt
    • remoteopenhab

I receive a cumulative value from a rain sensor via MQTT, I display it in a chart, but I would like to reset the value to zero everyday. How can I do that? I have seen some people writing some codes, but first I don’t understand where, and then, what.

Hope you can help, thanks in advance.

Behaviors in openHAB are implemented in rules.

Rules are event driven. Something happens and the rule runs to do something in response.

You have defined the event: every day. I would assume midnight or sometime around then would be acceptable.

Now there is the “what to do.” You want to reset the value to zero. But it’s your rain sensor that is doing the adding. Does it have a way to cause the device to reset to zero? Some message you can send over MQTT or the like?

What happens to the measurement when the device loses power?

1 Like

The device goes back to zero when I remove the batteries, but I won’t do that every day.
I was more thinking of looking at the value at 00h00, and then subtract that value from the value seen after midnight.
For example, if I receive the value 5.250 from the rain sensor,
at midnight, I should put in “memory” the value of 5.250 and the level in the graph should go back to zero (maybe I can directly say I want to display the newvalue - the oldvalue ?).
and if later I receive 5.500, then I want to see 0.250 (5.500 - 5.250).

OK, narrowing in on the requirements.

You need to store something in “memory”. But that value needs to persist across OH restarts. The best way to do that is by using an Item. Let’s make it a Number Item and call it “Rain_Accumulated_Yesterday”. If you are using a default OH 3 configuration, the state of this Item will be saved and restored by default.

You also need an Item that represents the the value reported by the device. Let’s call that “Rain_Accumulated”.

Finally, you need an Item to show only the rain for today. We’ll call that “Rain_Today”.

You’ve defined an event: when Rain_Accumulated changes. That’s the rule trigger.

In the rule, take the state of Rain_Accumulated and subtract Rain_Accumulated_Yesterday and update Rain_Today with the result.

So far so good and relatively easy to implement. But what about the edge cases? What happens that very first time the rule runs and Rain_Accumulated_Yesterday is NULL? It’s never had a state before and you can’t do math with NULL. So you need to initialize Rain_Accumulated_Yesterday.

What happens if you take the battery out of the device and it resets to 0? In that case you want to update Rain_Today with Rain_Today + Rain_Accumulated until the next day. How can you detect that event? Compare Rain_Today with Rain_Accumulated. If Rain_Today ever becomes larger than Rain_Accumulated then you know that the device reset back to 0.

From the previous requirements you need another rule at midnight to update Rain_Accumulated_Yesterday with the state of Rain_Accumulated and reset Rain_Today to 0.

You have everything you need there in prose. So how do you want to code this in rules? It’s too complicated to do without code. But it’s not too complicated to use Blockly. It’s not hard in Rules DSL.

1 Like

Thank you for these detailed explanations!
I have created the items, and I am in the rule. I don’t see where/how I can put the operation. Currently I have this in the code:

  - id: "1"
      itemName: Rain_Accumulated
    type: core.ItemStateUpdateTrigger
conditions: []
  - id: "2"
      itemName: Rain_Today
      state: CLOSED
    type: core.ItemStateUpdateAction

Is it in the state? Instead of “CLOSED” can I put “Rain_Accumulated-Rain_Accumulated_Yesterday” ?

For the reset at midnight, should I use a rule or a schedule?

I don’t mind writing code, but the first thing is I don’t know where (which file) and what language.

Just see the script option going to Blockly and Rules DSL.
I will look at Blockly documentation…

A schedule is a rule.

I have not found any documentation about Blockly, and I am not sure about rule DSL.
How do you simply update a value of an item?
I tried this:`actions:

  • inputs: {}
    id: “2”
    type: application/vnd.openhab.dsl.rule
    script: postUpdate(Rain_Accumulated,GenericMQTTThingJardin_Pluie)
    type: script.ScriptAction`

or “script: Rain_Accumulated = GenericMQTTThingJardin_Pluie;”

But it does not work.

Blockly should be relatively self explanatory. It’s all graphical. You want to interact with openHAB. There’s an openHAB set of Blocks.

You want to update an Item. There’s an “Items and Things” section under openHAB.

So you want to send the state of one Item to another Item. One of the blocks gets the state of an Item. Another talks about sending a value to an Item. So use those. And since you want to update the Item, change the send command to post update.

Or, if you want to use Rules DSL and type it out in text, be sure to reference the docs. Rules | openHAB

Again, you want to manipulate the state of Items. There’s a whole section for that. Rules | openHAB

And one key thing to notice in the examples is that to get the state of an Item you use .state. You can’t just postUpdate a whole Item to another Item. An Item has a whole lot of other stuff associated with it too. But only the state can be sent as an update.

1 Like

Thank you again for your detailed explanations and the screenshots. Very great help!
I went the Blockly way.
For the subtract, I had to use variables, was there a better way?
I noticed I am missing the arrow and several options under “openHAB” section. By chance, when I click on openHAB I have de “Items and Things” section.

The others depend of bindings you installed?

If it works it works. I do know there are times when you have to convert a state to a variable like that.

There have been a ton of additions to Blockly in the past weeks to make it support more openHAB features and therefore making it more suitable for rules development overall and not just useful for simple use cases.

1 Like