Blockly TypeError: (itemRegistry.getItem("MopidytSongId").getState()).indexOf is not a function in <eval> oh3

Hello

I just updated my server on debian 12 (so java 17)

I have a problem with a rule in blockly

2023-07-16 15:28:57.963 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'PlayerOnMopidyPause' failed: TypeError: (itemRegistry.getItem("MopidytSongId").getState()).indexOf is not a function in <eval> at line number 5

yaml


configuration: {}
triggers:
  - id: "1"
    configuration:
      command: PAUSE
      itemName: MopidyPlayer
    type: core.ItemCommandTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      blockSource: <xml xmlns="https://developers.google.com/blockly/xml"><block
        type="oh_log" id="Vm_ilGAa)6;hb@XJ{G.k" x="45" y="64"><field
        name="severity">debug</field><value name="message"><shadow type="text"
        id="f]g1:x|Y#gr3~6HfMlsX"><field name="TEXT">mopidy
        pause</field></shadow></value><next><block type="controls_if"
        id="]}Qw77I}p*28(D:$BaJ-"><value name="IF0"><block type="logic_compare"
        id=".S(-ur0%h%cHY2{4x]^c"><field name="OP">NEQ</field><value
        name="A"><block type="text_indexOf" id="oLK6gB:EziNgy];bVp{."><field
        name="END">FIRST</field><value name="VALUE"><block
        type="oh_getitem_state" id="HjNlm,^EXAL15Zn,y[=$"><value
        name="itemName"><shadow type="oh_item" id="k50bB?H2n_Kw1|=4gYJH"><field
        name="itemName">MopidytSongId</field></shadow></value></block></value><value
        name="FIND"><shadow type="text" id="YLOHGhaR+WQ+@Y#6#+Wu"><field
        name="TEXT">tunein:station</field></shadow></value></block></value><value
        name="B"><block type="math_number" id="xkxc2NOI3qk=yVW`#Bf]"><field
        name="NUM">-1</field></block></value></block></value><statement
        name="DO0"><block type="oh_log" id="wzYey(1EIdQXJUNEb2KO"
        disabled="true"><field name="severity">error</field><value
        name="message"><shadow type="text" id="/v:`C9FEgD*}zzc!+)ic"><field
        name="TEXT">abc</field></shadow></value><next><block type="oh_event"
        id="r+@e@1u{^E3s2o:ix(;!"><field
        name="eventType">sendCommand</field><value name="value"><shadow
        type="text" id="`#c?/tD8kTM-[=:70Cl^"><field
        name="TEXT">ON</field></shadow></value><value name="itemName"><shadow
        type="oh_item" id="]y:rw%a9pCAhnAf0WP4v"><field
        name="itemName">MopidyStop</field></shadow></value></block></next></block></statement></block></next></block></xml>
      type: application/javascript
      script: >
        var logger =
        Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.' +
        ctx.ruleUID);



        logger.debug('mopidy pause');

        if (itemRegistry.getItem('MopidytSongId').getState().indexOf('tunein:station') + 1 != -1) {
          events.sendCommand('MopidyStop', 'ON');
        }
    type: script.ScriptAction

After downgrading to java11 (Modbus transformation OH 3.3 java 17) the problem persists.

What version of OH?

If you’ve also upgraded to OH 4 you need to stay running on Java 17 and, per the release notes, you need to reopen and save each of the Blockly rules to have them converted from Nashorn JS to JS Scripting.

oh 3.4.4 (deb from JFrog)

a usual value of mopidySongId is “tunein:station:s15474”

But what is it when the rule is actually running? If it’s NULL or UNDEF the .state won’t be a String and therefore it won’t have an indexOf() method.

it’s “tunein:station:s15474”

I have added log messages

The generated javascript code

var MopidySongId;

var logger = Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.' + ctx.ruleUID);


logger.info('mopidy pause');
MopidySongId = itemRegistry.getItem('MopidytSongId').getState();
logger.info(MopidySongId);
if (MopidySongId.indexOf('tunein:station') + 1 != -1) {
  events.sendCommand('MopidyStop', 'ON');
}

oh log

2023-07-21 19:31:53.506 [INFO ] [org.openhab.rule.PlayerOnMopidyPause] - mopidy pause
2023-07-21 19:31:53.509 [INFO ] [org.openhab.rule.PlayerOnMopidyPause] - tunein:station:s15474
2023-07-21 19:31:53.513 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'PlayerOnMopidyPause' failed: TypeError: MopidySongId.indexOf is not a function in <eval> at line number 9

mmmh

logger.info(typeof(MopidySongId))

return object

Oh, now I think I know what’s happening.

