Create txt File

Dear forum members,

I wanted to ask if there is a solution, how to write in the .rules file via a command a String in a txt file.

Sorry for the bad English!


You can implement it using Java IO in your Rule but you probably should not be doing this.

What are you trying to accomplish? There is almost certainly a better way (e.g. persistence).

Perhaps you can execute an Bash Script in the .rules … and the Bash Script write the TXT File …

Let us know what you want write in the TXT … perhaps there is a better Solution …

Perhaps this helps … I think you are a Native German Speaker :wink:

I have an RFID reader that sends the TAG ID to the OpenHab 2 server via MQTT.
I would like to record in a txt file, who and at what time has read his TAG.

I would use persistence for this, that way, you have the events in a database and use SQL queries to read it from your consumer.


Or you could store the value in an item (I guess that’s what you are already doing) and use the openHAB REST Api to retrieve it whenever you want to. The detour through a file is possible but just not as clean as possible other solutions.

If you are interested in a better solution, it might help to tell us what’s your intended next step with the created file.

Could you please give me an example? So the individual steps I have to do. I’m still a beginner and I need a few bases.

o.k., from my setup, for InfluxDB based persistence, but you can adapt this to JDBC based persistence very easily:

in items/sensebox-terasse.items:

Group gInflux
Number  Terasse_Sensebox_Luminance        "Terasse Light Level [%.1f lx]"      <light>         (Weather, gInflux)

the important part is the “(gInflux)” group membership in the item.

Then, in persistence/influxdb.persist:

