It is currently March 29th, 2024, 1:23 pm

Funky lua call behavior

Discuss the use of Lua in Script measures.
dglexie
Posts: 3
Joined: May 27th, 2020, 1:02 pm

Funky lua call behavior

Post by dglexie »

Hi all,

I have some interpreter behavior I'm having a hard time understanding and looking to find some help. If I get this I think I'll have the key to understanding this language. First -- just so you know -- I'm a long retired developer who did the assembler/COBOL/Pascal/C route along with all the usual Unix scripting languages. No object-oriented stuff. So I prefer using old-school techniques -- old dog vs tricks thing. But if I have to go new "new school" I'll bite the bullet.

I want to convert a unix datestamp in the simplest way possible. Since we know there is no format call for doing this I decided to use lua to write a very simple function. Just pass it a dt string and it returns the hour and minutes. The lua script works fine -- I get consistent results from it.

So I'm not passing on any full scripts here as this is really a "philosophical" and/or technique question. I want to avoid working with variables that are outside of the lua script -- just pass it a value and it returns a converted value. What am I not understanding about the interpreter here?

This *test* statement sends a unix datetimestamp to the script. It returns exactly what the script was written to do. The statement is exactly how I coded in the rainmeter script (it's in the meter linked to the webparser measure that returns the value):

Text=[&Lua:ConvertEpochTimestamp(1590518074)]

DynamicVariables=1

After call text value is 02:34 PM

Now that I know this function call works I want to use some kind of variable in place of the text string in the parameter. I've tried using variables of different types (none work) but it seems that the *proper* thing to do is use the section variable. So that's what I do here. The problem is that instead of TEXT getting the function return it gets the full function call statement rather than actually executing it:

Text=[&Lua:ConvertEpochTimestamp([measureSiteDT])]

DynamicVariables=1

After call text value is [&Lua:ConvertEpochTimestamp(1590518074)]

How do I execute this function with a dynamically valued parameter and return the converted value to "Text" (or the direct placement on the screen)?

Thanks much,
David
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Funky lua call behavior

Post by jsmorley »

dglexie wrote: May 27th, 2020, 1:34 pm Hi all,

I have some interpreter behavior I'm having a hard time understanding and looking to find some help. If I get this I think I'll have the key to understanding this language. First -- just so you know -- I'm a long retired developer who did the assembler/COBOL/Pascal/C route along with all the usual Unix scripting languages. No object-oriented stuff. So I prefer using old-school techniques -- old dog vs tricks thing. But if I have to go new "new school" I'll bite the bullet.

I want to convert a unix datestamp in the simplest way possible. Since we know there is no format call for doing this I decided to use lua to write a very simple function. Just pass it a dt string and it returns the hour and minutes. The lua script works fine -- I get consistent results from it.

So I'm not passing on any full scripts here as this is really a "philosophical" and/or technique question. I want to avoid working with variables that are outside of the lua script -- just pass it a value and it returns a converted value. What am I not understanding about the interpreter here?

This *test* statement sends a unix datetimestamp to the script. It returns exactly what the script was written to do. The statement is exactly how I coded in the rainmeter script (it's in the meter linked to the webparser measure that returns the value):

Text=[&Lua:ConvertEpochTimestamp(1590518074)]

DynamicVariables=1

After call text value is 02:34 PM

Now that I know this function call works I want to use some kind of variable in place of the text string in the parameter. I've tried using variables of different types (none work) but it seems that the *proper* thing to do is use the section variable. So that's what I do here. The problem is that instead of TEXT getting the function return it gets the full function call statement rather than actually executing it:

Text=[&Lua:ConvertEpochTimestamp(1590518074)]

DynamicVariables=1

After call text value is [&Lua:ConvertEpochTimestamp([measureSiteDT])]

How do I execute this function with a dynamically valued parameter and return the converted value to "Text" (or the direct placement on the screen)?

Thanks much,
David

Using a [SectionVariable] in that context will attempt to pass the string value of the measure to the Lua script. However, unless you enclose the argument in 'single quotes' when you pass it, Lua will assume it is either a number (which it isn't) or a Lua variable name (which it isn't).

I would try one of:

[&Lua:ConvertEpochTimestamp('[measureSiteDT]')]
This will pass the value explicitly as a string.

[&Lua:ConvertEpochTimestamp([measureSiteDT:])]
This will pass the value explicitly as a number.

I'd have to see the rest of the Lua code to test, although I'm fairly certain that the first case is the right one...
dglexie
Posts: 3
Joined: May 27th, 2020, 1:02 pm

Re: Funky lua call behavior

Post by dglexie »

Well I tried both options and I get the same result -- just a string that is the statement that *should* be executed with the section variable name replaced by the value of the section variable. I actually based this on something you put in the forum -- essentially the only difference is that your examples used quoted strings to pass to the function and mine is trying to use a variable. That is a very useful bit you put up there (using os.date and time in lua to do timestamp conversions). Thanks for that -- it got me this far.

Code: Select all

[Lua]
Measure=Script
ScriptFile=ConvertEpochTimestamp.lua
UpdateDivider=-1

[MeasureSite]
Measure=WebParser

;URL=http://api.openweathermap.org/data/2.5/weather?zip=19006&units=imperial&APPID=yourkey
;for testing,,,
URL=file:///C:/Users/us/Desktop/test.json

RegExp=(?siU){"temp":(.*),"feels_like":(.*),"temp_min":(.*),"temp_max":(.*),"pressure":(.*),"humidity":(.*)},"visibility":(.*),"dt(.*)":(.*),


[MeasureSiteDT]
Measure=WebParser
URL=[MeasureSite]
StringIndex=9

[meterDT]
Meter=string
MeasureName=measureSiteDT
MeterStyle=styleRightText
;MeterStyle=styleTitle
DynamicVariables=1
X=400
Y=30
;W=190
W=400
H=14

;the following works with dynamicvariables set on
;Text=[&Lua:ConvertEpochTimestamp(1590518074)]

;the following just returns a text string but does include the actual value --  1590518074
;Text=[&Lua:ConvertEpochTimestamp([measureSiteDT])]

;the following just returns a text string but does include the actual value --  1590518074
Text=[&Lua:ConvertEpochTimestamp([measureSiteDT:])]

;the following just returns a text string but does include the actual value --  1590518074
;Text=[&Lua:ConvertEpochTimestamp('[measureSiteDT]')]

DynamicVariables=1

Lua script

Code: Select all

function ConvertEpochTimestamp(timeString)
	
		
	outString=os.date('%I:%M %p',timeString)
		
	return outString

end
Last edited by dglexie on May 27th, 2020, 4:03 pm, edited 1 time in total.
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Funky lua call behavior

Post by jsmorley »

Code: Select all

[Rainmeter]
Update=1000
DynamicWindowSize=1
AccurateText=1

[Lua]
Measure=Script
ScriptFile=ConvertEpochTimestamp.lua
Disabled=1

[MeasureSite]
Measure=WebParser
;for testing,,,
URL=file://#CURRENTPATH#Test.json
RegExp=(?siU)<dt>(.*)</dt>

[MeasureSiteDT]
Measure=WebParser
URL=[MeasureSite]
StringIndex=1

[meterDT]
Meter=string
MeasureName=measureSiteDT
FontSize=11
FontWeight=400
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
DynamicVariables=1
Text=[&Lua:ConvertEpochTimestamp([&measureSiteDT:])]
The issue, which I should have caught before, but couldn't really test, is that when you pass a [SectionVariable] to Lua, you need to use the Nesting Variables form of it. You precede the measure name with the & character.

https://docs.rainmeter.net/manual/lua-scripting/inline-lua/#NestedVariables

This is because in your example, the section variable [&measureSiteDT:] is "nested" inside the section variable [&Lua:...]. This will always be true of Inline Lua.

Then pass it as an 'explicit number', and all is well... The Lua is fine.


1.jpg


Extra credit:

If you want to strip off the leading zero on the time, you can do this:

Code: Select all

function ConvertEpochTimestamp(timeString)
	
	outString=os.date('%I:%M %p',timeString)
	
	outString = string.gsub(outString, '^0', '')
	
	return outString

end


2.jpg



The os.date function in Lua doesn't have the # formatting parameter that the Time measure in Rainmeter has, to automatically strip off leading zeros.
You do not have the required permissions to view the files attached to this post.
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Funky lua call behavior

Post by jsmorley »

Note that for one update, before the WebParser has a chance to get the data from the file / site, the time will always be something weird like 7:00 PM, This is because the number value will be zero for one update and 0 in Epoch time for MY local timezone is:

Wednesday, December 31, 1969 7:00:00 PM

If this bothers you, I would set Hidden=1 on the Meter, then have a FinishAction on the WebParser parent measure that unhides it with a !ShowMeter bang.
dglexie
Posts: 3
Joined: May 27th, 2020, 1:02 pm

Re: Funky lua call behavior

Post by dglexie »

Beautifully perfect! It would've taken me ages to get that one -- I'm only a couple of days into understanding how this all works. Hah, biting off more than I can chew. Gotta go and read the nesting variables section of the manual.

Yes, the output needs some "tuning". :)

Thanks much for your time!
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Funky lua call behavior

Post by jsmorley »

Glad to help.