syntax of writing to file for import in excel

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
BartSr
Posts: 395
Joined: Sunday 03 July 2016 16:16
Target OS: Raspberry Pi / ODroid
Domoticz version: V2024.7
Location: Netherlands
Contact:

syntax of writing to file for import in excel

Post by BartSr »

Hi!
In a script I want to save data to a text-file for analysing in excel
The code hereunder works fine.

Code: Select all

file = io.open("/opt/domoticz/userdata/logs/gas.txt", "a+")
file:write(datetimestamp.." gasstand ".."0000.000 ".."0000.000 "..gasstand.." \n")
file:close()
 
I want to import in excel to get a result like:
in seperate columns
-[datetimestamp] gasstand
-0000.000
-0000.000
-[value of var gasstand]

So there should be ";" inserted in the saved string. Anybody who can help me hoe to insert in the code that 'column-seperator'
just by inserting ";" or "," generates error

Code: Select all

	    file:write(datetimestamp.." gasstand "..";".."0000.000 "..";".."0000.000 "..gasstand.." \n")

2023-10-21 14:00:49.410 Error: dzVents: Error: (3.1.8) error loading module 'Gasmeter2' from file '/opt/domoticz/userdata/scripts/dzVents/generated_scripts/Gasmeter2.lua':
2023-10-21 14:00:49.410 ...userdata/scripts/dzVents/generated_scripts/Gasmeter2.lua:26: unfinished string near <eof> 
Anyone who can tell me what's wrong and how to solve?

TIA
-Bart
Raspberry pi 3b
Arduino
KAKU
RfxCom
Zwave2MQTT
OTGW
Chinese sensors temp (Dallas),movement
Tasmota
Esp8266 / 32 espeasy
Zigbee2MQTT
User avatar
jvdz
Posts: 2328
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: syntax of writing to file for import in excel

Post by jvdz »

Not sure why you would get that error from that line as it looks like all literal strings are correct, but for starters, i would simplify that statement to:

Code: Select all

file:write(datetimestamp..";gasstand;0000.000;0000.000;"..gasstand.."\n")
When the error persists, could you share the total script so we can check?
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
BartSr
Posts: 395
Joined: Sunday 03 July 2016 16:16
Target OS: Raspberry Pi / ODroid
Domoticz version: V2024.7
Location: Netherlands
Contact:

Re: syntax of writing to file for import in excel

Post by BartSr »

very strange behaviour

Code: Select all

return {
	on = {
		    timer = {
		        'every minute',
		        'at 07:00 on mon,tue,wed,thu,fri',
		        'at 19:00 on mon,tue,wed,thu,fri',
		        },
	     },
			
	logging = {	level = domoticz.LOG_INFO,marker = 'BSOtest',},
    	
    	execute = function(dz, timer)

	    local gasstand = dz.devices(149).counter -- 149 is IDX gasmeter
	    local datetimestamp = tostring(os.date('%Y-%m-%d %H:%M:%S'))
	    
	    print (gasstand)
	    print (datetimestamp)

       file = io.open("/opt/domoticz/userdata/logs/gas.txt", "a+")
       file:write(datetimestamp.." gasstand ".."0000.000 ".."0000.000 "..gasstand.." \n")
       file:close()
   end
   
}

then after I inserted the new line.....

return {
	on = {
		    timer = {
		        'every minute',
		        'at 07:00 on mon,tue,wed,thu,fri',
		        'at 19:00 on mon,tue,wed,thu,fri',
		        },
	     },
			
	logging = {	level = domoticz.LOG_INFO,marker = 'BSOtest',},
    	
    	execute = function(dz, timer)

	    local gasstand = dz.devices(149).counter -- 149 is IDX gasmeter
	    local datetimestamp = tostring(os.date('%Y-%m-%d %H:%M:%S'))
	    
	    print (gasstand)
	    print (datetimestamp)

       file = io.open("/opt/domoticz/userdata/logs/gas.txt", "a+")
       --file:write(datetimestamp.." gasstand ".."0000.000 ".."0000.000 "..gasstand.." \n")
       file:write(datetimestamp.."

 
after I inserted your suggested line I saved and once open again the script it looks like second scripe. so part of script got lost....
Raspberry pi 3b
Arduino
KAKU
RfxCom
Zwave2MQTT
OTGW
Chinese sensors temp (Dallas),movement
Tasmota
Esp8266 / 32 espeasy
Zigbee2MQTT
User avatar
jvdz
Posts: 2328
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: syntax of writing to file for import in excel

Post by jvdz »

Strange indeed! The top part (original script) doesn't have any syntax error as far as I can see, so much be something wrong in the database with this script. Maybe disable it and create a new one with the same code for testing?
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
willemd
Posts: 649
Joined: Saturday 21 September 2019 17:55
Target OS: Raspberry Pi / ODroid
Domoticz version: 2024.1
Location: The Netherlands
Contact:

Re: syntax of writing to file for import in excel

Post by willemd »

You can also use other separators in excel and therefore just leave your code as-is.
1) import it into excel, it will show in one column
2) then select the column, go to menu data, choose text to columns, choose space as separator and click finish
BartSr
Posts: 395
Joined: Sunday 03 July 2016 16:16
Target OS: Raspberry Pi / ODroid
Domoticz version: V2024.7
Location: Netherlands
Contact:

Re: syntax of writing to file for import in excel

Post by BartSr »

@jvdz
I will try but can't explain
@willemd
thanks for your suggestion, I have never been aware of this feature
Raspberry pi 3b
Arduino
KAKU
RfxCom
Zwave2MQTT
OTGW
Chinese sensors temp (Dallas),movement
Tasmota
Esp8266 / 32 espeasy
Zigbee2MQTT
User avatar
waltervl
Posts: 5853
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: syntax of writing to file for import in excel

Post by waltervl »

Using a semicolon in a script caused a save error. This bug has been fixed recently https://github.com/domoticz/domoticz/issues/5823
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
BartSr
Posts: 395
Joined: Sunday 03 July 2016 16:16
Target OS: Raspberry Pi / ODroid
Domoticz version: V2024.7
Location: Netherlands
Contact:

Re: syntax of writing to file for import in excel

Post by BartSr »

Waltervl, happy to see this was a bug. So as far as I understand the last stable version might be used. But..... I never did such before. Always waited for the message in Domoticz about new version. Is there a kind of guide how to deal with (getting update up and running).
Thanks for your post!
Bart
Raspberry pi 3b
Arduino
KAKU
RfxCom
Zwave2MQTT
OTGW
Chinese sensors temp (Dallas),movement
Tasmota
Esp8266 / 32 espeasy
Zigbee2MQTT
User avatar
waltervl
Posts: 5853
Joined: Monday 28 January 2019 18:48
Target OS: Linux
Domoticz version: 2024.7
Location: NL
Contact:

Re: syntax of writing to file for import in excel

Post by waltervl »

Change check for beta in menu setup - settings. Be aware that current beta does not support OpenZwave.
Also switch off automatic checking for updates in menu setup settings.

Perhaps it is safer for you to keep using stable and not using a semicolon as seperator for CSV file. You can also use ! or | or some other seperator
Domoticz running on Udoo X86 (on Ubuntu)
Devices/plugins: ZigbeeforDomoticz (with Xiaomi, Ikea, Tuya devices), Nefit Easy, Midea Airco, Omnik Solar, Goodwe Solar
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest