# Array in rules

Hi,

I am a beginner to OpenHAB.

I do have a requirement to capture the water pump On cycles in an array and to find out how many times the pump turned on in the last 24 hours.

The idea was to create an array with each member holding the time in seconds. When the seconds rolls over Max seconds a day the array member shall be considered as invalid. When the pump turns on a new entry shall be made in the empty slots of array or at an invalid member location.

Inshort the array keeps a track of how many times the pump turned ON in the last 24 hours.

Unfortunately I am unable to create an Integer array in OpenHAB rules and to manage with counting the seconds by ++ to each array member.

Can someone guide me from an example (compilable)

1. with the declaration of array
2. reading each array member in a loop and comparing with a fixed value (in decimal)
3. Assigning value / doing ++ in an array member
4. Loops to manage the array.

Thanks & Regards
Bibin

This is basically a textbook example of the XY Problem. You are asking for how to do something they way you think it should be solved rather than asking for the best way to solve your actual problem.

Arrays are not directly supported in Rules DSL, and even if they were, there are better ways to solve this problem.

For example, you can use persistence, something like InfluxDB or MySQL that can store the states of Switches and Contacts. Then at any time you want the count:

``````val numberOnCycles = Pump.sumSince(now.minusDays(1))
``````

will give you the number of times the Pump turned ON in the past 24 hours. Why make it more complicated than it needs to be? Use the resources available to you so you can focus on the fun stuff.

Thanks a lot for the insight.

I am an embedded guy hence was slightly influenced by C language.

The Pump On time is not derived from switches, as the pump is controlled by a water level controller.

What I do is to monitor the load and when the pump is on the energy consumption would be approx 750W (1HP pump). If the power goes high and after sometime comes back to zero I do increment an internal variable which adds to the pump on count

Let me try with the InfluxDB for this purpose. The idea to use array was to avoid SD card writes as well

If you avoid SD card writes then every time OH restarts, you change your .rules files, or change your .items files you will lose all the data you’ve stored in variables.

Yes true, the Openhabian is expected to run continuously. The only purpose of counting the pump is that is the water is lost somewhere (due to a leaky tap or garden irrigation system). It ends up in pumping & draining cycle, which finally results in an empty sump (Infact I had faced the issue couple of times). Hence if the pump count is > 3 per day, the IoT switch that powers the water level controller is turned off. There by breaking the cycle.

As the automation platform is almost matured for home. The rules refresh and reboot of OpenHab is now very minimal might once a month.

Is there a way that I could use persistance for only few variables or data items?

Persistence can be defined on an Item by Item basis. If you only want to store one Item you can certainly do that.

I would create a Rule and a proxy Number Item. When you would normally add one to your count trigger the Rule and set the Number Item to 1. If you set your persistence policy to “every update” then the Number Item will only store the 1 at that time.

Thanks a lot for the guidance.

But I am not good at the persistence and DBs

Can you guide me with an example for this scenario.

Might be this is too much to ask. But it may really help me to go ahead.

Persistence is fully documented here with examples. Choose a database to store the data. You probably don’t want to use MapDB (only stored the latest value) or rrd4j (requires an every minute strategy). InfluxDB is my perferred DB. If you are running openHABian there is an option in openHABian-config to install and configure it.

The documentation for the add-on for what ever database you choose will have further examples for how to write the .persist file.

InfluxDB+Grafana persistence and graphing has a good tutorial for installing and configuring InfluxDB.