Put state into a file?

Hey,

Today, I’ve got 4 knx sensors (1-2-3 & 4) to measure the level of my well. I would like to put the these values in a text file. And at the end, I’m creating a rrdtool with this through cacti.

Today, this was done with a bash script that read the values out the linuxmce database. But of course, I’m open for all suggestions… So what’s the best way to write this to a file with openhab?

I don’t know the best way, but some ideas for good ways include:

  • save the data to one of the non-embedded databases such as MySQL and extract the data from there into rrdtool
  • use the logging persistence addon and configure to persist only those four Items to a log file
  • use MQTT persistence to publish the values and a script client to write the received data to file or, even better, save it to rrdtool directly
  • create a rule to append the new data to a file when it is updated, or even better save it to rrdtool directly though executeCommandLine
  • is rrdtool compatible with rrd4j? If so use rrd4j persistence on those Items and load them into rrdtool directly

I’m sure there are other ways but these are what comes to mind right now.

Did I understand it right, that the 4 sensors are in fact ON/OFF, so:

Water level | Sensor 1 | Sensor 2 | Sensor 3 | Sensor 4
   0 - 0.9  |    OFF   |    OFF   |    OFF   |    OFF   
   1 - 1.9  |    ON    |    OFF   |    OFF   |    OFF   
   2 - 2.9  |    ON    |    ON    |    OFF   |    OFF   
   3 - 3.9  |    ON    |    ON    |    ON    |    OFF   
  4 & above |    ON    |    ON    |    ON    |    ON   

?
Then I would suggest to build a bunch of Items:

Group well
Switch well_1 "Level 1 is [%s]" (well) {knx="GA1/1/1"}
Switch well_2 "Level 2 is [%s]" (well) {knx="GA1/1/2"}
Switch well_3 "Level 3 is [%s]" (well) {knx="GA1/1/3"}
Switch well_4 "Level 4 is [%s]" (well) {knx="GA1/1/4"}
Number well_level "Well level is [%d]"  //proxy item, no Binding, no group!

a simple rule:

rule "well level"
when
    Item well received update
then
         if (well_4.state == ON) well_level.postpdate(4)
    else if (well_3.state == ON) well_level.postpdate(3)
    else if (well_2.state == ON) well_level.postpdate(2)
    else if (well_1.state == ON) well_level.postpdate(1)
    else                         well_level.postpdate(0)
end

Do persistence to well_level (strategy: everyMinute) with rrd4j.

What’s missing is Average and Maximum, but you can calculate this through another rule, maybe combined with a setpoint item to choose the period, and show the values through openHAB items.

Posted right now, it could be even simpler :slight_smile:

Group:OR(ON,OFF) well "Well level is [%d]"

as the group, no rule nor proxy item needed at all, the well.state will show the level :slight_smile: so persistence is done to the group item itself (use well in persistence file, not well*)

You understood it completely. :wink:

I’ve ended up with:

Switch contact_WO_Waterput_1 “Waterput 0 tot 25” (WOWaterput) { knx=“3/1/118” }
Switch contact_WO_Waterput_2 “Waterput 25 tot 50” (WOWaterput) { knx=“3/1/119” }
Switch contact_WO_Waterput_3 “Waterput 50 tot 75” (WOWaterput) { knx=“3/1/120” }
Switch contact_WO_Waterput_4 “Waterput 75 tot 100” (WOWaterput) { knx=“3/1/121” }
Group:Switch:OR(ON,OFF) WOWaterput “Waterputniveau [(%d)]” (All)

And in the sitemap, I’ve put following, just for testing now:

Text item=WOWaterput icon=“cistern”

But I’m not getting any data. It stays on (0).
After checking the log files, it seems that the sensors (contact_WO_Waterput_*) aren’t triggered? Is that only been filled in if the sensor changes? If so, is there a way to force this status update?

ps in your example, you write knx="GA1/… Does the GA part do anything special?

Thanks already for your support!!!

Ah. I should have reread my posting… “GA” is wrong :slight_smile:
You can setup the binding with

{ knx="<3/1/118" }

Pay attention to the <, this will result in a read request when starting up openHAB - of course the GA should answer the read request :slight_smile:

hmmm, think I’ll need to check the knxbus a bit more closely, as you mention.

In the openhab.log, I’m getting follwoing entry after adding the ‘<’:

2016-05-20 06:09:35.156 [WARN ] [.KNXBindingDatapointReaderTask] - Autorefresh: Cannot read value for item ‘contact_WO_Waterput_2’ from KNX bus: timeout waiting for group read response: timeout

The knx inputs are a ABB I/O device for bus system 4 In/ 4 Out US/U 4.2.
I’ll see if I can find something in ETS that could explain this.

ps I’ve also added the < before a working GA, and it’s giving me the same error in the log.

You have to set the R-Flag in the ABB I/O device for each GA you want to read from the knx bus. The R-Flag should be set in for exactly one communication object per Group Address, usually the status CO of an actuator, but I think there is no such device for this GA :). Of course the C-Flag has to be set, just as the T-Flag.

Flag
C (Communicate): The device will communicate with the bus.
R (Read)       : The communication object is readable (i.e. it will answer a read request)
W (Write)      : The communication object is writable.
T (Transmit)   : The communication object will send changes in value
U (Update)     : The communication object update it's value if other bus subscribers will answer a read request.

Cool! I can now read nicely the status through openhab after updating the R flag.
Next step (hopefully tomorrow),I can adding the info to a graph. I’ll let you know how this is going…

Thanks already!!!

Nice, it’s working!!! What I’ve got now is:

Rule in openhab to be run every 5 minutes, and send the status (0>4)

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

rule “Waterput Niveau Voor grafieken”
when
Time cron “0 0/5 * * * ?”

then
if (contact_WO_Waterput_4.state == ON) {
executeCommandLine("/etc/openhab/configurations/scripts/waterput.sh 4")
} else if (contact_WO_Waterput_3.state == ON) {
executeCommandLine("/etc/openhab/configurations/scripts/waterput.sh 3")
} else if (contact_WO_Waterput_2.state == ON) {
executeCommandLine("/etc/openhab/configurations/scripts/waterput.sh 2")
} else if (contact_WO_Waterput_1.state == ON) {
executeCommandLine("/etc/openhab/configurations/scripts/waterput.sh 1")
} else
executeCommandLine("/etc/openhab/configurations/scripts/waterput.sh 0")
end

And /etc/openhab/configurations/scripts/waterput.sh is a little script to put the status in a text file with the date. And at the end, this file is being send to the monitoringserver (cacti) to be converted in a rrd file.

#!/bin/sh
VAR1="$1"
NOW=$(date +%s)
echo $NOW:$1 > /etc/openhab/configurations/scripts/waterput_status.txt
sleep 2
sshpass -p ‘XXXXXX’ scp
/etc/openhab/configurations/scripts/waterput_status.txt
beheerder@monitare:/scripts/waterput_status.txt

And at the end, this give following graph (ups and downs is me testing):