So, I read a lot about the different persistent bindings, and I want to start off by saying that I would like to do things as simple as possible.
Therefore, I would like to log things to a txt file, log file, whatever.
My plan is to than have some tool such as gnuplot, analyse the log periodically and save an image that is shown on OH2’s UI.
So I have an item: Number GasCounter "Gas Flow [%.1f m3]"
which is calculated simply by a rule:
var GasCount = 0.0
var GasReset = 0
rule "test"
when
Item ReedSwitch1 changed from 1 to 0
then
GasCount = GasCount + 0.1
GasCounter.postUpdate(GasCount)
end
Now I would like to make a new rule to output GasCounter.state / value whatever, to a logfile with the timestamp.
I tried this but feel like I am far off:
rule "SaveGasData"
when
Time cron "0 0 * * * ?" // Is this value correct? I want to save every minute (as an example) or every (10min).
then
val String GasString = GasCounter.state.toString
logInfo("gas_usage, %s, %s", GasString, now)
end
Now should be the date+time (not sure how).
Ideally, Date, time, value
FYI - I wanted to implement this with command-line commands of echo >>…
But I couldn’t figure it all out.
rule "SaveGasData"
when
Time cron "0 0 * * * ?" // Is this value correct? I want to save every minute (as an example) or every (10min).
then
val String GasString = GasCounter.state.toString
val DateTime datetime= now()
val String date = datetime.toLocalDate()
val String time = datetime.toLocalTime()
val String logString = String::format("Gas Information, %s, %s, %s",date,time,GasString)
logInfo(" Gas Information",logString)
end
I´m not familliar with log4j; but maybe you can create a separate logfile using that with the format you required; as far as I understand that should work; as events are also logged into a different file …
Maybe someone with more background in that area can help.
You have a weird definition of “simple as possible.” This is simple for the tools doing the writing but it will be a lot of extra work for you. Maybe you mean “simple as possible” to mean “I have to learn as little as possible.”
Have you looked at events.log? It already logs out all updates, commands, and state changes to all your Items in a standard format.
You can look into log4j2 logging layouts and configure a logger to log these out in some other format. I see both JSON and CSV are supported out of the box.
Yeah, I am trying the log4j2 now.
Reason for that being simple as in, I saw many fancy systems for logging and visualising the data.
On the other hand, I couldn’t find packaged deb builds for the ARM, so I would have to compile them, and probably meet new hurdles along the way.
Perhaps I should have said simple and flexible. logging data to CSV is just raw data, then post-processing will give me control of what I can display and what not.
This week was first OH2, first playing with ESP8266, first playing with MQTT, etc. So I am all for learning new things, but some things such as this logging topic - I could not find any easy example.
http://docs.openhab.org/administration/logging.html <-- this one for example uses log4j and not log4j2 - I do not know if that is the same thing or not… but I am computer illiterate beyond the days of C++ and OpenGL 2…
rule "SaveGasData"
when
Time cron " 0 0/1 * 1/1 * ? *"
then
val String GasString = GasCounter.state.toString
val DateTime datetime= now()
val String date = datetime.toLocalDate()
val String time = datetime.toLocalTime()
val String logString = String::format("Gas Information, %s, %s, %s",date,time,GasString)
logInfo("Measure",logString)
end