I have a weather station that seems to send temperature as ºF instead of ºC, so in order to have in OH showing the correct temperature I have to use the Item Type as “Number:Temperature”.
Problem is, when I am creating a DSL rule, I cannot use this:
logInfo("Teste1",Sensor_Temperatura_Rua.historicState(now).state)
2024-06-14 11:03:56.636 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'estores-4' failed: An error occurred during the script execution: Could not invoke method: org.openhab.core.model.script.actions.Log.logInfo(java.lang.String,java.lang.String,java.lang.Object[]) on instance: null in estores
And I also cannot use this:
logInfo("Teste2",Float::parseFloat(Sensor_Temperatura_Rua.historicState(now).state.toString))
2024-06-14 11:05:35.182 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'estores-4' failed: For input string: "20.88888888888889 °C" in estores
So how can I solve this and be able to compare values? Thanks!
Ideally you would add a transformation to the Channel reporting the temperature in the first place and have it transform the unit to °C with a Script transformation (I think JS is your only option in OH 3.3).
If it’s the MQTT or HTTP binding you can specify that it’s publishing °F by setting the unit property on the Channel. Then I think if you set the State Description Pattern to %.0f °C it will transform the state from °F to °C automatically. But that was always a bit flakey which is why all of that was reworked in OH 4.
Why now? That doesn’t really make much sense. If that’s what you want you should use lastUpdate or, if this is the Item that triggered the rule newState.
But the null error like that usually means that Rules DSL wasn’t able to transform the Object into something it knows how to use. Try appending .toString to the state to help Rules DSL figure out you want a String.
In Rules DSL you cannot use parseFloat et al with QuantityTypes. The space and the unit is not part of a standard number so it throws an exception. To convert a QuantityType state to a plain Number you would use (Sensor_Temperatura_Rua.historicState(now).state as QuantityType<?>).toFloat.
Notes about OH 4:
you are not forced to use UoM if you don’t want to. When you link a Channel that publishes a QuantityType you can link it to a plain Number and unlike in OH 3.x, the units will be stripped from the state
to solve this sort of problem in OH 4 you would set the unit at the Channel to °F and set the unit metadata on the Item to °C and OH will convert for you automatically
Blockly has a special set of blocks for working with Quantity states and in JS Scripting you can get either the quantityState or numericState from an Item. When you get the numericState it strips the units off the number for you
in OH 4.1 and before the Object returned from Persistence does not always include the unit (e.g. sumSince, averageSince, etc.) but in OH 4.2 and later the unit is applied.
Thanks for the help, but unfortunately this did not worked. tried using it like so:
logInfo("Temp",(Sensor_Temperatura_Rua.historicState(now).state as QuantityType<?>).toFloat)
Got this as a result:
2024-06-17 00:09:06.012 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'exterior-8' failed: 'toFloat' is not a member of 'org.openhab.core.library.types.QuantityType<? extends java.lang.Object>'; line 166, column 16, length 76 in exterior
I am using a Weather Station installed in my roof and connected to Wunderground.com and then using the Weathercompany binding to have my thing created with the multiple channels.
Unfortunately I do receive the information as ºF instead of ºC and setting the State Description Pattern to %.0f °C did not helped.
Because you are just logging it you don’t really need the float in the first place. Just log the Sensor_Temperatura_Rua.historicState(now).state.toString.
But the problem was I remembered the method call wrong. It’s floatValue, not toFloat. A QuantityType is a Number. Number (Java SE 17 & JDK 17)