org.eclipse.smarthome.core.library.types.StringType to java.lang.String conversion

Hello to the forum,

how do I convert an “org.eclipse.smarthome.core.library.types.StringType” to “java.lang.String”?
I need to use the method “charAT()” to get a certain character out of a variable of type “StringType”…

Thanks for help

Ansgar

type.toString()

Hello namraccr,
your solution unfortunately doesn’t work.
I’d like to use the following:
I have a variable “Heizprofil” which is of type “org.eclipse.smarthome.core.library.types.StringType”. In this variable I have to find out the character at a certain position. So I’d like to use the “java.lang.String.charAt()” method:
String Zeichen = Heizprofil.charAt(position)
This ends in the error message
“Error during the execution of rule ‘Thermostatsteuerung’: Could not invoke method: java.lang.String.charAt(int) on instance:…”
Any idea what I can do in this case?

That’s incomplete. Does it say “on instance: null” ?
Then your script has an error.

Share your code.

Hi namraccr,
the instance is not NULL. The complete error message from the openHAB-log is:

“2020-02-22 15:24:30.053 [ERROR] [ntime.internal.engine.ExecuteRuleJob] - Error during the execution of rule ‘HeatingValveControl’: Could not invoke method: java.lang.String.charAt(int) on instance: ____________________________________________________HHHHHHHHHHHH__________HHHHHHHHHHHH______________________________________HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH___________HHHHHHHHHHHHH____________________________________________________________HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH______________________________________HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH____________________________________________________________HHHHHHHHHHHHHHHH____________HHHHHH______________________________________HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH____________HHHHHHHH____________________________________________________________HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH______________________________________HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH______________________________________________________HHHHHHHHHHHHHHHHHH____________HHHHHHHHHH______________________________________HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH____________________________________HHHH____________________HHHHHHHHHHHH____________HHHHHHHHHH______________________________________HHHH____________________HHHHHHHHHHHH____________HHHHHHHHHHHH____________________________________HHHH____________________HHHHHHHHHHHH____________HHHHHHHHHH______________________________________HHHH____________________HHHHHHHHHHHH____________HHHHHHHHHHHH

And the string (instance) is at I expect to be.
And here is the code of the function:

val Functions$Function4<GenericItem, Number, String, String, GenericItem> HeatingValveControl = [
Number VacationMode,
Number TemperatureControl,
String HeatingProfile,
NumberItem HeatingValveOperatingMode |

// Determin current day of the week, hours and minutes of the day

val DayOfWeek = now.getDayOfWeek
val Hours = now.getHourOfDay
val Minutes = now.getMinuteOfHour

// Calculating the character position inside thestring "Heating_Profile" which has
// to be evaluatet for heating valve control
// Therefore at first the offset for the day of the week is calculated:
// day of week * 24 hours * heating intervals per hour * 2

val OffsetDayOfWeek= DayOfWeek * 24 * 4 * 2

// Calculating the offset into the day of the week profile according to
// the vacation mode:
// vacation mode * 24 hours * heating intervals per hour

val OffsetVacationMode = VacationMode * 24 * 4

// Calculation the offset into the selected day's profile:
// hours * heating intervals per hour + integer(miniutes / 15) - 1
// ("-1" takes into account that sting indices starts with offset "0")

val OffsetDay = Hours * 4 + Minutes / 15 - 1

val HeatingProfilePosition = OffsetDayOfWeek + OffsetVacationMode + OffsetDay

// Starting heating valve control
// At first vacation mode is evaluated. If it is set to "vacation with absence" (2)
// then temperature is lowered

switch(VacationMode){

	case 2: {

			sendCommand(HeatingValveOperatingMode, 11)
		
		}

	default:{

		switch(TemperatureControl){
		
			case 1:{

				// If temperature control is set to manual mode (1)
				// the heating valve operation mode is set to 
				// "Heat"

				sendCommand(HeatingValveOperatingMode, 1)
			
				}

			case 0:{

				// If neither vacation mode is set to "vacation
				// with absence" nor temperature control is set to 
				// manual mode the heating profile is evaluated and
				// the heating valve operation mode is set accordingly.

			switch(HeatingProfile.charAt(HeatingProfilePosition)){

				case "B":{

						// "Boost"-mode

						sendCommand(HeatingValveOperatingMode, 15)

						}

				case "H":{

						// "Heat"-mode

						sendCommand(HeatingValveOperatingMode, 1)

						}

				case "_":{

						// lower temperature

						sendCommand(HeatingValveOperatingMode, 11)

						}

				case " ":{

					// Heating valve of - only freezing protection

						sendCommand(HeatingValveOperatingMode, 0)

						}
					}
				}
			}
		}
	}
]

Brief explanation of the heating profile structure:
The heating profile has a resolution of 15 minutes, so one hour is represented by four characters. Each day of the week has ist own profile, consisting of two profiles, one for normal work days and one for vacation mode. Each of this profiles consists of 24 hours, equivalent to 96 characters. Therefore a complete profile for every day of the week consists of 192 characters. And at least a complete heating profile string consists of 1244 characters.

Now the rule which calls the function “HeatingValveControl” every 15/30/45 minutes in every hour:

rule "Heating valve control"

when

    // cron expression generator unter "https://www.freeformatter.com/cron-expression-generator-quartz.html"

    Time cron "0 0,15,30,45 * ? * * *"

then

	HeatingValveControl.apply(VacationMode.state, TemperatureControl_LivingRoom.state, HeatingProfile_LivingRoom.state, HeatingValveOperatingMode_LivingRoom)

end

where

VacationMode is a number item
TemperatureControl_LivingRoom is a number item
HeatingProfile_LivingRoom is a string item
HeatingValveOperatingMode_LivingRoom is a number item

I hope this helps to solve my problem.
Regards
Ansgar

Very strange. As far as I can see that should be working. Plenty of other examples for using charAt, e.g. this one.

I suggest logging the value of the index you’re using HeatingProfilePosition to see what it is…

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.