[Solved] How to to use the content of a string as item name

Hi All,

I’m quite new to openhab so please be patient. I’m very impressed by the functionality and ease of getting started. So thumbs up to all the developers and people involved :grinning:

I am monitoring the temperature in all my rooms. Whenever the current temperature (IST) is more then 2 degrees below the wanted temperature (SOLL) I want to receive an email.

So for each room I have a pair of Numbers which are read from KNX:

Number Heating_Temp_WZ “Wohnzimmer [%.1f °C]” (Heating) { knx = “<3/1/1”}
Number Heating_Temp_WZ_Soll “Wohnzimmer Soll [%.1f °C]” (Heating) { knx = “3/1/11+<3/1/21”}

This is my approach:

rule “Room too cold”
when
Item Heating changed
then
var changedTemperatures = Heating.members.filter(x|x.changedSince(now.minusSeconds(30))).map[it]
changedTemperatures.forEach [ room |
logInfo(“Heating”, "room too cold: " + room.name) // --> “Heating_Temp_WZ”

		var sollName = ( room.name + "_Soll") as String  
		logInfo("Heating", "name: " + sollName)  // --> "Heating_Temp_WZ_Soll"
		
		// FIXME: how to get from sollName to item?
		
	   var soll = sollName.state as DecimalType
	   var ist = room.state as DecimalType

		logInfo("Heating", "soll:"  + soll + "  ist:" + ist)
   
   		if ( soll.floatValue - ist.floatValue > 2.0) {
	   		logInfo("Heating", "sending email")
   		}
   	]

end

Of course I could harcode the pairs in my rule file. But there must be a smarter way reusing the already defined items?!

Any hints?

Thanks a lot
Karl

I found the solution. More an xtend “problem” then openhab. I can just filter the member of Heating again and search for the SOLL-Name.

Here ist the working solution:

rule “room too cold”

when

Item Heating_Temp_WZ 		changed or

Item Heating_Temp_Bad 		changed or

...

Item Heating_Temp_Flur 		changed	

then

    logInfo("Heating", "changed")

    var changedTemperatures = Heating.members.filter(x|x.changedSince(now.minusSeconds(30))).map[it]

    changedTemperatures.forEach [ room |

	    logInfo("Heating", "room temp changed: " + room.name)

	    val sollName = ( room.name + "_Soll") as String			
	    var sollItems = Heating.members.filter(x|x.name == sollName.toString).map[it]   // <-- this does the trick
	    var soll = sollItems.head.state as DecimalType
	    var ist = room.state as DecimalType

	    logInfo("Heating", "Soll:"  + soll + "  Ist:" + ist)
   
   	    if ( soll.floatValue - ist.floatValue > 2.0 ) {
	   	logInfo("Heating", "sending email")
	   	...
   	    }
   	]

end

So maybe this will help some other newbie. Thanks to everyone so far.

2 Likes