Condensing rule syntax to eliminate nested if/else?

I have a situation where I have to write a number of different rules which all executeCommandLine a curl POST value based on the value the item receives from the UI.

What rule method should I take to do this the simplest?

I know I could use nested if/else statements but if the items can receive up to 20 different states (not always numerically sequential) it would be ideal to not have to write all that out if there was a simpler approach.

In addition to this, one of the items is a setpoint temperature value which I don’t want POSTing values every time the temperature changes one point. I’m expecting swmbo to cycle up and down this value quickly and don’t want several post requests all at once. I was thinking a simple timer to delay the postUpdate, but if i’m using nested if/else rule syntax then I think i’d need a timer for each state?

Any help would be appreciated.

Bit short on detail. Maybe a sample of existing rules?

executeCommandLine takes a string. Maybe you can build your POST request bit by bit, assembling a string.

Don’t understand why a timer is needed for each “state”, maybe you could create one timer to do its thing a short time in the future, and reschedule/modify as changes come in. Or, take the first action and use a timer to block further action for a while.

EDIT - Maybe you want the switch-case method

Cool, thanks. Will have to have a look at the case option.

The situation is that I have a heatpump with I can control by posting values to it’s web address. I have no trouble getting the executeCommandLine code to work fine, it’s simply how best to write my rules so I don’t have to make them stupidly long.

eg. The mode of the heatpump can be modified by sending the value 1,2,3,4,8, 9, and 16. Each represents a different mode. The same applies to the fan speed, fan mode, zone, and temperature. The temp values range from 18 to 32.

I’d like a rule for each setting I can change which responds to the value set in the gui by POSTing one value, not a series of them if the user scrolls through the list of them.

I don’t want someone to write the rule for me, just tell me if I have to nest my if/else statements, or if there is a better long term robust rule syntax that I should be using for each situation.

A structure I thought would make sense would be:

rule "heatingUpdate"
when item heatMode changed
    if (item heatMode.state==1 or 2 or 3 or 4 or 8 or 9 or 16) //provide the options available to choose from
    executeCommandLine("curl") //need to set a variable (XX) which is then added to the end of my curl

But I don’t believe this will work.

I’ll have a look at the case option regardless.

psuedo rule

      Item heatMode changed
      val myParam = some code that process heatMode.state into a string  e.g. "01"  
      executeCommandLine("curl" + myParam)
1 Like