Number item with greatest value

For garden watering purpose, I’m trying to determine for how long time the drip / sprinkler watering system should be turned on every night.

Every hour a rule is evaluating the weather, and assigns it to one of five weather groups (type of weather).

rule "HourlyWeaterGroupDetection"
when
    Time cron "0 0 * * * ?"
then
	logInfo("Weather","Weather rule fired")
    if(Humidity_Average.state <= 30 && Clouds.state == 0 && Netatmo_RainGauge_RainLevel1h.state == 0 && Temperature.state > 20) {
    	WeatherGroup05 = WeatherGroup05 + 1
    	postUpdate(SitemapWeatherGroup05, WeatherGroup05)
		logInfo("Weather","Weather group 5 evaporation 5mm")
 	} 
	else if(Humidity_Average.state <= 70 && Clouds.state <= 20 && Netatmo_RainGauge_RainLevel1h.state == 0 && Temperature.state > 15) {
    	WeatherGroup04 = WeatherGroup04 + 1
    	postUpdate(SitemapWeatherGroup04, WeatherGroup04)
		logInfo("Weather","Weather group 4 evaporation 4mm")
	}
	else if(Humidity_Average.state <= 90 && Clouds.state <= 80 && Netatmo_RainGauge_RainLevel1h.state < 0.5 && Temperature.state > 10) {
    	WeatherGroup03 = WeatherGroup03 + 1
    	postUpdate(SitemapWeatherGroup03, WeatherGroup03)
		logInfo("Weather","Weather group 3 evaporation 3mm")
 	}
	else if(Humidity_Average.state >= 60 && Clouds.state >= 65 && Netatmo_RainGauge_RainLevel1h.state < 0.5 && Temperature.state > 5) {
    	WeatherGroup02 = WeatherGroup02 + 1
    	postUpdate(SitemapWeatherGroup02, WeatherGroup02)
		logInfo("Weather","Weather group 2 evaporation 2mm")
 	}
	else if(Humidity_Average.state >= 60 && Clouds.state >= 65 && Netatmo_RainGauge_RainLevel1h.state >= 0.5 && Temperature.state > 5) {
    	WeatherGroup01 = WeatherGroup01 + 1
    	postUpdate(SitemapWeatherGroup01, WeatherGroup01)
		logInfo("Weather","Weather group 1 evaporation 1mm")
 	}
end

Just before midnight, I want to turn on the garden watering, and the most used weather group (highest decimal value) decides for how long time.

At midnight all five weather group items are set to zerro

My question is, what is the best way to determine which of the five number items have the greatest value? I have tried with “>” two at a time, I think I could mange it to work, but it dose not look quite elegant I guess.

Any suggestions?

BR O

if      (WeatherGroup01>=WeatherGroup02 && WeatherGroup01>=WeatherGroup03 && WeatherGroup01>=WeatherGroup04 && WeatherGroup01>=WeatherGroup05) {}
else if (WeatherGroup02>=WeatherGroup01 && WeatherGroup02>=WeatherGroup03 && WeatherGroup02>=WeatherGroup04 && WeatherGroup02>=WeatherGroup05) {}
else if (WeatherGroup03>=WeatherGroup01 && WeatherGroup03>=WeatherGroup02 && WeatherGroup03>=WeatherGroup04 && WeatherGroup03>=WeatherGroup05) {}
else if (WeatherGroup04>=WeatherGroup01 && WeatherGroup04>=WeatherGroup02 && WeatherGroup04>=WeatherGroup03 && WeatherGroup04>=WeatherGroup05) {}
else {}
1 Like

If all five SitemapWeatherGroup0x items are member of a group (for example myGroup), you could try something like the following loop:

(untested code):

var Number watering_time = 0

myGroup.members.forEach[ item | if (item.state > watering_time) watering_time = item.state ]

Hi,

I have played arround with both @hr3 and @duiffie reply, thanks to both of you.

Actually @hr3 suggestion was the way I imagined handling it, but I thought there would be a more “right” way, maybe like @duiffie suggest. The problem with the “forEach” is, that it identifies the greatest value, but not which item is holding the greatest value, and that’s what I need. With some modification it might be possible to achieve that, but it surely surpasses my abilities to do that.

Therfore Im doing like @hr3 has suggested.

Thanks a lot.

BR O

var NumberItem watering_item
var String     watering_item_name
var Number     watering_item_state = 0

myGroup.members.forEach[ item |
  if (item.state > watering_item_state) {
    watering_item       = item
    watering_item_name  = item.name
    watering_item_state = item.state
  }
]
1 Like

Nice, thanks! :slight_smile:

BR O