Regex string to switch

Hello everyone,
I am trying to check if a String I get from my local weather provider contains a certain word and turn a switch on if this is true and off otherwise.
I initially thought this would be an ideal case for a regEx and I also kind of remembered how to mage a regex that matches the string, but how do I get it to return ON if it matches and OFF if not?
thanks for any advice!

What binding? If the HTTP binding, which is what I suspect, you can use a REGEX transformation to extract the part of the string that would contain the word and chain that to a MAP transformation to map the string to ON/OFF.

If there is only two possible values, instead of the MAP transform, you could use the on/off value properties to do the mapping.

Obviously, this could be done in a rule too but it’s not strictly necessary now.

If it’s not the HTTP binding and it’s a binding that doesn’t support chaining transformations, you can apply the REGEX to the Channel and use a Profile to apply the MAP.

You could also apply a SCRIPT transformation to do it all in code instead of chaining transformations. This might be easier because you could use something like (JavaScript):

(function(i) {
  return (i.includes('mystring')) ? 'ON' : 'OFF';

But to answer your specific question, you can’t get the REGEX to return ON/OFF. You can only have it return a portion of the string you care about and then you can use that with another transform.

Thanks very much! This sounds like a way better approach. Now I have one problem: the binding I am using (DWD warning) outputs undef if there is no warning present. It seems to me the transformation script isn’t even executed. How do I get the transformation to turn the switch off if the thing state is undefined?

Thanks for your help!

Use transform profile. That’s gets run between the channel and the Item and you can check for UNDEF there.

Thanks a lot!
Got it working by just checking in js for “UNDEF”. The transformation now looks like that:

(function(i) {
    if (i = "UNDEF") return 'OFF';
    return (i.includes('String I want')) ? 'ON' : 'OFF';