[SOLVED] JSR223 - Javascript: Cannot access group object

jsr223
javascript
Tags: #<Tag:0x00007f6ce55803a0> #<Tag:0x00007f6ce5580210>

(Henning F. Mettge) #1

Hi,

I can get all members of a group with

var members = getItem("gDeviceStatus").getAllMembers();

but if I try to access it via

for (var mem in members) {
    logInfo("Mem", "mem " + mem);	
}

I get nothing.

When I echo the variable with

logInfo("Mem", "mem.ACCESS " + members);

I get

[
FibEye_1_Status (Type=SwitchItem, State=ON, Label=Sensor Küche Status, Category=network, Groups=[gDeviceStatus]),
FibEye_2_Status (Type=SwitchItem, State=ON, Label=Sensor WoZi Status, Category=network, Groups=[gDeviceStatus]), 
FibEye_3_Status (Type=SwitchItem, State=ON, Label=Sensor ElZi Status, Category=network, Groups=[gDeviceStatus]), 
FibEye_4_Status (Type=SwitchItem, State=ON, Label=Sensor KiZi Status, Category=network, Groups=[gDeviceStatus]), 
FibEye_5_Status (Type=SwitchItem, State=OFF, Label=Sensor ArZi Status, Category=network, Groups=[gDeviceStatus]), 
FibEye_7_Status (Type=SwitchItem, State=ON, Label=Sensor Flur Status, Category=network, Groups=[gDeviceStatus]), 
FibEye_8_Status (Type=SwitchItem, State=ON, Label=Sensor Treppe unten Status, Category=network, Groups=[gDeviceStatus]), 
Aeon_1_Status (Type=SwitchItem, State=ON, Label=Sensor Bad Status, Category=network, Groups=[gDeviceStatus]), 
Philio_1_Status (Type=SwitchItem, State=ON, Label=Sensor Hütte Status, Category=network, Groups=[gDeviceStatus]), 
BackupNas_Status (Type=SwitchItem, State=ON, Label=NAS BACKUP, Category=network, Groups=[gDeviceStatus]), 
SmokeDetector_1_Status (Type=SwitchItem, State=ON, Label=Rauchmelder Dach, Category=network, Groups=[gDeviceStatus]), 
Hall_Spotlights_1_Status (Type=SwitchItem, State=ON, Label=Flur Spotlights, Category=network, Groups=[gDeviceStatus]), 
Smoke_Detector_1_Status (Type=SwitchItem, State=ON, Label=Rauchmelder Dach Status, Category=null, Groups=[gDeviceStatus])

]

What do I wrong?

Bye
HFM


(Henning F. Mettge) #2

@lewie Are you the lead for JSR223? Can you help please?


(Helmut Lehmeyer) #3

Yes, this is crazy for JavaScript.
https://docs.oracle.com/javase/8/docs/technotes/guides/scripting/nashorn/api.html

If it is unknown, it often helps to test class.
Then you will see there is not a typical known JS Object but a java class:

logInfo( members.class ); 
// -> class com.google.common.collect.RegularImmutableSet

To iterate a (java) Set in JS use forEach.

var members = getItem("gDeviceStatus").getAllMembers();
	
//https://eclipse.org/smarthome/documentation/javadoc/index.html?org/eclipse/smarthome/core/library/items/class-use/SwitchItem.html
logInfo( members.class );
members.forEach(function(key) {
	logInfo( "Prints a toString output: "+ key );
	logInfo( "As everything in JS: "+ typeof key );
	logInfo( "OpenHAB Item class: "+ key.class );
	logInfo( "OpenHAB Item Type: "+ key.getType() );
	logInfo( "Item Name: "+ key.getName() );
	logInfo( "Item State: "+ key.getState() );
});

Let’s put some light into the dark :wink:


(Henning F. Mettge) #4

@lewie Thank you very much!!!