Using a Measure as a Variable
In Rainmeter, dynamic variables (DVs) let you use measure values almost anywhere you want. When you use a measure value in a Calc formula, there are special rules to consider. A Calc formula is the only place where you do not need DVs to use another measure's value. For example:
Sometimes, however, you may still want to use DVs to get a measure value into a Calc formula. Why? Because in fact, every measure has two values: a number value and a string value. Normally, Calc formulas use the number value. But using DVs (with brackets around the measure name) forces Rainmeter to use the string value. Which means that this...
...does not always mean the same thing as this:
When should I use brackets?
In general, you should use measures in Calc formulas as numbers (without brackets). This is the "safe" approach, because:
If a measure's string and number values are the same, then dynamic variables are unnecessary. Calc measures without DVs are a tiny bit faster, and your formula will behave consistently whether or not DVs are enabled.
If the string and number values are different, then the string value may be inappropriate for mathematical calculations.
The common exception to this rule is when a substitute has been applied. (See below.)
When are the string and number values different?
No math. If the original measure value contains any non-numeric characters, aside from valid operators, the number value is zero. This includes letters, punctuation and special characters.
You'll probably never use a measure like this in a Calc formula to begin with.
Rounding. Even when a measure gives you an actual number, Rainmeter may round the string value to a certain decimal place. The number value preserves the "true" value, which is always better for calculation.
Even if you want a rounded number, the ROUND(x[,precision]) calculator function will give you much finer control over the result.
Formatting. Some measures return a formatted string value that is meant for display, but not for calculations. For example, the Time measure gives a string value with hours, minutes and seconds ("HH:MM:SS"), which is how a real person would want to see the time, but which makes no sense in a math equation. The number value gives the raw Windows timestamp, which can be used by a Calc measure, e.g. to calculate the difference between two dates.
There may be cases in which you would want to use a formatted string value in a Calc formula, but these would likely be rare and unorthodox.
Substitution. Measure substitution is applied only to the string value, not the number value. For example:
MeasureB uses the number value of MeasureA. It ignores the Substitute string on Measure A. Therefore, MeasureB returns a value of 4. In MeasureC, we use DVs to get the string value of MeasureA. This value has substituted the original value, "1", with "2". So MeasureC returns a value of 5.
This means that if you need the substituted value of a measure, you will need to use DVs and brackets. This can be useful when the original value of a measure is not a number at all (as in case #1), but a Substitute is used to change it into one:
IfEqualAction=[!SetOption Light MeterStyle "Green"]
IfBelowAction=[!SetOption Light MeterStyle "Red"]
In this case, we have a web feed that returns either "Running" or "Offline". But we can't do math on a word—since neither of those values is a number or mathematical formula, the number value for both would be zero. Instead, we use a Substitute to transform the string value into "1" or "0", respectively. That way, our Calc measure can compare it to our IfEqualValue and IfBelowValue, and take the right action. If 1 ("Running"), then the light turns green; if 0 ("Offline"), the light turns red.
Substitution is virtually the only case in which you would want to use a string value in a Calc formula.
You can use measures as number values if you are using DVs elsewhere in the Calc measure. You can even mix types of measure values within the same formula:
A quick reference to using measure values
- All measures that return numbers create them as both a "number" and a "string".
- Most measures return a number and a string that are the same. Some measures (Win7Audio, Time, NowPlaying for instance) can return a "number" and a "string" that are different.
- Substitute acts only on the string value of a measure.
- The About window in the Rainmeter context menu displays the string value of measures.
- When you use the name of a measure in MeasureName= on a meter, the type of meter determines which is used. For instance, Bar meters use the number value, and String meters use the string value. Again, in most cases this is not important, as they are for practical purposes the same thing.
- When you use the name of a measure in a Calc measure in a formula (without square brackets), such as Formula=(200 * MeasureName), the number value is used.
- When you use the name of a measure in a Calc measure or other formula (with square brackets and DynamicVariables=1), then the string value is used.
- The Section Variables
[MeasureName:]functionality offers some additional control. Used with DynamicVariables=1, it uses the number value of the measure. This is particularly useful where you want to use the value of a measure dynamically in a formula on a meter or measure, while not getting the string or Substituted value.