Unable to read from log files in Smart Home Designer

I have created a custom file for logging specific events.

Now I want to read the timestamp of the events into variables in the designer and set up rules to auto trigger the events. But I am unable to find the correct syntax to read the log file into the designer.

Am using the below rule to write to the file:

rule "User Pattern ON"
     when
     Item MySwitch changed from OFF to ON
     then
     var SimpleDateFormat df = new SimpleDateFormat( "YYYY-MM-dd HH:mm:ss" )
     var String Timestamp = df.format( new Date() )
     logInfo("Demo",Timestamp + " TimeStamp for user pattern on")
     end
     
     
    rule "User Pattern OFF"
     when
     Item MySwitch changed from ON to OFF
     then
     var SimpleDateFormat df = new SimpleDateFormat( "YYYY-MM-dd HH:mm:ss" )
     var String Timestamp = df.format( new Date() )
     logInfo("Demo",Timestamp + " TimeStamp for user pattern off")
     end     

Now when I try to read the file, using the below syntax, the designer points out that it is incorrect.

 rule "Parse the LOG"    
 when
     Time cron "0 * * * * ?"
 then
	     
    file = open("Demo.log",'r')
    
 end

Can someone tell me where I am going wrong?

Why are you trying to get the values from the log file instead of a dummy item and variable persistence? You can create a Datetime item, and assign the value in rules to that item. Adding persistence makes the values reachable historically.

I am completely new to openHAB, can you please give me any guides/links where I can know how to do it?

I have read somewhere that it is possible using persistence with MySQL/JDBC but I am having difficulties trying to implement the same.

Well you need to explain us what exactly are you trying to achieve. You know you can always read the current switch position with “MySwitch.state” if you do not need historical data. You can also change the switch position with “SendCommand (MySwitch, OFF)”. I recommend you check the Weather items usage on demo files and RRDJ4 persistence. That’s a great start for beginners. Please open up what exactly are you trying to achieve so we can be of help.

Well I just started using OpenHAB a month ago for my Master’s project. I have a homematic CCU with a Actuator(Switch) and am running OH2 on a raspberry pi to control the system.

I wanted to check if it is possible to get a user statistic(like timings of the day when the user turns on or off a switch) and then based on the report develop a system which can automatically switch the system based on the user history reports(like if the user is not at home).

Since OH2 logs events in the event.log file, I though this might be possible by retrieving the data from that file. I am also trying to understand the persistence method from the different threads in the community(I did set up mysql/jdbc plugins through paper UI and set up a DB too, but I can only find entries for item name in the tables, but nothing related to timestamp which I need). Not sure which would be correct approach since I am not getting any documentations.

Sourajit, i do not know if what you want is a requirement on your project or not, but you can always use a smartphone presence if you want to check if a user is at home or not. Android is easier compared to iphone but you can use both smartphones.
I will just put my examples regarding on weather temperature data on RRDJ4 persistence:

items file:

Group  Weather_Chart								<chart>
Group  Weather					"Weather"			<temperature>
Number Weather_Temperature      "Sıcaklık [%.1f °C]"                  <temperature>   (Weather, Weather_Chart) 	[ "CurrentTemperature" ] { channel = "yahooweather:weather:83fbd0f2:temperature" }
Number Weather_Temp_Max         "Bugünkü En Yüksek [%.1f °C]"         <temperature>   (Weather, Weather_Chart)
Number Weather_Temp_Min         "Bugünkü En Düşük [%.1f °C]"          <temperature>   (Weather, Weather_Chart)
Number Weather_Chart_Period     "Grafik Periyodu"					  <chart>
Number YahooWeatherCode         "Bugün [MAP(yahoo_weather_code.map):%s]"  <yahoo_weather> (Weather) { http="<[weatherCache:60000:XSLT(yahoo_weather_code.xsl)]"}
DateTime Weather_LastUpdate     "Son Güncelleme [%1$ta %1$tR]"            <clock>

sitemap file:

Text item=Weather_Temperature valuecolor=[Weather_LastUpdate=="NULL"="lightgray",Weather_LastUpdate>90="lightgray",>25="orange",>15="green",>5="orange",<=5="blue"] {
			Frame {
				Text item=Weather_Temp_Max valuecolor=[>25="orange",>15="green",>5="orange",<=5="blue"]
				Text item=Weather_Temp_Min valuecolor=[>25="orange",>15="green",>5="orange",<=5="blue"]
				Text item=Weather_LastUpdate visibility=[Weather_LastUpdate>30] valuecolor=[Weather_LastUpdate>120="orange", Weather_LastUpdate>300="red"]
			}
			Frame {
				Switch item=Weather_Chart_Period label="Grafik Periyodu" icon="chart" mappings=[0="Hour", 1="Day", 2="Week"]
				Chart item=Weather_Chart period=h refresh=600 visibility=[Weather_Chart_Period==0, Weather_Chart_Period=="NULL"]
				Chart item=Weather_Chart period=D refresh=3600 visibility=[Weather_Chart_Period==1]
				Chart item=Weather_Chart period=W refresh=3600 visibility=[Weather_Chart_Period==2]
			}

rrdj4.persist file:

// persistence strategies have a name and a definition and are referred to in the "Items" section

Strategies {
	everyMinute : "0 * * * * ?"
}

Items {
	// let's only store temperature values in rrd
	Weather_Chart* : strategy = everyMinute, restoreOnStartup
}

// vim: syntax=Xtend

And please check page: https://github.com/openhab/openhab1-addons/wiki/persistence for Persistence Extensions in Scripts and Rules. Please note that i use RRDJ4 for persistence database. Hope this will be of help.

You can use REST API to get histocial values from persistence database, whethet you use rrdj4 or any other database, like:

{
  "name": "Weather_Temperature",
  "datapoints": "360",
  "data": [
    {
      "time": 1495389840000,
      "state": "20"
    },
    {
      "time": 1495390080000,
      "state": "20"
    },
    {
      "time": 1495390320000,
      "state": "20"
    },
    {
      "time": 1495390560000,
      "state": "20"
    },

You could of course make your own timestamps using another Item and Rules.

rule "trip switch"
	when
		Item my_switch changed
	then
		my_timestamp_item.postUpdate(new DateTimeType())   // defaults to now
end

You could extend the idea to capture ON and OFF events separately.