[SOLVED] SCALE transform and UNDEF - Could not transform 'UNDEF'

I’m using the OWM binding, and am using a SCALE transform to transform the wind direction into human-readable wind directions (e.g., NNE, SW…).

This is the OWM item using the SCALE transform:

Number:Dimensionless	Weather_OWM_Current_WindDirection	"Current wind direction [SCALE(wind.scale):%s]" {channel="openweathermap:weather-and-forecast:c06828c6:local:current#wind-direction"}

So far so good, but when the wind direction is set to UNDEF then the SCALE transform bails out with an error.

2019-02-26 12:31:33.513 [WARN ] [rm.AbstractFileTransformationService] - Could not transform 'UNDEF' with the file 'wind.scale' : Scale can only be used with numeric inputs or valid quantity types

I’ve tried adding a catch-all transform rule in the wind.scale file, but so far to no avail:

[0..11.25[ = N
[11.25..33.75[ = NNE
[33.75..56.25[ = NE
[56.25..78.75[ = ENE
[78.75..101.25[ = E
[101.25..123.75[ = ESE
[123.75..146.25[ = SE
[146.25..168.75[ = SSE
[168.75..191.25[ = S
[191.25..213.75[ = SSW
[213.75..236.25[ = SW
[236.25..258.75[ = WSW
[258.75..281.25[ = W
[281.25..303.75[ = WNW
[303.75..326.25[ = NW
[326.25..348.75[ = NNW
[348.75..360] = N
[..] = (undefined)

Any clues? Or should I create a proxy Item and a rule to address this case?

Scale transformation works on numbers only. So you need to tweak it in ordwr to set the UNDEF to something that is a number. Proxy item with rule that converts UNDEF to 0 (or whatever durection you like) and use the scale teansformation in the second item. Or use a javascript that does it all at once.

Strikes me that some kind of default/non-numeric action would be a useful enhancement to SCALE

2 Likes

I found a solution by using a proxy item.

openweathermap.items:

Number:Angle	Wx_OWM_Current_Wind_Direction				"Wind Direction [%d °]"	<wind>	(gWeatherCurrent) {channel="openweathermap:weather-and-forecast:77e8fd82:local:current#wind-direction"}
// The following Item will be set with a rule to address the non-numeric values that cannot be transformed through SCALE:
String			Wx_OWM_Current_Wind_Direction_Simplified	"Wind Direction [%s]"	<wind>	(gWeatherCurrent)

openweathermap.rules:

rule "Update simplified wind direction"
when
	Item Wx_OWM_Current_Wind_Direction changed
then
    val String ruleTitle = "UpdateOWMCurrentWindDirection"
    var String status = ""
    var boolean error = true
    if (Wx_OWM_Current_Wind_Direction.state == NULL) { // Error
        status = "(not set)"
    } else if (Wx_OWM_Current_Wind_Direction.state == UNDEF) { // OWM may not return a forecast value
        status = "(undefined)"
    } else if (Wx_OWM_Current_Wind_Direction.state instanceof Number) {
        status = transform("SCALE", "wind.scale", Wx_OWM_Current_Wind_Direction.state.toString())
        error = false
    } else { // Unexpected state type
        status = "(invalid: " + Wx_OWM_Current_Wind_Direction.state.toString() + ")"
    }
    if (error) {
        logWarn(ruleTitle, "{} has state '{}', expecting Number", Wx_OWM_Current_Wind_Direction.name, Wx_OWM_Current_Wind_Direction.state.toString())
    } else {
        logDebug(ruleTitle, "{} has Number state '{}'", Wx_OWM_Current_Wind_Direction.name, Wx_OWM_Current_Wind_Direction.state.toString())
    }
    postUpdate(Wx_OWM_Current_Wind_Direction_Simplified, status)
end```
home.sitemap:
```csv
	Default item=Wx_OWM_Current_Wind_Direction
	Default item=Wx_OWM_Current_Wind_Direction_Simplified

I could also decide to hide Wx_OWM_Current_Wind_Direction_Simplified if the wind direction is non-numeric by introducing yet another proxy item.

If SCALE would allow non-numeric catch-phrases, then there would be no need for this rather convoluted fix. But now I can also persist the simplified wind directions easily without triggering errors.