Send new state to an ITEM

Hi There

I Try to make a script where I send a command (a new value) to the Item in openhab3 ECMAScript-2021

This is working:

var new_state = 5;
items.getItem('Test_item').sendCommand(new_state);

But the following not:

var Nap_allas=items.LocalSun_Azimuth.state
var new_state = Nap_allas.toString;
items.getItem('Test_item').sendCommand(new_state);

Where I made a mistake?
Thanks in advance

How about:

And I don’t think that you need the ‘.toString’ on ‘Nap_allas’

There are a couple of different issues here, I suspect. First, @opus is correct. Because you are still on OH3, unless you have manually updated the helper libraries, you must still use the getItem syntax. Forms like this

items.LocalSun_Azimuth.state

Were introduced into JSscripting for OH4.

Next, in JSscripting, if you get the state of an item it has already been converted to a string. You have to use a different property, rawState, to get the state in it’s original, non-string, type. Calling toString on a string isn’t a problem, it’s just not necessary.

Lastly, however, if you just use Nap_allas.toString, that is referencing thetoString function, not calling it. This means that your Nap_allas variable has now become the function itself, not the output of the function! Not surprisingly, sendCommand doesn’t know what to do when you give it a function for input instead of the string it is expecting. To make sure you get the function’s output, you need () after the function name: Nap_allas.toString().

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