I need some hint how to do in OH3 value conversions.
I had a working script in OH3, just triggered on change of the string value.
my code was:
HT_PV_AC_Voltage_num.postUpdate(Float::parseFloat(String::format("%s",HT_PV_AC_Voltage.state)))
if (HT_PV_AC_Power.state == 'NaN') //Wenn PC Wechselrichter aus wird der wert "NaN" Gemeldet, dann AC_Power=0 Watt
{
sendCommand(HT_PV_AC_Power_num, 0)
}
else {
HT_PV_AC_Power_num.postUpdate(Float::parseFloat(String::format("%s",HT_PV_AC_Power.state)))
}
logInfo("AC_Voltage und AC Power wurde in Number umconvertiert", "Gesendet: " + HT_PV_AC_Voltage_num + HT_PV_AC_Power_num)
This code dows not work in OH3;
021-02-09 21:11:52.746 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID '659ae918f9' failed: <eval>:1:37 Expected , but found :
HT_PV_AC_Voltage_num.postUpdate(Float::parseFloat(String::format("%s",HT_PV_AC_Voltage.state)))
^ in <eval> at line number 1 at column number 37
How should I do this now with state of the art rule coding?
However, this seems way overly complex. What type of Item is HT_PV_Voltage_num?
If it’s a String Item, why? Why not a Number or Number:ElectricPotential?
If it’s a Number there is no need to parse it to a float.
The String::format("%s" is basically a noop (does nothing). You’ll get the exact same result using HT_PV_AC_Voltage.state.toString.
You can pass a String to postUpdate. So if the Float::parseFloat is able to parse HT_PV_AC_Power’s state, postUpdate can too so there’s no need to call Float::parseFloat there either.
If it’s always a number after transformation, just assign it to a Number Item. OH will parse the String to a Number for you. Then you can get the state as Number and do any math with it you need to.
If you change to a Number Item, you have to change the Channel to be a Number type Channel too. Once you do that, so long as the XPATH always returns a parsable number all will work. You cannot link a Number Item to a String Channel.
I did try that first, see the printscreen.
But this config always returns an error.
2021-02-09 22:28:25.217 [WARN ] [.profiles.XPathTransformationProfile] - Could not transform state 'UNDEF' with function '(number(//Measurements/Measurement[@Type='AC_Voltage']/@Value))' and format '%s'
If I use a string, I can get the value, but than I have to do the rules, I thought.
That’s using a profile. (That should work,but there is a bug.)
Apply your transformation directly in the HTTP Thing/channel of number type, link to a Number type Item without using a profile.
You could even add a unit of “V” in the number channel, and link to a Number:ElectricPotential type Item.
No. Go one step at a time, sneak upon your problem.
Set up a basic string channel and Item for your http Thing, no transform, see if you get the XML you expect.
Then set up another string channel using your XPATH and see if you get the numeric you expect.
When that’s all good, you can change to number channel and Item.