Both IF and ELSE running in embedded IF rule

Hi All,

Just wondering if anyone could help me out with how to ‘stack’ embedded IF and ELSE statements in a rule correctly.

Basically, i want the main part of the rule to run when ‘FilterSetting’ = 2. If that is true, i need the top half to run if UV_Filter == OFF and the second half to run if UV_Filter is ON.

Rule runs great, but it seems that the second half (else { if (hr1gain > 0) { runs even when UV_Filter == OFF, even though the IF statement above is True.

Here is my rule:

rule "Solar Adaptive"
when Time cron "0 0/15 * ? * * *"
then
if (FilterSetting.state == 2) {
  var hr1gain = (SolarGain.sumSince(now.minusHours(1), "rrd4j"))
	if (UV_Filter == OFF) {
  if (hr1gain > 600) {
        sendHttpGetRequest("http://192.168.86.24/white/2?turn=on&timer=3660")
        logInfo("Pond Rule", "Solar Gain more than 10W, filter on for 1 hour (" + hr1gain + "value)") }
  if (hr1gain > 1200) {
        sendHttpGetRequest("http://192.168.86.20/white/3?turn=on&timer=3660")
        logInfo("Pond Rule", "Solar Gain more than 20W, UV filter on for 1 hour" ) }
		}
	else {
  if (hr1gain > 0) {
        sendHttpGetRequest("http://192.168.86.24/white/2?turn=on&timer=3660")
        sendHttpGetRequest("http://192.168.86.20/white/3?turn=on&timer=3660")
	logInfo("Pond Rule", "Solar Gain >0 Filters to remain on (" + hr1gain + " value)") }


}
  }
else
  logInfo("Pond Rule", "Solar not set to Adaptive")
end

Any help greatly appreciated!

Since UV_Filter is not assigned as a variable, it must be an Item? If so, you should be comparing its state to OFF…

if (UV_Filter.state == OFF) {

Try indenting your if statements and putting your brackets neatly on a new line and you’ll see what’s wrong :wink:

Sorry, pretty new to this! Any examples of how to format the code well?

Yes, it is an item, OK, will try .state! Thanks!

rule "Solar Adaptive"
when 
    Time cron "0 0/15 * ? * * *"
then
    if (FilterSetting.state == 2) {
        var hr1gain = SolarGain.sumSince(now.minusHours(1), "rrd4j")
        if (UV_Filter.state == OFF) {
            if (hr1gain > 600) {
                sendHttpGetRequest("http://192.168.86.24/white/2?turn=on&timer=3660")
                logInfo("Pond Rule", "Solar Gain more than 10W, filter on for 1 hour ({} value)", hr1gain)
            }
            if (hr1gain > 1200) {
                sendHttpGetRequest("http://192.168.86.20/white/3?turn=on&timer=3660")
                logInfo("Pond Rule", "Solar Gain more than 20W, UV filter on for 1 hour")
            }
        } else {
            if (hr1gain > 0) {
                sendHttpGetRequest("http://192.168.86.24/white/2?turn=on&timer=3660")
                sendHttpGetRequest("http://192.168.86.20/white/3?turn=on&timer=3660")
                logInfo("Pond Rule", "Solar Gain >0 Filters to remain on ({} value)", hr1gain)
            }
        }
    } else {
        logInfo("Pond Rule", "Solar not set to Adaptive")
    }
end

Amazing! Thank you! Did you use a specific program to do that?

VS Code helps :slightly_smiling_face:

I am actually using that already, but my alignment still doesn’t look as neat as yours! :frowning: I will have to practice!

You have to care about indentation yourself :slight_smile: simply use <tab> for indentation, VSCode will keep the indentation. It’s also possible to change indentation for more than one line at once (both directions).