Nice tutorial, thanks for posting!
Just a couple of minor code ideas which might make the code even easier to read.
- In “Fan Control - Power On” using a
switchinstead of the if/else the code will look a little cleaner. I also like to centralize the lines of code that do things to one place.
var level = 0
switch(Rowenta_Fan_Speed.state) {
case "Silent": level = 10
case "Low": level = 30
case "Medium": level = 50
case "High": level = 70
case "Turbo": level = 90
}
if(level > 0) Rowenta_Fan.postUpdate(level)
You could even get clever her and use a map lookup or the like but I think this hits the spot for readability. If you are certain Rowenta_Fan_Speed will always have one of those states, you can eliminate the if(level > 0).
-
In all the rules, when the fan isn’t on you toggle it on and then immediate adjust it. Be wary of the timing here, particularly since this is driven by IR. It might still be processing the “PowerToggle” when the subsequent commands come in which not all devices/technologies handle well. Some sleeps might be needed if commands get lost. The Gatekeeper DP could be employed in that case which will queue up the commands and make sure enough time passes between them to no cause trouble.
-
The “Fan Control - Low” could probably be adjusted to handle all the levels using a for loop. However, I don’t understand how three “+/-” commands gets you from “Silent” or “Turbo” to “Low”. But you should be able to get the delta between the the current level and the new level to determine how many “+/-” you need to send and use a for loop to send that many.
-
The “Fan Control - Silent” rule could probably be merged with the “Turbo” and avoid the duplicated code to toggle the fan on.
-
It might be worth while centralizing that code that toggles the fan on to a global lambda and have all your rules call that to avoid the duplicated code.
-
Ultimately this is a state machine so there might be some additional ideas in A State Machine Primer with HABlladin, the openHAB Genie