# How to assure a range for a specific variable in Pyomo

I would like to specify a certain range for a variable for a certain timeslot in Pyomo. I have a temperature variable $$T(t)$$ that is defined by using the difference equation $$T(t) = T(t-1) + \cdots,\quad\forall t>1.$$ But for the last value of the timeslot t_end the temperature should be between two limits T_init + 0.1 >= T(t_end) >= T_init - 0.1, where T_init is a constant temperature value at the beginning of the optimization. How can I realize that in Pyomo? I have only set one boundary so far (either the greater than or equal or the less than or equal constraint). Here is the code that I have:

def temperatureBufferStorageConstraintRule(model, t):
if t == model.set_timeslots.first():
return model.variable_temperatureBufferStorage[t] == SetUpScenarios.initialBufferStorageTemperature
if t == model.set_timeslots.last():
return  SetUpScenarios.initialBufferStorageTemperature + 0.1 >=  model.variable_temperatureBufferStorage[t] >= SetUpScenarios.initialBufferStorageTemperature - 0.1
return model.variable_temperatureBufferStorage[t] == model.variable_temperatureBufferStorage[t-1] + ((model.variable_heatGenerationCoefficient_SpaceHeating[t] * model.param_COPHeatPump_SpaceHeating[t] *  SetUpScenarios.electricalPower_HP * SetUpScenarios.timeResolution - model.param_heatDemand_In_W [t]  * SetUpScenarios.timeResolution - SetUpScenarios.standingLossesBufferStorage * SetUpScenarios.timeResolution) / (SetUpScenarios.capacityOfBufferStorage * SetUpScenarios.densityOfCement * SetUpScenarios.specificHeatCapacityOfCement))

model.constraint_temperatureBufferStorage = pyo.Constraint (model.set_timeslots, rule=temperatureBufferStorageConstraintRule)

model.constraint_temperatureBufferStorage.pprint()


You do not have to care about the exact definition of model.variable_temperatureBufferStorage[t] which is the $$T(t)$$. Basically only the line with if t == model.set_timeslots.last(): is important. Just for clarification I want to mention that I load all the constant values from a class called SetUpScenarios but this is not so important I guess for my question. When using my code I only realize the less than or equal constraint and get the following output when printing the constraint:

variable_temperatureBufferStorage[288] :  21.1 :   True


Do you know how I can combine those two constraints and also realize the less than or equal constraint? I'd appreciate every comment and would be quite thankful for your help.

if t == model.set_timeslots.last():

model.constraint_temperatureBufferStorage_last = Constraint(expr = model.variable_temperatureBufferStorage[model.set_timeslots.last()*] >= SetUpScenarios.initialBufferStorageTemperature - 0.1