OH4 - Using a profile with MAP transform to invert switch linked to channel

Hi All

I am trying to invert the “states” of a switch linked switch.

I have an Electric Fence connected to a Shelly UNI. However the Electric Fence requires the linked switch to be OFF when the electric fence is ON. I cannot get the reverse inputs option on the Shelly to work, so thought I would try us a Profile with a Transformation to swap the ON/OFF so that the logic looks correct on a widget etc.

So I have a Channel with two switch items linked to it (One Native and one via Profile):

The swapped switch is linked via a Profile and Transformation as follows:

This works 100% when toggling the switch via the NATIVE Item. The transformed item changes state opposite to the native, fence turns on and off when it should etc.

However when toggling the transformed switch, things just don’t work as expected:

Switch Transforation

The first toggle seems to reset and then change, etc.

Does the profile only work in one direction?

Is there any other way to achieve the same result?

Thanks
Mark

You’ve tagged OH 4 but not indicated which version.

In later OH 4 versions the SCRIPT transform profile supports transforming both ways. But this is only supported on the SCRIPT transform profile (i.e. the ones that let you write a transform in a rules language like JS or Rules DSL). You can’t do two way with the other transforms as far as I know. It’s just from the Thing to the Item and from the Item to the Thing.

I have been trying this on Build 3485.

Am I correct that this should read “not NOT from item to thing”?

Will have to try and find how to achieve the MAPping in a script them.

Correct.

So I have tried creating a SCRIPT transformation via thh GUI.

Created the following script via Blockly (to try ensure I have syntax etc OK):

(function(data) {
  var returnValue;
  if (data == 'ON') {
    returnValue = 'OFF';
  }
  if (data == 'OFF') {
    returnValue = 'ON';
  }
  return returnValue
})(input)

Linked the Profile via GUI:

Nothing seems to happen to the linked item when state changes. Logs only report changes to the direct item.

I am guessing my script is wrong?

EDIT: Tried to add some logging and get nothing. Also tried to use the syntax from documentation:

(function(data) {
  var returnValue = "String has " + data.length + " characters"
  return returnValue
})(input)

linking to a STRING Item, but also get nothing.

Notice the descriptions under the profile configuration. Updates from the Thing to Item get translated and commands from the Item to the Thing get translated. Is it possible that the Thing is commanding the Item instead of updating it? That would be unusual but I suppose it’s possible and that would bypass the transform.

Updating the Item any other way except from the Thing (e.g. from a rule) and commanding the Item from the Thing will skip the transform.

Another thing is the transform only handles ON and OFF as Item states. An Item can also become “UNDEF” or “NULL” and bindings are not always consistent in when they update and Item to either of these. But, if either of those occur, your transform will return undefined I think, perhaps null which I think will cause the transform to suppress the event entirely.

I don’t think it’s likely to happen here but it’s always good to be aware of and handle that case. But some logging will prove it.

What sort of logging did you add? I’d make sure to add a line at the top of the function just to show it’s invoked that logs out data and then one before the return to log out returnValue.

I see nothing wrong with the code. If there were a syntax error you’d get errors in the logs.

Thank You. I tried like this:

(function(data) {
  var returnValue;
  if (data == 'ON') {
    returnValue = 'OFF';
  }
  if (data == 'OFF') {
    returnValue = 'ON';
  }
  
  logger.info(typeof data);
  logger.info(data); 
  logger.info(returnValue);   
  
  return returnValue
})(input)

EDIT:

Deleted the THING and now seeing some logging - so looks like something went wrong with the ITEM links.

Will try all again and report back.

EDIT: Working now, but I must be doing something wrong with the logging.

Sorry fo rthe time wasting with faulty THING links.

Yes, see JavaScript Scripting - Automation | openHAB for details.

I prefer to use JavaScript Scripting - Automation | openHAB because it doesn’t require defining logger as a variable first.

If you ever have a question about syntax or whether you are doing something in the correct way, be sure to refer to the reference docs for the add-on. It’s very well written and everything is documented.

In addition, when you are struggling with something, be it a rule or a transform, and it’s not clear whether or not it’s even being invoked, make the very first line of the function/rule is a simple log statement. If, for example, the transform was crashing on your if statements, your log statements will never run and you won’t know if there is something wrong with the code or the code isn’t even being invoked.

(function(data) {
    console.info('Entered map transformation with "' + data + '"!');
    ...

Dear all,

I’m also struggeling with the JS profile transformation “item to thing” - but only for the case when the item is of type “datetime”.

The transformation seems simply not to be executed.
For item types switch and string it is working.

Here is the simple JS Transformation I use:

(function(data) {
console.info( ((time.toZDT(data.toString())).format(time.DateTimeFormatter.ofPattern(‘yyyy-MM-dd’))) );
return (“OFF”);
})(input)

The return value “OFF” is consumed properly by the thing (it is expecting a string).
In case that input data type is a string or switch everything is working as expected.

Any hint?

O.

Given this is a completely different issue from this thread, it’s more appropriate to open a new thread than reply here.

When you open a new thread, make sure to mention what version of OH you are running, show the configuration that calls this transform (i.e. the profile config), and use code fences.

```
code goes here
```

I suspect this is an XY problem also (why use profile for this in the first place, shouldn’t it be in the State Description pattern?) which deserves it’s own discussion.

But after all that, there is no such thing as time.DateTimeFormatter in JavaScript, JS-Joda, nor openhab-js.