# Help to Simplify Code

Dear All, would appreciate some hints as I’m rather working with trial and error then really being able to code

I would like to check 24 values (power tariff for each hour a day) and assign a value of 0-5 (low to high tariff) to an respective item. based on these values I would like to optimize usage of pool pump and my heatpump.
Currently I copy each code for each item … (I know not very smart but the best I can come up with)

``````	val Number Tariff_High = (Today_Price_High.state as DecimalType)
val Number Tariff_Low = (Today_Price_Low.state as DecimalType)
val Number Tariff_Diff = (Tariff_High - Tariff_Low)
Tarif_Diff.postUpdate(Tariff_Diff)
val Number B1 = Tariff_Diff * 0.1
val Number B2 = Tariff_Diff * 0.20
val Number B3 = Tariff_Diff * 0.50
val Number B4 = Tariff_Diff * 0.8
val Number B5 = Tariff_Diff * 0.9

val Number P0 = (Price_Now_0.state as DecimalType)
if (P0 <= (Tariff_Low + B1)) 								{Tarif0.postUpdate(0)}
if (P0 > (Tariff_Low + B1) && P0 <= (Tariff_Low + B2)) 		{Tarif0.postUpdate(1)}
if (P0 > (Tariff_Low + B2) && P0 <= (Tariff_Low + B3)) 		{Tarif0.postUpdate(2)}
if (P0 > (Tariff_Low + B3) && P0 <= (Tariff_Low + B4)) 		{Tarif0.postUpdate(3)}
if (P0 > (Tariff_Low + B4) && P0 <= (Tariff_Low + B5)) 		{Tarif0.postUpdate(4)}
if (P0 > (Tariff_Low + B5))						 			{Tarif0.postUpdate(5)}

val Number P1 = (Price_Now_1.state as DecimalType)
if (P1 <= (Tariff_Low + B1)) 								{Tarif1.postUpdate(0)}
if (P1 > (Tariff_Low + B1) && P1 <= (Tariff_Low + B2)) 		{Tarif1.postUpdate(1)}
if (P1 > (Tariff_Low + B2) && P1 <= (Tariff_Low + B3)) 		{Tarif1.postUpdate(2)}
if (P1 > (Tariff_Low + B3) && P1 <= (Tariff_Low + B4)) 		{Tarif1.postUpdate(3)}
if (P1 > (Tariff_Low + B4) && P1 <= (Tariff_Low + B5)) 		{Tarif1.postUpdate(4)}
if (P1 > (Tariff_Low + B5)) 								{Tarif1.postUpdate(5)}
``````

this would be now repeated 24 times

Try that:

``````import org.eclipse.smarthome.model.script.ScriptServiceUtil  // AT THE TOP OF THE RULE FILE!!!

val Number Tariff_High = (Today_Price_High.state as DecimalType)
val Number Tariff_Low = (Today_Price_Low.state as DecimalType)
val Number Tariff_Diff = (Tariff_High - Tariff_Low)
Tarif_Diff.postUpdate(Tariff_Diff)
val Number B1 = Tariff_Diff * 0.1
val Number B2 = Tariff_Diff * 0.20
val Number B3 = Tariff_Diff * 0.50
val Number B4 = Tariff_Diff * 0.8
val Number B5 = Tariff_Diff * 0.9

var GenericItem PriceNowItem = null //NEW
var GenericItem TarifItem = null //NEW
var Number price = 0  // NEW

for (var int i = 0 ; i < 24 ; i++) {
PriceNowItem = ScriptServiceUtil.getItemRegistry.getItem("Price_Now_" + i)
TarifItem = ScriptServiceUtil.getItemRegistry.getItem("Tarif" + i)
price = (PriceNowItem.state as DecimalType)
if (price <= (Tariff_Low + B1))                                      {TarifItem.postUpdate(0)}
if (price > (Tariff_Low + B1) && price <= (Tariff_Low + B2))         {TarifItem.postUpdate(1)}
if (price > (Tariff_Low + B2) && price <= (Tariff_Low + B3))         {TarifItem.postUpdate(2)}
if (price > (Tariff_Low + B3) && price <= (Tariff_Low + B4))         {TarifItem.postUpdate(3)}
if (price > (Tariff_Low + B4) && price <= (Tariff_Low + B5))         {TarifItem.postUpdate(4)}
if (price > (Tariff_Low + B5))                                       {TarifItem.postUpdate(5)}
}

``````

Thanks a lot - will try it hopefully during weekend take care