1

I'm using Rainmeter to pass air pressure values to a lua script which I then want to be written to a text file and store the last x number to be retrieved later on to produce a histogram of the pressure changes over time.

To start with I am just trying to write a string to a file as a test but I am failing and feeling quite stupid in the process.

My script has an Update function where I'm passing and returning the pressure value from Rainmeter then another function called writetofile where I'm trying to open, write and then close a text file. It is this part I am having trouble with.

My script is as follows:

function Initialize()
end -- function Initialize

function Update()
    sMyPressure = SELF:GetOption('CurrentPressure')
    return sMyPressure
end -- function Update

function writetofile()
    file = io.open("pressures.txt", "w")
    file:write("Hello World")
    file:close()
end

EDIT

I tried removing the writetofile function and moving the contents to within the update function but this didn't work and the script stopped passing the sMyPressure back to the Rainmeter script and also didn't write to the text file.

eg:

function Initialize()
end -- function Initialize

function Update()
    sMyPressure = SELF:GetOption('CurrentPressure')
    return sMyPressure
    file = io.open("pressures.txt", "w")
    file:write("Hello World")
    file:close()
end -- function Update

What am I doing wrong? the file pressures.txt exists and is writable and if I delete it, it isn't created.

Can anyone help please.

Thanks.

EDIT

Got it working.

1) I can't call a function from within Update

2) As Mike said, I needed a full path to the file

3) This path requires double backspaces \

Working code if anyone is interested or has the same issue:

function Initialize()
end -- function Initialize

function Update()
    sMyPressure = SELF:GetOption('CurrentPressure')
    writetofile(sMyPressure)
    return sMyPressure
end -- function Update

function writetofile(CurrentPressure)
    file = io.open("C:\\Users\\Hedley\\Documents\\Rainmeter\\Skins\\SimplyNova\\Wunderground\\pressures.txt", "a")
    -- file:write("hello", "\n")
    file:write(CurrentPressure, "\n")
    file:close()
end
Digital Essence
  • 307
  • 7
  • 21
  • 1
    is it complete script? because here writetofile is not called actually – shabunc Jan 12 '18 at 14:45
  • Hi, yes that's the complete script. I tried calling writetofile from within the update function but that just stopped the update function from working and I'm struggling to get my head round it. – Digital Essence Jan 12 '18 at 14:52
  • I guess `Update()` is not expected to return anything. Remove the line `return sMyPressure` – Egor Skriptunoff Jan 12 '18 at 15:00
  • 1
    you need valid path to pressures.txt – Mike V. Jan 12 '18 at 15:03
  • Hi Egor. If I remove the "return sMyPressure" from the Update function it doesn't pass the value back to my Rainmeter script. – Digital Essence Jan 12 '18 at 15:03
  • Hi Mike. I changed it to: file = io.open("C:\Users\Hedley\Documents\Rainmeter\Skins\SimplyNova\Wunderground\pressures.txt", "w") but this still doesn't work. The txt file was in the same directory as the lua script so I didn't think I required a full path. – Digital Essence Jan 12 '18 at 15:05

0 Answers0