Energy management and scheduling

I have flag items that are ON during hours they apply, e.g. bestMaximal is ON when we’re within the n cheapmost hours with n defined by another (user-configurable) number item called besteStundenMaximal, essentially like this

    j = (besteStundenMaximal.state as Number).intValue
    f = Float::parseFloat(StromPreisSortiert.get(j - 1))
    if ((dynTarifStrompreis.state as Number).floatValue <= f) {
        bestMaximal.sendCommand(ON)
        Tarifniveau.postUpdate(TarifOptimal)
        logInfo("Energiemanagement", "aktueller Preis = " + dynTarifStrompreis.state + " ist günstiger oder gleich der " + j + ".-günstigsten Stunde = " + f)
    } else
        bestMaximal.sendCommand(OFF)

Then in my main routine, I proceed depending on this doing stuff like

if (ModusWaermepumpe.state == WPWinterbetrieb && bestMaximal.state == ON)
    Waermepumpe.sendCommand(SGrMode4)