Number to Switch conversion

I need to turn a Number Item (0/3) into a Switch Item (OFF/ON), is there any way I can accomplish that other than with Rules (I need this Switch conversion to use with Echo devices, etc…)

This is what I’m using right now. It works, but I wonder if there is any more efficient way to accomplish this type of linking/mapping within the Items itself.

rule "ThermostatSwitch"
when
	Item FF_Master_Thermostat received command
then
	if(receivedCommand == OFF) {
		FF_Master_Thermostat_Mode.sendCommand(0)
	} else {
		FF_Master_Thermostat_Mode.sendCommand(3)
	}
end

rule "ThermostatOmni"
when
    Item FF_Master_Thermostat_Mode changed
then
	if (FF_Master_Thermostat_Mode.state == 0) {
		FF_Master_Thermostat.postUpdate(OFF)
	} else {
		FF_Master_Thermostat.postUpdate(ON)
	}
end

If you get the source of the item change event from a Thing Channel, you can use a 2.4 Profile to apply a transformation before the value is assigned to the item. That would be how it is done with the newest concepts.

Thanks! Looking forward to 2.4!

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…