[Jython] Signature of events.sendCommand()? ( 1st arg can't be coerced to org.eclipse.smarthome.core.items.Item)

When using events.sendCommand() in a new function, I suddenly get the error mesage

Jan  7 13:38:05 cubietruck karaf[20646]: TypeError: sendCommand(): 1st arg can't be coerced to org.eclipse.smarthome.core.items.Item, String

Call:

events.sendCommand(heizung_switch_item, new_state)

Type is unicode (I also tried str) and ONOFFType.

13:46:00.770 [DEBUG] [RULES.Heating.PWM.Switch             ] - Prefix u4, Item u4_heizung [<type 'unicode'>], State ON [<type 'org.eclipse.smarthome.core.library.types.OnOffType'>]

What is the problem here? The function call works with the same types in another method!?

Full exception trace:

Jan  7 13:38:05 cubietruck karaf[20646]: Traceback (most recent call last):
Jan  7 13:38:05 cubietruck karaf[20646]: File "/etc/openhab2/lib/python/openhab/triggers.py", line 118, in execute
Jan  7 13:38:05 cubietruck karaf[20646]: self.callback(module, inputs) if self.extended else self.callback()
Jan  7 13:38:05 cubietruck karaf[20646]: File "/etc/openhab2/lib/python/openhab/triggers.py", line 162, in callback
Jan  7 13:38:05 cubietruck karaf[20646]: result_value = fn(*fn_args)
Jan  7 13:38:05 cubietruck karaf[20646]: File "<script>", line 112, in switch_pwm
Jan  7 13:38:05 cubietruck karaf[20646]: TypeError: sendCommand(): 1st arg can't be coerced to org.eclipse.smarthome.core.items.Item, String

Full source at https://gist.github.com/euphi/e56f2be0c88017d4cb97bc7c188957f6

use the name of the item:
“heizung_switch” or get it from the object: heizung_switch_item.name

It is already a string.

However, I found that there really is no sendCommand(string, command), only

  • item, command
  • item, string
  • string, string

but no

  • string, command.

However, I tried all these different variants, but these still result in exceptions after reloading the rule.

I guess that even if the script is reloaded and the problematic function is not called from a timer, the fact that other functions in the script heating.py are called from a running timer, the old script stays in memory for some time?!

After all old timers have finished, the correct variant (now i use item, command) works fine.

As an alternative you can cast the Command to string which is often the easier way to do things! :wink:

I would prefer this too

For now, you are limited to these. Would this work for you… events.sendCommand(item_name_string, command.toString())?

1 Like

For now, you are limited to these . Would this work for you… events.sendCommand(item_name_string, str(command))?

Excellent hint. This works for me :+1:

Glad this helped! I suggest to use as many Java or OH classes/methods as possible, so that there is less effort when migrating to the scripting API. So…

events.sendCommand(item_name_string, command.toString())

For me both works
events.sendCommand(item_name_string, str(command)) and events.sendCommand(item_name_string, command.toString())

Honestly, I’m going a little bit crazy with the mixing of languages :sweat_smile:
And I would never have had the idea to use the Java function toString() here.