Device uptime


I would like to get the date and timestamp of the latest device turned on then turned off. I can ping the device but I don’t know how to show that in openhab.

Is it possible to write something in Python that will write something in a text file then ask opehab to print it somewhere?

What I would do is ping the device (starting for down state for example) and if up, it writes the timestamp for up then when down, it writes the timestamp for down state then the time difference then next line and restart when device goes up.

Thanks to let me know how to do this.


You can use the REST API with python.
Pseudo code:

#Get item state
curl -X GET --header “Accept: text/plain” “http://localhost:8080/rest/items/door_contact_main/state
#STDOUT to file
#STDIN read from file

I hope I can lead you into the right direction

When you say “ping the device” do you literally mean the ping command or are you talking figuratively?

If you are literally doing a network ping, then just use the Network binding. Then create a Rule that triggers when that switch changes state. If it changed to ON you know the device came up. If it changed to OFF you know the device went down.

From there you can populate a DateTime Item with the current time.

In another rule that triggers periodically using a Time cron trigger, calculate the difference between the DateTime Item and now and populate an Item with that difference to get your Uptime/Downtime.