# Create txt File

**URL:** <https://community.openhab.org/t/create-txt-file/34115>\
**Category:** Scripts & Rules\
**Created:** [September 11, 2017, 5:38pm UTC](https://community.openhab.org/t/create-txt-file/34115 "2017-09-11T17:38:08Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![theo416](https://community.openhab.org/letter_avatar_proxy/v4/letter/t/d6d6ee/32.png) [@theo416](https://community.openhab.org/u/theo416)\
**Post date:** [September 11, 2017, 5:38pm UTC](https://community.openhab.org/t/create-txt-file/34115/1 "2017-09-11T17:38:08Z")

</div>

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!

Thanks!

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [September 11, 2017, 6:41pm UTC](https://community.openhab.org/t/create-txt-file/34115/2 "2017-09-11T18:41:26Z")

</div>

You can implement it using [Java IO in your Rule](https://docs.oracle.com/javase/tutorial/essential/io/) but you probably should not be doing this.

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

---

<div class="post-metadata">

**Author:** ![ei\_Gelb\_Geek](https://community.openhab.org/user_avatar/community.openhab.org/ei_gelb_geek/32/14503_2.png) [@ei\_Gelb\_Geek](https://community.openhab.org/u/ei_Gelb_Geek)\
**Post date:** [September 11, 2017, 7:53pm UTC](https://community.openhab.org/t/create-txt-file/34115/3 "2017-09-11T19:53:37Z")

</div>

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 …

---

<div class="post-metadata">

**Author:** ![ei\_Gelb\_Geek](https://community.openhab.org/user_avatar/community.openhab.org/ei_gelb_geek/32/14503_2.png) [@ei\_Gelb\_Geek](https://community.openhab.org/u/ei_Gelb_Geek)\
**Post date:** [September 11, 2017, 7:56pm UTC](https://community.openhab.org/t/create-txt-file/34115/4 "2017-09-11T19:56:53Z")

</div>

Perhaps this helps .. I think you are a Native German Speaker 😉

> **[Java Text in eine Datei schreiben :: Ein Karlsruher Bloggt!](http://blog.mynotiz.de/programmieren/java-text-in-eine-datei-schreiben-450/)**

---

<div class="post-metadata">

**Author:** ![theo416](https://community.openhab.org/letter_avatar_proxy/v4/letter/t/d6d6ee/32.png) [@theo416](https://community.openhab.org/u/theo416)\
**Post date:** [September 12, 2017, 8:09am UTC](https://community.openhab.org/t/create-txt-file/34115/5 "2017-09-12T08:09:49Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![hakan](https://community.openhab.org/user_avatar/community.openhab.org/hakan/32/70815_2.png) [@hakan](https://community.openhab.org/u/hakan)\
**Post date:** [September 12, 2017, 12:32pm UTC](https://community.openhab.org/t/create-txt-file/34115/6 "2017-09-12T12:32:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ThomDietrich](https://community.openhab.org/user_avatar/community.openhab.org/thomdietrich/32/71239_2.png) [@ThomDietrich](https://community.openhab.org/u/ThomDietrich)\
**Post date:** [September 12, 2017, 12:42pm UTC](https://community.openhab.org/t/create-txt-file/34115/7 "2017-09-12T12:42:55Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![theo416](https://community.openhab.org/letter_avatar_proxy/v4/letter/t/d6d6ee/32.png) [@theo416](https://community.openhab.org/u/theo416)\
**Post date:** [September 16, 2017, 5:03pm UTC](https://community.openhab.org/t/create-txt-file/34115/8 "2017-09-16T17:03:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![hakan](https://community.openhab.org/user_avatar/community.openhab.org/hakan/32/70815_2.png) [@hakan](https://community.openhab.org/u/hakan)\
**Post date:** [September 17, 2017, 10:00am UTC](https://community.openhab.org/t/create-txt-file/34115/9 "2017-09-17T10:00:17Z")

</div>

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,

```
url=http://my-influxdb-host:8086
user=my-secret-username
password=nope-i-won't-tell-you :slight_smile: 
db=openhab_db
retentionPolicy=autogen

```

See also the threads:

> [@Design Pattern: Group Based Persistence](https://community.openhab.org/t/design-pattern-group-based-persistence/15098):
>
> Edit: Updated for OH 4 See [Design Pattern: What is a Design Pattern and How Do I Use Them](https://community.openhab.org/t/design-pattern-what-is-a-design-pattern-and-how-do-i-use-them/40319) for an explanation of what a DP is and how to use them. Problem Statement There are several reasons why a home automation enthusiast would want to use openHAB persistence: charting, using historical Item states in rules logic, restore previous values on openHAB restart, detailed analysis, access to the data with external tools, etc. By default, a persistence add-on will implement a default strategy which…

> [@InfluxDB+Grafana persistence and graphing](https://community.openhab.org/t/influxdb-grafana-persistence-and-graphing/13761):
>
> Store your openHAB item states in an [InfluxDB](https://influxdata.com/time-series-platform/influxdb) time-series datastore and create highly customizable diagrams with [Grafana](http://grafana.org). These diagrams can in return be included in your sitemap as images. [image] Following this guide, you will install InfluxDB and Grafana, make openHAB store data in an InfluxDB database, make Grafana fetch data from the InfluxDB database and draw diagrams. Finally you can include these diagrams in your openHAB sitemap. Table of Content [Starting Point](#starting-point)[InfluxDB Installation …](#influxdb-setup)

---

<div class="post-metadata">

**Author:** ![Andrew\_Pawelski](https://community.openhab.org/user_avatar/community.openhab.org/andrew_pawelski/32/85454_2.png) [@Andrew\_Pawelski](https://community.openhab.org/u/Andrew_Pawelski)\
**Post date:** [October 1, 2018, 12:29am UTC](https://community.openhab.org/t/create-txt-file/34115/10 "2018-10-01T00:29:06Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [October 1, 2018, 12:39am UTC](https://community.openhab.org/t/create-txt-file/34115/11 "2018-10-01T00:39:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Andrew\_Pawelski](https://community.openhab.org/user_avatar/community.openhab.org/andrew_pawelski/32/85454_2.png) [@Andrew\_Pawelski](https://community.openhab.org/u/Andrew_Pawelski)\
**Post date:** [October 2, 2018, 11:53am UTC](https://community.openhab.org/t/create-txt-file/34115/12 "2018-10-02T11:53:47Z")

</div>

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

```auto
var Timer Bedroom1_EB1_Switch_timer = null
    rule "Mums EB time"
    when
        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
    then
		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) {

								Bedroom1_EB1_Switch.sendCommand(OFF)
							}
					} 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)) {
							Bedroom1_EB1_Switch.sendCommand(ON)
							}
					} 
				} else {

					if (Bedroom1_EB1_Switch_OnceOff.state != ON) {
						Bedroom1_EB1_Switch.sendCommand(OFF)
						}
					}
		}
end

```

my items

```auto
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]" 

```

sitemap

```auto
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

 ![image](https://community-openhab-org.s3.dualstack.eu-central-1.amazonaws.com/original/3X/8/6/86125ad095a101d554db3b96ab52452639630908.png)

---

<div class="post-metadata">

**Author:** ![rlkoshak](https://community.openhab.org/user_avatar/community.openhab.org/rlkoshak/32/75251_2.png) [@rlkoshak](https://community.openhab.org/u/rlkoshak)\
**Post date:** [October 2, 2018, 5:25pm UTC](https://community.openhab.org/t/create-txt-file/34115/13 "2018-10-02T17:25:46Z")

</div>

> [@Andrew\_Pawelski](#):
>
> I have only ever creates timers based on now + something - never even considered I can make a timer at a set time!

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

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 : [Textual Rules | openHAB](https://www.openhab.org/docs/configuration/rules-dsl.html#defining-rules)

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.

```php
var alarmTimer = null

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

    // Don't schedule an alarm if there isn't one set for today
    if(Bedroom1_EB1_Switches.members.findFirst[s | s.name == "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) {
            Bedroom1_EB1_Switch.sendCommand(OFF)
        }
        else if (Away_switch.state == OFF &&
                    Proxy_Bedtime_switch.state == OFF &&
                    Bedroom1_Temp_Temperature.state < Bedroom1_EB1_Switch_Temp.state) {
            Bedroom1_EB1_Switch.sendCommand(ON)
            alarmTimer = createTimer(alarm1End, [| Bedroom1_EB1_Switch.sendCommand(OFF)]
        }
    ])
end

```

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.

---

<div class="post-metadata">

**Author:** ![Andrew\_Pawelski](https://community.openhab.org/user_avatar/community.openhab.org/andrew_pawelski/32/85454_2.png) [@Andrew\_Pawelski](https://community.openhab.org/u/Andrew_Pawelski)\
**Post date:** [October 4, 2018, 12:07pm UTC](https://community.openhab.org/t/create-txt-file/34115/14 "2018-10-04T12:07:29Z")

</div>

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