Here’s a few things:

  • I assumed you are running OH 4 since you upgraded Java. OH 3.4 does not support Java 17.

  • If you are running OH 4, this Blockly should have been converted from Nashorn to JS Scripting. This is relevant here because the way the state of the Item is accessed and what type it is ends up being different for JS Scripting. Assuming you’ve installed JS Scripting, opening the Blockly file and saving it should change the JS code to JS Scripting. One thing of note is in JS Scripting, the state of the Item is by default always a String so your current approach would work just fine.

  • if you are running OH 3.4, once you downgrade back to Java 11, the important thing to realize is the state of the Item is a Java State Object, not a String. I think the Blockly might be smart enough to handle converting the state to a String if you don’t assign it to a variable first. If you do assign it to a variable, you’ll have to use a text block to convert it to a String.Then you can use the indexOf.

Over all, your environment is inconsistent and I don’t even know which JS add-on(s) you have installed nor which version of OH we are working with.

I updated debian and java 17 came with it.

I put java 11 back after learning that OH3.3 didn’t support java 17. So I use OH3.4 + java11. (I delete java 17 from the title of the post which is confusing)

addons :

bundle:list | grep Add-ons
235 │ Active │  80 │ 3.1.0.202102201650     │ openHAB Add-ons :: Bundles :: Snapcast Binding
247 │ Active │  80 │ 3.4.0.202210081928     │ openHAB Add-ons :: Bundles :: ZigBee Binding
248 │ Active │  80 │ 3.4.0.202210081929     │ openHAB Add-ons :: Bundles :: ZigBee CC2531 Bridge
249 │ Active │  80 │ 3.4.0.202210081929     │ openHAB Add-ons :: Bundles :: ZigBee Console
250 │ Active │  80 │ 3.4.0.202210081930     │ openHAB Add-ons :: Bundles :: ZigBee Console Ember
251 │ Active │  80 │ 3.4.0.202210081929     │ openHAB Add-ons :: Bundles :: ZigBee Console Telegesis
252 │ Active │  80 │ 3.4.0.202210081929     │ openHAB Add-ons :: Bundles :: ZigBee Ember Bridge
253 │ Active │  80 │ 3.4.0.202210081928     │ openHAB Add-ons :: Bundles :: ZigBee Serial Driver
254 │ Active │  80 │ 3.4.0.202210081929     │ openHAB Add-ons :: Bundles :: ZigBee Telegesis Bridge
255 │ Active │  80 │ 3.4.0.202210081929     │ openHAB Add-ons :: Bundles :: ZigBee XBee Bridge
263 │ Active │  80 │ 3.4.0.202209180847     │ openHAB Add-ons :: Bundles :: Ecowatt Binding
287 │ Active │  80 │ 3.4.4                  │ openHAB Add-ons :: Bundles :: Automation :: JavaScript Scripting
288 │ Active │  80 │ 3.4.4                  │ openHAB Add-ons :: Bundles :: Automation :: Jython Scripting
289 │ Active │  80 │ 3.4.4                  │ openHAB Add-ons :: Bundles :: Astro Binding
290 │ Active │  80 │ 3.4.4                  │ openHAB Add-ons :: Bundles :: Denon / Marantz Binding
291 │ Active │  80 │ 3.4.4                  │ openHAB Add-ons :: Bundles :: Freebox Binding
292 │ Active │  80 │ 3.4.4                  │ openHAB Add-ons :: Bundles :: HTTP Binding
293 │ Active │  80 │ 3.4.4                  │ openHAB Add-ons :: Bundles :: hue Binding
294 │ Active │  80 │ 3.4.4                  │ openHAB Add-ons :: Bundles :: iCalendar Binding
295 │ Active │  80 │ 3.4.4                  │ openHAB Add-ons :: Bundles :: Kodi Binding
296 │ Active │  80 │ 3.4.4                  │ openHAB Add-ons :: Bundles :: Linky Binding
297 │ Active │  80 │ 3.4.4                  │ openHAB Add-ons :: Bundles :: Log Reader Binding
298 │ Active │  80 │ 3.4.4                  │ openHAB Add-ons :: Bundles :: Modbus Binding
299 │ Active │  80 │ 3.4.4                  │ openHAB Add-ons :: Bundles :: E3DC Modbus Binding
300 │ Active │  80 │ 3.4.4                  │ openHAB Add-ons :: Bundles :: HeliosEasyControls Binding
301 │ Active │  80 │ 3.4.4                  │ openHAB Add-ons :: Bundles :: Modbus SBC Binding
302 │ Active │  80 │ 3.4.4                  │ openHAB Add-ons :: Bundles :: StiebelEltron Bundle
303 │ Active │  80 │ 3.4.4                  │ openHAB Add-ons :: Bundles :: Studer Binding
304 │ Active │  80 │ 3.4.4                  │ openHAB Add-ons :: Bundles :: SunSpec Bundle
305 │ Active │  80 │ 3.4.4                  │ openHAB Add-ons :: Bundles :: MPD Binding
306 │ Active │  80 │ 3.4.4                  │ openHAB Add-ons :: Bundles :: MQTT Broker Binding
307 │ Active │  81 │ 3.4.4                  │ openHAB Add-ons :: Bundles :: MQTT EspMilightHub
308 │ Active │  81 │ 3.4.4                  │ openHAB Add-ons :: Bundles :: MQTT Things and Channels
309 │ Active │  82 │ 3.4.4                  │ openHAB Add-ons :: Bundles :: MQTT HomeAssistant Convention
310 │ Active │  82 │ 3.4.4                  │ openHAB Add-ons :: Bundles :: MQTT Homie Convention
311 │ Active │  80 │ 3.4.4                  │ openHAB Add-ons :: Bundles :: Network Binding
312 │ Active │  80 │ 3.4.4                  │ openHAB Add-ons :: Bundles :: OpenWeatherMap Binding
313 │ Active │  80 │ 3.4.4                  │ openHAB Add-ons :: Bundles :: Pulseaudio Binding
314 │ Active │  80 │ 3.4.4                  │ openHAB Add-ons :: Bundles :: SamsungTV Binding
315 │ Active │  80 │ 3.4.4                  │ openHAB Add-ons :: Bundles :: Spotify Binding
316 │ Active │  80 │ 3.4.4                  │ openHAB Add-ons :: Bundles :: Telegram Binding
317 │ Active │  80 │ 3.4.4                  │ openHAB Add-ons :: Bundles :: Teleinfo Binding
322 │ Active │  80 │ 3.4.4                  │ openHAB Add-ons :: Bundles :: IO :: openHAB Cloud Connector
323 │ Active │  80 │ 3.4.4                  │ openHAB Add-ons :: Bundles :: Persistence Service :: InfluxDB
324 │ Active │  80 │ 3.4.4                  │ openHAB Add-ons :: Bundles :: Persistence Service :: MapDB
325 │ Active │  80 │ 3.4.4                  │ openHAB Add-ons :: Bundles :: Persistence Service :: RRD4j
327 │ Active │  75 │ 3.4.4                  │ openHAB Add-ons :: Bundles :: Transformation Service :: JSonPath
330 │ Active │  80 │ 3.4.4                  │ openHAB Add-ons :: Bundles :: Voice :: Mary Text-to-Speech
331 │ Active │  80 │ 3.4.4                  │ openHAB Add-ons :: Bundles :: Voice :: Pico Text-to-Speech
334 │ Active │  75 │ 3.4.4                  │ openHAB Add-ons :: Bundles :: Transformation Service :: JavaScript

