UpdateDivider in Lua Measure causes delay before variables are returned

Discuss the use of Lua in Script measures.

UpdateDivider in Lua Measure causes delay before variables are returned

January 12th, 2018, 5:08 pm
DigitalEssence
   [27 posts]

Hi,

I've seen this answered in the forums before when I was searching for another answer but typically, now I need to know the answer I can't find it despite searching for ages.

I am trying to write a skin with a lua script that takes the current air pressure, writes it to a file x times and then reads back x number of values to create a histogram showing the changes in the pressure over time.

The issue I have now is that when I place an UpdateDivider in the lua measure to make it run at the same frequency as the parser, I have to wait for the first update before any values are returned.

My skin has the following related to the timing and passing the value to the lua script

Code: Select all

[Rainmeter]
Update=1000

;======================================================================================
; Pass pressure reading to Lua and return it
;======================================================================================
[MeasurePasspressureToLua]
Measure=Script
ScriptFile=#CURRENTPATH#writepressure.lua
CurrentPressure=[MeasureCurrentPressure]
UpdateDivider=540

[MeterGetPressureFromLua]
Meter=String
MeasureName=MeasurePasspressureToLua
MeterStyle=TextStyle
X=10
Y=R
Text=Pressure: %1


The lua script contains the following

Code: Select all

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


I now don't get a value for %1 until 540 x 1000 milliseconds.

The answer I saw before but can't find showed how to get round this issue. Apologies for my poor search skills and not being able to find it again.

Thanks.
User avatar

Re: UpdateDivider in Lua Measure causes delay before variables are returned

January 12th, 2018, 5:28 pm
jsmorley
Developer   [16571 posts]

I assume you are getting the value for the air pressure from WebParser. What you want to do is have a FinishAction on the WebParser parent measure that uses !UpdateMeasure on the Lua measure, so it is updated as soon as the WebParser measure is done getting the value, rather than waiting for the next normal update of the Lua measure, which will be up to 539 seconds later. You will want to follow that with an !UpdateMeter bang, probably with "*" as the parameter, and a !Redraw bang. Those will update the meters and redraw the skin as well, so you are not waiting for the next normal skin update up to a second later.

https://docs.rainmeter.net/manual/plugins/webparser/#FinishAction

https://docs.rainmeter.net/manual/bangs/#UpdateMeasure
https://docs.rainmeter.net/manual/bangs/#UpdateMeter
https://docs.rainmeter.net/manual/bangs/#Redraw

The reason for this is that unlike most measures in Rainmeter, which in effect "ask a question and wait for the answer", WebParser is different. Since it can take some time to connect to a remote resource, download the HTML, and parse it, WebParser is "threaded", and is allowed to go off on its own, while the skin carries on. This means that a WebParser measure will never have a value during the first update of the skin. It will set any child measures as soon as it is done, but since your Lua measure is updating every 540 seconds, it won't see any result until then. Forcing it to update when the WebParser measure is done will make it be as responsive as possible.

I would personally set UpdateDivide=-1 on the Lua measure, and have !UpdateMeasure / !UpdateMeter * / !Redraw on the FinishAction do all the driving... Just set UpdateRate=540 on the parent WebParser measure, set UpdateDivider=-1 on the Lua measure, and let the WebParser measure's FinishAction control things. That is the most efficient way possible.

Re: UpdateDivider in Lua Measure causes delay before variables are returned

January 12th, 2018, 5:54 pm
DigitalEssence
   [27 posts]

jsmorley wrote:I assume you are getting the value for the air pressure from WebParser. What you want to do is have a FinishAction on the WebParser parent measure that uses !UpdateMeasure on the Lua measure, so it is updated as soon as the WebParser measure is done getting the value, rather than waiting for the next normal update of the Lua measure, which will be up to 540 seconds later. You will want to follow that with an !UpdateMeter bang, probably with "*" as the parameter, and a !Redraw bang. Those will update the meters and redraw the skin as well, so you are not waiting for the next normal skin update up to a second later.

https://docs.rainmeter.net/manual/plugins/webparser/#FinishAction

https://docs.rainmeter.net/manual/bangs/#UpdateMeasure
https://docs.rainmeter.net/manual/bangs/#UpdateMeter
https://docs.rainmeter.net/manual/bangs/#Redraw

The reason for this is that unlike most measures in Rainmeter, which in effect "ask a question and wait for the answer", WebParser is different. Since it can take some time to connect to a remote resource, download the HTML, and parse it, WebParser is "threaded", and is allowed to go off on its own, while the skin carries on. This means that a WebParser measure will never have a value during the first update of the skin. It will set any child measures as soon as it is done, but since your Lua measure is updating every 540 seconds, it won't see any result until then. Forcing it to update when the WebParser measure is done will make it be as responsive as possible.

I would personally set UpdateDivide=1 on the Lua measure, and have !UpdateMeasure / !UpdateMeter * / !Redraw on the FinishAction do all the driving...



Oh that's sweet.

I added

Code: Select all

FinishAction= [!UpdateMeasure "MeasurePasspressureToLua"] [!UpdateMeter "MeterGetPressureFromLua"] [!Redraw]


and like you say, the redraw stops that 1 sec delay and makes it instantaneous.

And yes, I am using the webparser. Should have mentioned that in the OP.

Thank you so much.
User avatar

Re: UpdateDivider in Lua Measure causes delay before variables are returned

