Logging to csv or some txt at fixed interval of cron rule

Hi again,

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.

You could try to format the output this way …

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

The cron setting is not correct. Please see the documentation on cron in the documentation https://docs.openhab.org/configuration/rules-dsl.html#time-based-triggers

Cool, thanks @MartinKo, where can I find the log?

Thanks.

/var/log/openhab2/openhab.log

Damn, so I cannot save that to a separate file?

Actually, now that the string has all the data I need.
Is there a way to echo via commandline?

executeCommandLine(“echo somefile.txt” + “>>” + logString)

?

It can be done, but then you have to use the Exec Binding. You could also do a

cat openhab.log | grep "Gas Information"

to only get the lines with gas information in the log file

… or even

cat openhab.log | grep "Gas Information" >> log.file

hmmm, grep could work - I will keep that as a last resort.
Since I ought to do that periodically.

Whereas the echo way would allow me to append at the cron speed.

This must be the second hardest thing I encountered after understanding what an item is…

Anyone? :frowning:

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.

https://docs.openhab.org/administration/logging.html#logging-into-separate-file

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.

https://logging.apache.org/log4j/2.x/manual/layouts.html

Yes, you can. See http://docs.openhab.org/administration/logging.html

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… :frowning:

Not sure if it will work but…

# Logger - Measure.log
log4j2.logger.Measure.name = measure
log4j2.logger.Measure.level = INFO
log4j2.logger.Measure.additivity = false
log4j2.logger.Measure.appenderRefs = Measure
log4j2.logger.Measure.appenderRef.Measure.ref = Measure

log4j2.appender.Measure.name = MEASURE
log4j2.appender.Measure.type = RollingRandomAccessFile
log4j2.appender.Measure.fileName = /home/openhabia/Measure.log
log4j2.appender.Measure.filePattern = /home/openhabia/Measure.log.%i
log4j2.appender.Measure.immediateFlush = true
log4j2.appender.Measure.append = true
log4j2.appender.Measure.layout.type = PatternLayout
log4j2.appender.Measure.layout.pattern = %d{dd-MMM-yyyy HH:mm:ss.SSS} [%-5.5p] [%-50.50c] - %m%n
log4j2.appender.Measure.policies.type = Policies
log4j2.appender.Measure.policies.size.type = SizeBasedTriggeringPolicy
log4j2.appender.Measure.policies.size.size = 10MB
log4j2.appender.Measure.strategy.type = DefaultRolloverStrategy
log4j2.appender.Measure.strategy.max = 10

How I make use of it now…?

Log using logInfo("MEASURE","your logging statement")

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

Events log:
==> /var/log/openhab2/openhab.log <==

2017-12-27 22:14:00.042 [INFO ] [lipse.smarthome.model.script.Measure] - Gas Information, 2017-12-27, 22:14:00.019, NULL

But no new logfile…

Case matters:

logInfo(“Measure”

log4j2.appender.Measure.name = MEASURE

log4j2.logger.Measure.name = measure

log4j2.appender.Measure.name = MEASURE

Should those 2 have the same name then?