[SOLVED] Read data from file

Dear community,

I am new to Open HAB and I’d like to do the following:

I have some data on my raspberry pi, stored in files (measurement data in ascii format). I want to read this with open hab (to store it using persistency).

My question is: is it necessary to write a new binding or are there some features, which enable my to achieve my planned task?

Thanks for your help.

the Rules DSL is able to create file objects with

import java.io.File

you need to use the constructor for an instance

var myFile = new File(PathAsURL)

Here are the docs for the class:
https://docs.oracle.com/javase/7/docs/api/java/io/File.html

1 Like

So the answer is yes, I do need a new binding?

No, you don’t need a binding, use rules dsl

No, just make a cronjob to postUpdate all items with the stored values.
But I’ve never used Files with this purpose, hope it works!

Thanks, understood.

I did, it works just like in Java.

perfect! you can mark the answer as solution, to end this thread :slight_smile:

Could you please mark @Nico111 ‘s post (number 2 in this thread) as the solution? I only confirmed his solution afterwards!
Thank you!

There are other options as well. For example:

  • var file = executeCommandLine("cat /path/to/file")
  • external script to push the file contents to OH using the REST API or MQTT
  • Java File IO of course
2 Likes

thank you for sharing but my data content is always null

import java.io.File
//package io; 

rule "Schedule log MQTT"
when
	//	Time cron "0 0 4 1/1 * ? *" or Time cron "0 5 16 1/1 * ? *"
	Item DummyEvent changed
then
    var file =executeCommandLine("cat /etc/openhab2/scripts/MQTT/broadlink/mqtt_A1_1/test.txt")
	logInfo("myFile",file)
end

in the logs, the file content is always null

[ome.event.ItemCommandEvent] - Item 'DummyEvent' received command ON
[vent.ItemStateChangedEvent] - DummyEvent changed from OFF to ON
==> /var/log/openhab2/openhab.log <==
[INFO ] [lipse.smarthome.io.net.exec.ExecUtil] - executed commandLine 'cat /etc/openhab2/scripts/MQTT/broadlink/mqtt_A1_1/test.txt'
[INFO ] [clipse.smarthome.model.script.myFile] - null

the file exists and is not empty

what i am missing?

You need to give executeCommandLine a timeout. Without the timeout it runs the script in the background and immediately returns without giving you the result of the script. The timeout is ni milliseconds.

executeCommandLine("script to run", 1000)

I neglected to include the timeout in my example above.

1 Like

Thank you. i have made a nice ui with Habpanel to display local logs content

import java.io.File
//package io; 

rule "Order log MQTT"
when
        //      Time cron "0 0 4 1/1 * ? *" or Time cron "0 5 16 1/1 * ? *"
        Item CollectmqttLog received command
then
    val file =executeCommandLine("cat@@/etc/openhab2/scripts/MQTT/broadlink/"+ 
    getFilelog.state.toString +"/mqtt.log", 1000)
    logInfo("myFile",file)
    outputgetFilelog.postUpdate(file)
// cat mqtt.log |head -10|tail -2
end

@rlkoshak, i have 3 questions please

1. The lines are concatenated.


is it possible to display the last log row ?
i have tried a command with " // cat mqtt.log |head -10|tail -2" without success

2. get a service status

How could i get update an item with a service status

In the perfect world, i would want to update two different items with the squared rows

Thanks again for your help

all your propositions or enhancements are welcome.

This is a HABPanel question. I don’t use HABPanel. But it does look like the text is center justified. Yoo might try left justification which might be easier to read.

The last line isn’t truncated, it wraps to the next line. The first part of that line is on the previous line.

The “correct” approach would probably be to use the snmp binding.

The easiest would be to use the Network binding with a servicedevice Thing to ping port 1883 (or whatever port your MQTT broker uses. If the service goes down the Switch will go OFF.

Beyond that, you would use the Exec binding and parse the output.