Hi,
TL;DR: Will not work with profiles at the moment
I just played around and it seems there are still some rough edges with that api.
My scenario is quite similar, in my case, I have a valve (Number:Dimensionless) which I want to convert into Switch -> if > 0 ON, else OFF (or contact)
So I used a very basic JS transformation to check out the mechanism.
First problem here: although there is the source format parameter, it is never used correctly:
TransformHelper:
public static @Nullable String transform(TransformationService service, String function, String format,
String state) throws TransformationException {
try {
String value = String.format(format, state);
return service.transform(function, value);
} catch (IllegalFormatException e) {
throw new TransformationException("Cannot format state '" + state + "' to format '" + format + "'", e);
}
}
It should actually be (with the state passed to the transform method, not a string representation):
String value = state.format(format);
transform
accepts only a String a transform, which will never work with non String States.
So basically, my Javascript function always gets the unformatted String, which I can work around:
valves.js:
(function(i) {
if (parseInt(i) > 0)
return "ON"
else
return "OFF";
})(input)
Next Problem:
The result of the function is always converted back into a StringType instead of the accepted one, which fails in my (and your) use case:
JavaScriptTransformationProfile
private Type transformState(Type state) {
String result = state.toFullString();
try {
result = TransformationHelper.transform(service, function, sourceFormat, state.toFullString());
} catch (TransformationException e) {
logger.warn("Could not transform state '{}' with function '{}' and format '{}'", state, function,
sourceFormat);
}
StringType resultType = new StringType(result);
logger.debug("Transformed '{}' into '{}'", state, resultType);
return resultType;
}
StringType has valid conversion to an OnOffType. In order to make that usefull, either the ItemStateConverter must cover a lot more cases, or the TransformationProfiles should be allowed to return other types (by including or deriving a targetFormat parameter).
I was really excited by the promises of this new mechanism, but now I am back to reality…
Perhaps I am missing something obvious, but I was never abled to make this work - and the code seems to match my results…