January 12th, 2018, 5:56 pm
jsmorley
Developer   [16571 posts]

Glad to help. I hope you caught that I had said UpdateDivider=1 before, when I meant UpdateDivider=-1. The point is, there is no reason for the Lua measure to ever update on its own... It is entirely dependent on the WebParesr measure.

Re: UpdateDivider in Lua Measure causes delay before variables are returned

January 12th, 2018, 5:59 pm
DigitalEssence
   [27 posts]

jsmorley wrote:Glad to help. I hope you caught that I had said UpdateDivider=1 before, when I meant UpdateDivider=-1. The point is, there is no reason for the Lua measure to ever update on its own... It is entirely dependent on the WebParesr measure.


I have now.

Re: UpdateDivider in Lua Measure causes delay before variables are returned

January 12th, 2018, 6:29 pm
DigitalEssence
   [27 posts]

I've got another slight issue which I think I know what is causing it but I can't fix it.

When I load the skin, the first line in the text file written by the lua script is blank. I assume this because it doesn't have a value and my lua script is writing:

Code: Select all

file:write(CurrentPressure, "\n")


where "CurrentPressure" is null so it just writes a new line.

The value for the pressure is taken from the ParseWunderground and the MeasureCurrentPressure measure which the MeasurePasspressureToLua uses to write to the text file.

I added an UpdateDivider=-1 to MeasureCurrentPressure and then added it to the Webparser FinishAction but I still get a blank line on load.

I even changed the order so that the MeasureCurrentPressure was updated before the MeasurePasspressureToLua but that didn't help.

Here are the sections of code involved.

Code: Select all

RegExp=(?siU)<observation_time_rfc822>.*?(\d{2,2}:\d{2}:\d{2}).*?</observation_time_rfc822>.*<weather>(.*)</weather>.*<temp_#Degree#>(.*)</temp_#Degree#>.*<relative_humidity>(.*)</relative_humidity>.*<wind_dir>(.*)</wind_dir>.*<wind_degrees>(.*)</wind_degrees>.*<wind_#WindSpeed#>(.*)</wind_#WindSpeed#>.*<wind_gust_#WindSpeed#>(.*)</wind_gust_#WindSpeed#>.*<pressure_#Pressure#>(.*)</pressure_#Pressure#>.*<pressure_trend>(.*)</pressure_trend>.*<dewpoint_#Degree#>(.*)</dewpoint_#Degree#>.*<windchill_#Degree#>(.*)</windchill_#Degree#>.*<feelslike_#Degree#>(.*)</feelslike_#Degree#>


[ParseWunderground]
Measure=Plugin
Plugin=Plugins\WebParser.dll
Url=file://C:\Users\Hedley\Documents\Rainmeter\Skins\SimplyNova\Wunderground\wunderground.xml
UpdateRate=10
FinishAction= [!UpdateMeasure "MeasureCurrentPressure"] [!UpdateMeasure "MeasurePasspressureToLua"] [!UpdateMeter "MeterGetPressureFromLua"] [!Redraw]

[MeasureCurrentPressure]
Measure=Plugin
Plugin=Plugins\WebParser.dll
Url=[ParseWunderground]
StringIndex=9
UpdateDivider=-1

;======================================================================================
; Pass pressure reading to Lua and return it
;======================================================================================
[MeasurePasspressureToLua]
Measure=Script
ScriptFile=#CURRENTPATH#writepressure.lua
CurrentPressure=[MeasureCurrentPressure]
UpdateDivider=-1


The lua script


Code: Select all

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(CurrentPressure, "\n")
    -- file:write(CurrentPressure)
    file:close()
end


My understanding is that the [MeasureCurrentPressure] and [MeasurePasspressureToLua] won't update until [ParseWunderground] has run the FinishAction so they should have a value by the time the lua script writes it to the text file.

Thanks
User avatar

Re: UpdateDivider in Lua Measure causes delay before variables are returned

January 12th, 2018, 6:33 pm
jsmorley
Developer   [16571 posts]

What you can do it to set both UpdateDivider=-1 and Disabled=1 on the Lua measure.

Then have the FinishAction on the WebParser parent be:

FinishAction=[!EnableMeasure LuaMeasure][!UpdateMeasure LuaMeasure][!UpdateMeter LuaMeter][!Redraw]

The the Lua measure won't be fired at all, not even once, until the WebParser measure pokes it with an actual value.

Re: UpdateDivider in Lua Measure causes delay before variables are returned

January 12th, 2018, 6:47 pm
DigitalEssence
   [27 posts]

jsmorley wrote:What you can do it to set both UpdateDivider=-1 and Disabled=1 on the Lua measure.

Then have the FinishAction on the WebParser parent be:

FinishAction=[!EnableMeasure LuaMeasure][!UpdateMeasure LuaMeasure][!UpdateMeter LuaMeter][!Redraw]

The the Lua measure won't be fired at all, not even once, until the WebParser measure pokes it with an actual value.


Fantastic. That's working perfectly now. Thank you.

I would normally go and grab a cold beer to celebrate but the wife says I drink too much so it's a hot tea instead. ho hum.

Thank you for your help.
User avatar

Re: UpdateDivider in Lua Measure causes delay before variables are returned

January 12th, 2018, 6:51 pm
jsmorley
Developer   [16571 posts]

Sure thing. I can't help much with your wife, that's on you... ;-)

Return to “Lua Scripting”



Who is online

Users browsing this forum: No registered users and 1 guest