It is currently April 27th, 2024, 11:46 am

Acting on a value's place within a numerical range

Get help with installing and using Rainmeter.
jn_meter
Posts: 136
Joined: December 27th, 2016, 12:04 pm

Acting on a value's place within a numerical range

Post by jn_meter »

Hi all

Is there really nothing better - more elegant and more performant - than the following type of thing? I suppose I could call Lua, but I imagine that so doing would be expensive (and I am sure how well Lua itself handles numeric ranges).

Code: Select all

IfCondition=(measure_cpu_usage <= #threshold_cpu_lowest#)
IfTrueAction=#action_cpu_usage_lowest#
IfCondition2=(measure_cpu_usage > #threshold_cpu_lowest#) && ( measure_cpu_usage <= #threshold_cpu_low# )
IfTrueAction2=#action_cpu_usage_low#
IfCondition3=(measure_cpu_usage > 34) && ( measure_cpu_usage <= #threshold_cpu_medium# )
IfTrueAction3=#action_cpu_usage_medium#
IfCondition4=(measure_cpu_usage > #threshold_cpu_medium#) && ( measure_cpu_usage <= #threshold_cpu_high# )
IfTrueAction4=#action_cpu_usage_high#
IfCondition5=(measure_cpu_usage > #threshold_cpu_high#)
IfTrueAction5=#action_cpu_usage_highest#
User avatar
Yincognito
Rainmeter Sage
Posts: 7175
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: Acting on a value's place within a numerical range

Post by Yincognito »

jn_meter wrote: August 20th, 2023, 1:47 pm Hi all

Is there really nothing better - more elegant and more performant - than the following type of thing? I suppose I could call Lua, but I imagine that so doing would be expensive (and I am sure how well Lua itself handles numeric ranges).

Code: Select all

IfCondition=(measure_cpu_usage <= #threshold_cpu_lowest#)
IfTrueAction=#action_cpu_usage_lowest#
IfCondition2=(measure_cpu_usage > #threshold_cpu_lowest#) && ( measure_cpu_usage <= #threshold_cpu_low# )
IfTrueAction2=#action_cpu_usage_low#
IfCondition3=(measure_cpu_usage > 34) && ( measure_cpu_usage <= #threshold_cpu_medium# )
IfTrueAction3=#action_cpu_usage_medium#
IfCondition4=(measure_cpu_usage > #threshold_cpu_medium#) && ( measure_cpu_usage <= #threshold_cpu_high# )
IfTrueAction4=#action_cpu_usage_high#
IfCondition5=(measure_cpu_usage > #threshold_cpu_high#)
IfTrueAction5=#action_cpu_usage_highest#
All variants of doing this are equally good and performant, and Lua is very fast, though I wouldn't use it just for such minor considerations. The only difference is elegance, length or convenience for the user. IfConditions, numeric conditionals (also called ternary operators in various languages), Lua, or simply math tricks for the sake of elegance are some of the ways to do this. It can even be done sometimes using strings, string and numerical combinations or nested variable syntax, but it's probably not the case here. It depends on the particularities of the situation.
Profiles: Rainmeter ProfileDeviantArt ProfileSuites: MYiniMeterSkins: Earth
jn_meter
Posts: 136
Joined: December 27th, 2016, 12:04 pm

Re: Acting on a value's place within a numerical range

Post by jn_meter »

Thanks, Yincognito.

Unless someone is generous enough to provide a concrete - i.e. more or less worked out - alternative, then I'll live with my existing code!
User avatar
Yincognito
Rainmeter Sage
Posts: 7175
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: Acting on a value's place within a numerical range

Post by Yincognito »

jn_meter wrote: August 20th, 2023, 9:10 pm Thanks, Yincognito.

Unless someone is generous enough to provide a concrete - i.e. more or less worked out - alternative, then I'll live with my existing code!
It's not really about generosity, cause the code itself is so simple that you can do it as well. For example, in Lua you'd create a, say, function ActionName(value, thresholdlowest, thresholdlow, thresholdmedium, thresholdhigh, thresholdhighest) where you'd use an appropriate if...then...elseif...else...end statement to return the name of the action variable as a string, which can be then used in Rainmeter via the nested syntax, e.g. OnUpdateAction=[#[&Script:ActionName([&measurecpuusage:],[#thresholdlowest],[#thresholdlow],[#thresholdmedium],[#thresholdhigh],[#thresholdhighest])]] or doing it in an OnChangeAction to only run it when changing. If your thresholds follow a consistent pattern, e.g. 20, 40, 60, 80, 100, you can use a variable to get the integer part of the division between the usage value and 20 to help choosing the proper action variable, assuming you number them accordingly, e.g. OnUpdateAction=[!SetVariable ActionIndex (Trunc([measurecpuusage:]/20))][#action_cpu_usage_[#ActionIndex]]. The numeric conditional can be easily found in the manual at the bottom of the Formulas page, though you'd have to use an index approach like above since the conditional only accepts numbers as its terms and not strings like variable names, and so on.

Like I said, it depends on circumstances. The variants are easy to construct assuming some basic Rainmeter knowledge, it's not like some unheard of approach that requires full code generosity...
Profiles: Rainmeter ProfileDeviantArt ProfileSuites: MYiniMeterSkins: Earth