Check value is between x and z

Is it possible to check if a value ‘between something’? That my first value is compared with another value + or - a certain value. This way, when my value is ‘in between’, it should be triggerd.
Today, the value must be exact, no?
So I’m afraid that fe 17.3 > 17.5 > 17.8 > 17.9 > 18.1 > 18.3… won’t be triggered, since the value isn’t 18.00 == 18.00

My idea is something like:

if ((TEMP1.state as DecimalType)==(TargetTemp.state as DecimalType) +1 or -1)

Or should I use something like

if ((TEMP1.state as DecimalType) > (TargetTemp.state as DecimalType) -1) &&
 ((TEMP1.state as DecimalType) < (TargetTemp.state as DecimalType) +1)

ps my rule today stops the heating (or cooling) when the temperature is reached. But of course, when it goes above without being exact, it won’t stop…

if ((TEMP1.state as DecimalType).floatValue > ((TargetTemp.state as DecimalType).floatValue -1)
 && (TEMP1.state as DecimalType).floatValue < ((TargetTemp.state as DecimalType).floatValue +1))
1 Like

See one of my rooms rule thermostat:
House_HeatingOffset is like your “window” of 2 degrees (-1 to +1)
The rule will open a radiator valve if the windows are closed
Another rule will fire the boiler if one of the valves is open

rule "Thermostat Master Bedroom"
when
    Item MasterBedroom_ThermostatAmbientTemp changed or
    Item MasterBedroom_ThermostatTarget changed
then
    val offset = House_HeatingOffset.state as Number
    val target = MasterBedroom_ThermostatTarget.state as Number
    val ambient = MasterBedroom_ThermostatAmbientTemp.state as Number
    var turnOnTemp = target - (offset / 2)
    var turnOffTemp = target + (offset / 2)
    if (ambient <= turnOnTemp) {
        if (MasterBedroomWindows.state == CLOSED) {
            if (MasterBedroom_RadiatorValve.state == OFF) {
                MasterBedroom_RadiatorValve.sendCommand(ON)
            }
        }
    } else if (ambient >= turnOffTemp) {
        if (MasterBedroom_RadiatorValve.state == ON) {
            MasterBedroom_RadiatorValve.sendCommand(OFF)
        }
    }
end
1 Like