Items {
        gInflux* : strategy = everyChange, everyHour
        gInfluxDaily* : strategy = everyChange, everyDay

Here, the important part is the “*” after the group name. I missed it in my first configurations, and it took me ages to find the simple syntax error.

Then, in services/influxdb.cfg,

password=nope-i-won't-tell-you :slight_smile: 

See also the threads:

I have a number of areas in my interface dedicated to having a GUI to adjust on and off times during the week (WAF requirement). However to make this “responsive” I am running a cron job to check these values every minute and lots of timers

I think a better option would be a GUI that had a “save” button that when pressed it runs a rule that then creates a rule file with the values in the GUI to create cron jobs that reflect what’s needed

Anyone see a better option?

Presumably the start and stop times are stepped in items?

I’d recommend:

  • persist these items and restoreOnStartup
  • have a rule or rules that will creates a Timer for each of these based on the start and stop times when ever they change
  • have a System started rule to create the Timers on OH restart (there may need to be a delay to and the startup timing bug)
  • depending on how these timers work, a role that tons around midnight to reschedule timers for the coming day.

This will be immediately performant, doesn’t require polling, and certainly doesn’t require code to write code.

Hi Rich, wow this is why I love this community - I have only ever creates timers based on now + something - never even considered I can make a timer at a set time!
If you dont mind having a look at the rule and help convert it i’d really appreciate your keen eye

var Timer Bedroom1_EB1_Switch_timer = null
    rule "Mums EB time"
        Time cron "0 0/15 * * * ?" or
       Item Bedroom1_EB1_Switch_StartClockTimeHour changed or 
		Item Bedroom1_EB1_Switch_StartClockTimeMinute changed or
		Item Bedroom1_EB1_Switch_EndClockTimeHour changed or 
		Item Bedroom1_EB1_Switch_EndClockTimeMinute changed or
		Item Bedroom1_EB1_Switch_AlarmMonday changed or	
		Item Bedroom1_EB1_Switch_AlarmTuesday changed or
		Item Bedroom1_EB1_Switch_AlarmWednesday changed or
		Item Bedroom1_EB1_Switch_AlarmThursday changed or
		Item Bedroom1_EB1_Switch_AlarmFriday changed or
		Item Bedroom1_EB1_Switch_AlarmSaturday changed or
		Item Bedroom1_EB1_Switch_AlarmSunday changed or
		Item Bedroom1_EB1_Switch_Temp changed or
		Item Proxy_Bedtime_switch changed
		if (Away_switch.state != ON) {
			//calculate mins since midnight for start time set
				  var int alarm1start
			  alarm1start = (Bedroom1_EB1_Switch_StartClockTimeHour.state as DecimalType).intValue * 60 + 
						  (Bedroom1_EB1_Switch_StartClockTimeMinute.state as DecimalType).intValue
			  alarm1start = alarm1start.intValue   

				  var int alarm1end
			  alarm1end = (Bedroom1_EB1_Switch_EndClockTimeHour.state as DecimalType).intValue * 60 + 
						  (Bedroom1_EB1_Switch_EndClockTimeMinute.state as DecimalType).intValue
			  alarm1end = alarm1end.intValue   

			//calculate mins since midnight for now()  
			var int now1
			  now1 = now.getMinuteOfDay
			  now1 = now1.intValue

				   var Number day = now.getDayOfWeek
				if (((day == 1) && (Bedroom1_EB1_Switch_AlarmMonday.state == ON))     ||
					((day == 2) && (Bedroom1_EB1_Switch_AlarmTuesday.state == ON))   ||
					((day == 3) && (Bedroom1_EB1_Switch_AlarmWednesday.state == ON))   ||
					((day == 4) && (Bedroom1_EB1_Switch_AlarmThursday.state == ON)) ||
					((day == 5) && (Bedroom1_EB1_Switch_AlarmFriday.state == ON))    ||
					((day == 6) && (Bedroom1_EB1_Switch_AlarmSaturday.state == ON))    ||
					((day == 7) && (Bedroom1_EB1_Switch_AlarmSunday.state == ON))
					) {

					if ((now1 < alarm1start) || (now1 > alarm1end)  || (Proxy_Bedtime_switch.state == ON)  || (Away_switch.state == ON) || ((Bedroom1_Temp_Temperature.state as DecimalType).intValue > (Bedroom1_EB1_Switch_Temp.state as DecimalType).intValue)) 
							if (Bedroom1_EB1_Switch_OnceOff.state != ON) {

					} else {			
						if ( (Proxy_Bedtime_switch.state == OFF)  && (Away_switch.state == OFF) && ((Bedroom1_Temp_Temperature.state as DecimalType).intValue < (Bedroom1_EB1_Switch_Temp.state as DecimalType).intValue)) {
				} else {

					if (Bedroom1_EB1_Switch_OnceOff.state != ON) {

my items

Switch	    Bedroom1_EB1_Switch			"Mum's Electric Blanket"	[ "Switchable" ]	 { channel="zwave:device:Controller:node43:switch_binary" }  
Number      Bedroom1_EB1_Switch_Power 	"EB1 Power [%.2f W]" (gCurrentPower)	{ channel="zwave:device:Controller:node43:meter_watts" } 
Number      Bedroom1_EB1_Switch_Energy 	"EB1 Consumption [%.2f KWh]" (gPower)	 { channel="zwave:device:Controller:node43 :meter_kwh" }
Switch   Bedroom1_EB1_Switch_AlarmMonday	"Monday"     <switch>   
Switch   Bedroom1_EB1_Switch_AlarmTuesday   "Tuesday"   <switch>  
Switch   Bedroom1_EB1_Switch_AlarmWednesday   "Wednesday"   <switch>   
Switch   Bedroom1_EB1_Switch_AlarmThursday "Thursday" <switch>   
Switch   Bedroom1_EB1_Switch_AlarmFriday    "Friday"    <switch>   
Switch   Bedroom1_EB1_Switch_AlarmSaturday    "Saturday"    <switch>   
Switch   Bedroom1_EB1_Switch_AlarmSunday    "Sunday"    <switch>   
Number  Bedroom1_EB1_Switch_StartClockTimeHour "Start Hour [%d]"  
Number  Bedroom1_EB1_Switch_StartClockTimeMinute "Start Minute [%d]"  
Number  Bedroom1_EB1_Switch_EndClockTimeHour "End Hour [%d]"  
Number  Bedroom1_EB1_Switch_EndClockTimeMinute "End Minute [%d]" 
Switch  Bedroom1_EB1_Switch_OnceOff	"Mum's Electric Blanket Once Off"    <switch> 
Number  Bedroom1_EB1_Switch_OnceOff_Duration "Minutes [%d]"  
DateTime Bedroom1_EB1_Switch_Time "Going off at [%1$tH:%1$tM]"
Number  Bedroom1_EB1_Switch_Temp "Off if warmer than [%d]" 


Frame label="Power" {
				Switch item=Bedroom1_EB1_Switch label="Mum's Electric Blanket [%s]" icon="switch"
				Text item=Bedroom1_EB1_Switch_Time	icon="time" visibility=[Bedroom1_EB1_Switch_OnceOff==ON]	
				Text label="Mum's Electic Blanket Schedule" icon="settings" {
					Frame label="Power" {
						Switch item=Bedroom1_EB1_Switch label="Mum's Electric Blanket [%s]" icon="switch"
						Text item=Bedroom1_EB1_Switch_Power icon="battery"					
						Text item=Bedroom1_EB1_Switch_Energy icon="battery"
					Frame label="Manual Control" {
						Switch item=Bedroom1_EB1_Switch_OnceOff 		
						Setpoint item=Bedroom1_EB1_Switch_OnceOff_Duration icon="time" minValue=0 maxValue=1440 step=5
						Text item=Bedroom1_EB1_Switch_Time	icon="time" visibility=[Bedroom1_EB1_Switch_OnceOff==ON]	

					Frame label="Temp Control" {
						Setpoint item=Bedroom1_EB1_Switch_Temp icon="temperature" minValue=10 maxValue=30 step=1
						Text item=Bedroom1_Temp_Temperature label="Current Temp"
					Frame label="Start and End Time" {
						Setpoint item=Bedroom1_EB1_Switch_StartClockTimeHour icon="time" minValue=0 maxValue=23 step=1						
						Setpoint item=Bedroom1_EB1_Switch_StartClockTimeMinute icon="time" minValue=0 maxValue=55 step=5						
						Setpoint item=Bedroom1_EB1_Switch_EndClockTimeHour icon="time" minValue=0 maxValue=23 step=1						
						Setpoint item=Bedroom1_EB1_Switch_EndClockTimeMinute icon="time" minValue=0 maxValue=55 step=5						
					Frame label="Day" {
						Switch item=Bedroom1_EB1_Switch_AlarmMonday						
						Switch item=Bedroom1_EB1_Switch_AlarmTuesday						
						Switch item=Bedroom1_EB1_Switch_AlarmWednesday						
						Switch item=Bedroom1_EB1_Switch_AlarmThursday						
						Switch item=Bedroom1_EB1_Switch_AlarmFriday						
						Switch item=Bedroom1_EB1_Switch_AlarmSaturday						
						Switch item=Bedroom1_EB1_Switch_AlarmSunday						

sitemap image

Well, when you use now + something you are basically setting it to a set time. :slight_smile:

Some things I notice with the Rule:

  1. Put all the Bedroom1_EB_StartClockTime* Items Items in a Group:Number, and the rest in a Group:Switch, and maybe even add the Proxy_Bedtime_switch to the Group:Switch as well and change the Rule Trigger to use Member of :

  2. Assuming you are using some Asto events, change the cron trigger to sometime after midnight, at least a couple minutes.

  3. Change the names of your Switch Items to use the Number instead of the name of the day of the week so we can pull the Item out of the Group by name. So Bedroom1_EB1_Switch_AlarmThursday becomes Bedroom1_EB1_Switch_Alarm4. NOTE: I believe Sunday comes back as 0, not 7.

var alarmTimer = null

rule "Mums EB time"
    Member of Bedroom1_EB1_Times changed or
    Member of Bedroom1_EB1_Switches changed or
    Time cron "0 10 0 * * ?" or
    System started
    // Cancel the outstanding timer

    // Don't schedule an alarm if there isn't one set for today
    if(Bedroom1_EB1_Switches.members.findFirst[ s | == "Bedroom1_EB1_Switch_Alarm"+now.getDayOfWeek ].state != ON) return;

    // Schedule the alarm for today (plusDays1 minus Hours to deal with daylight savings days)
    val alarm1start = now.plusDays(1).minusHours(24 - Bedroom1_EB1_Switch_StartClockTimeHour.state).plusMinutes(Bedroom1_EB1_Switch_StartClockTimeMinute.state)
    val alarm1End   = now.plusDays(1).minusHours(24 - Bedroom1_EB1_Switch_EndClockTimeHour.state).plusMinutes(Bedroom1_EB1_Switch_EndClockTimeMinute.state)

    alarmTimer = createTimer(alarm1Start, [ |

        if(Away_switch.state == ON ||
           Proxy_Bedtime_switch.state == ON || 
           Bedroom1_EB1_Switch_OnceOff.state != ON || 
           Bedroom1_Temp_Temperature.state > Bedroom1_EB1_Switch_Temp.state) {
        else if (Away_switch.state == OFF &&
                    Proxy_Bedtime_switch.state == OFF &&
                    Bedroom1_Temp_Temperature.state < Bedroom1_EB1_Switch_Temp.state) {
            alarmTimer = createTimer(alarm1End, [ | Bedroom1_EB1_Switch.sendCommand(OFF) ]

I don’t know if this does exactly what you want and I just typed it in. You should study it and make sure you understand how it works so you can expand it and adjust it as needed.

farout thanks so much that looks awesome and far better than what I posted - will get it into action! Thanks Rich