Split function in OH3

Hello together

I’ve changed from OH3 to OH4. Following function doesn’t work in OH4. Do anybody know, how to fix that?

var Fenster = triggeringItem.name.split(“_”)[1];

the example-item is: window_office_right. With the funcition I would like to have the second word of the item (“office”)

English please, this is an international community.

of course :slight_smile:

Like so?

var myList;
myList = 'the_office_word'.split('_');
console.info((myList[1]));   // equals 'office'

Hi Stefan
Thank you for your answer. That goes in the right direction. How do I get this with the triggerItem?
Something like that?

var myList;
myList = ((event.itemName).split(‘_’));
console.info((myList[1])); // equals ‘office’

Here you go:

or

that works - thanks a lot.
But i have to use the script directly inside a rule. When I execute the same script (in the folder script) separately then the trigger-item doesn’t come. the result is “undefined”.

I use following script:

console.info('-----------------------------');
var myList;
var room;
myList = String(event.itemName).split('_');
room = myList[1];
console.info('testlog: ' + room);

items.getItem('Jalousien_' + room).sendCommand('100');

console.info('-----------------------------');

what could be the solution? in OH3 it was no difference to use script outside of the rules.
Thanks for your support!

Can you exactly explain what you are trying to do?

I rule is run when a condition is triggering it. Only then you have the context and the trigger-item.

How do you trigger the script with the condition of a rule?

i would like to create a Rule which get trigger, when a member of the groupitem “gFenster” changed (a window gets closed or open). In this case a script (in which i would read the trigger-item (window) ) should be run. I would like to have the script separately in case i must use with an oder rule.

You can’t do it like that. The called rule does not automatically inherit the variables that exist in the calling rule. So there is no event.itemName to access.

Don’t do that. Wait until you have an absolute need to use it in an other rule. And then first consider how you might combine those rules into one.

Having a rule call another rule should be the exception, not the rule and definitely not something you should do “just in case”.

In those cases where you do need to have a rule call another rule, the called rule must either assume it will be passed nothing, or you will have to pass to the rule everything that the rule needs. Of course, you won’t be able to use the “run a rule” UI action to do that. You’ll have to create a Script Action to gather all the data you want to pass to the rule into a Map and then include that Map as part of the call to the rule.

Blockly’s run a rule block supports this.

But especially given what this proposed rule does, I’d recommend creating a new Block Library (under Developer Tools → Block Libraries, see Tutorial: How To Write Block Libraries (also linked to from the official docs).

ok I understand. Thank you for the explanation!