(I must delete zigbee)

I think the Blockly might be smart enough to handle converting the state to a String

//generated by blockly

var MopidySongId;

var logger = Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.' + ctx.ruleUID);


logger.info('mopidy pause');
if (itemRegistry.getItem('MopidytSongId').getState().indexOf('tunein:station') + 1 != -1) {
  events.sendCommand('MopidyStop', 'ON');
}

Same error, Apparently not :frowning:

I’ve used a “inlne script block”, and it works. For me, the problem is solved, but if necessary I’m available for testing :wink:

var MopidySongId;

var logger = Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.' + ctx.ruleUID);


logger.info('mopidy pause');
MopidySongId = MopidySongId = itemRegistry.getItem('MopidytSongId').getState().toString();
logger.info(MopidySongId);
if (MopidySongId.indexOf('tunein:station') + 1 != 0) {
  events.sendCommand('MopidyStop', 'ON');
}

Thanks for your help

Nope, it wasn’t. It all works a lot more smoothly in OH 4 now.

It really isn’t. In OH 4 Blockly “compiles” to a completely different JS and this specific problem isn’t going to occur.

You probably don’t need the inline. This ought to work:

Of course, it’s two block. It definitely works in OH 4, but “get state of item” is a String in the first place. But the code it generates is:

m1 = '';
m1 += String(items.getItem('MyItem').state);

It works!

var MopidySongId;

var logger = Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.' + ctx.ruleUID);


logger.info('mopidy pause');
MopidySongId = '';
MopidySongId += String(itemRegistry.getItem('MopidytSongId').getState());
logger.info(MopidySongId);
if (MopidySongId.indexOf('tunein:station') + 1 != 0) {
  events.sendCommand('MopidyStop', 'ON');
}

I’ve seen a lot of new features in OH4 that I really like, but I’m going to wait a few weeks after the final version is released before updating.

Thanks a lot!

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.