How to set a variable to the lowest value of a set of device values

First, I’ve tried to search for something similar, but not finding anything so apologies if this is covered somehow already.

My application is that I want to have a variable ($LowBattLevel) that contains the lowest value of all my device battery levels. I have a rule with a set of triggers that fire when any battery level changes, then I call another rule that finds the lowest battery level and sets the $LowBattLevel to that value. That variable is then used in a super tile on my dashboard to alert me when any battery goes below a threshold and I can immediately see the value by looking at the $LowBattLevel value on the super tile.

I’ve tried to build a simple rule that uses a cascading set of If Then Else statements comparing the values of each battery level to $LowBattLevel and changing $LowBattLevel to that devices level if it’s lower. However, you quickly run out of the ability to nest IF THEN ELSE because the screen formatting gets broken after a couple nesting levels. See Screen Pic below. You’ll notice you can no longer see (or click on) the Set Variable option.

If there another way to do this, or is there a way to use more nested IF THEN ELSE levels?

I believe you should be able to accomplish the concept with sequential IF / THEN statements rather than nested.

IF Sensor1 battery < $LowBatteryLevel
THEN Set $LowBatteryLevel = Sensor1 battery
ELSE //do nothing

IF Sensor2 battery < $LowBatteryLevel
THEN Set $LowBatteryLevel = Sensor2 battery
ELSE //do nothing

IF Sensor3 battery < $LowBatteryLevel
THEN Set $LowBatteryLevel = Sensor3 battery
ELSE //do nothing

Since each comparison ultimately sets the variable, then the subsequent comparisons will be against the lowest value so far in the series.

PS. There’s a Math with Variables feature request that you may be interested in casting a vote on.

Josh,

Thanks for the reply and the idea! Yes, that worked fine.

My dashboard is coming together well. Im going to share on the share your dashboards thread.

I’ll look at the math with variable feature request.

1 Like