Conversion from string item to number item

Hi everyone.

I’ve a short question and hope anyone can help me. I’ve searched the community but did not find any thread which fits to me.

I’m using an item of the type string which has a binding to a temperature sensor via SNMP. This sensor provides the data as string and not as integer or float. I can use this item without any problem for visualization but cannot use it in a chart because it’s not a number. What I want to do now is to convert the string to a number item which I can also use for the chart.

Is there a way, I easily can convert the string item to a number item?

Hopefully someone can help me. :wink:

Thanks in advance.

Regards,
Bernd

Assuming that it is a parsable format (i.e. no leading or trailing spaces) just change your Item from a String to a Number and OH should handle the conversion for you. In other words if you have

String myTempItem ...

change it to

Number myTempItem ...

If it does have leading or trailing white spaces you can use a JavaScript transform to remove the whitespace.

Rich is correct, but just adding a note that the SNMP binding knows how to update String, Number and Switch items by parsing the string representation to update Number and Switch items. There are many bindings that don’t allow different item types, but the SNMP binding does. From the SNMP binding source code:

                        if (itemType.isAssignableFrom(StringItem.class)) {
                            state = StringType.valueOf(value);
                        } else if (itemType.isAssignableFrom(NumberItem.class)) {
                            state = DecimalType.valueOf(value);
                        } else if (itemType.isAssignableFrom(SwitchItem.class)) {
                            state = OnOffType.valueOf(value);
                        }

Also be aware that some persistence services don’t take kindly to changing the item type of an item without also changing its name. For example, the MySQL persistence service creates an SQL table per item, on first storage, that is sensitive to the item type. If you later change a String item to a Number item (while leaving the name the same), the MySQL persistence service will not change the table definition to match, leaving you with charting or other problems.

1 Like

To echo @watou, I am not sure the logic can be applied to OH2, as the channel defines the type

The only way I can see is to do conversion with a rule to satisfy the needs of the topic starter

That’s true for ESH/OH2 bindings, which don’t have the ability to dynamically adapt to the types of the items to which their channels are linked. But running an OH1 binding under OH2 should continue to work the same as in OH1, where the binding can determine which type of item it’s updating (or better, inspect what the item’s accepted data and command types are and remain unconcerned with the item type), and generate states that will be accepted by the bound item. I wish ESH/OH2 did not impose this constraint on its native bindings.

@bertl1982 I had the same issue with some SNMP values in string format, were I wanted to track numeric values for graphing. After lot of testing and searching created the following rule for creating numeric values I can use for graphing:
rule "nas update"
when
** Item nas_Cpu received update**
then
** var Number CpuUse**
** var Number Temp**
** CpuUse=Float::parseFloat(String::format("%s",nas_Cpu.state).replace(’%’,’’))**
** nas_CpuLoad.postUpdate(CpuUse)**
** Temp=Integer::parseInt(String::format("%s",nas_Temp.state).substring(0,2))**
** nas_Temprtr.postUpdate(Temp)**
end

1 Like

Thank you so much @pensionado for this solution. Is working like a charm also in my case. :wink:

Well, also a thanks to all the other repliers. :wink:

I had the same problem as @bertl1982 but my String item originated from an exec command reading the temperature from a 1-wire DS18B20 temperature sensor.

The postUpdate to the number item did not work in my case but a sendCommand did. If any of you have the same problem please try this instead.

My code for reference:
Thing:

Thing exec:command:vattenTemp [command="/home/openhabian/onewiretemp.sh", interval=60, timeout=5, transform="REGEX((.*?))"]

Item:

String vattentemperaturString "Vattentemperatur [%.1f °C]" {channel="exec:command:vattenTemp:output"}
Number vattentemperatur "Vattentemperatur [%.1f °C]" <temperature> (temperatureG)
DateTime vattentemperaturDT "Vattentemperatur [%1$ta %1$tR]" <time> {channel="exec:command:vattenTemp:lastexecution"}

Rule:

rule "Convert String temp to Number"
when
    Item vattentemperaturString received update
then
    vattentemperatur.sendCommand(Float::parseFloat(String::format("%s",vattentemperaturString.state).replace(' ','')))
end

Merry Christmas! :slight_smile:

After update OH to 2.4.0 this doesn´t work in my case. Have you done this update?