Basic help in getting names and state from a group

Hi all,

I am new to OpenHab and am struggling with trying to achieve what I want. I am working on an android dashboard for my home that will be deployed on various devices in my household.

I am using Pushover to communicate changes to my devices.

What I am trying to do is when an item in my group changes, I would like to retrieve all items in that groups names and current state. I would then like to create json string from these results and send them as a message in PushOver.

I am struggling right from the start really!

I have a group called gHall_Lights, that contains 2 switches and two dimmer items, I am trying to send a json string like
{“Items”:[“light1”:“ON”, “light1dimmer”:60]}

can anyone get me started?

I have:

rule "Update Hall Lights"
when Item gHall_Lights changed
then
end

I have tried gHall_Lights.members.forEach but I get an couldn’t resolve reference error so I am completely stumped! EDIT -! Restarting The designer fixed this bit!

Any help would be gratefully received!

First, are you aware of the REST API which lets you query for this info directly?

Second, using forEach is a little tricky but I think this will work:

    val StringBuilder sb = new StringBuilder()
    gHall_Lights.members.forEach[sw |
        // build JSON
        sb.append("Name = " + sw.name)
        sb.append("State = " + sw.state.toString)
    ]
    sb.toString

The tricky part is you can only access final variables from inside a forEach (i.e. those declared as val as opposed to var). A final variable cannot be reassigned so you can’t use String concatenation (e.g. str = str + " some more string text").

Hi @rlkoshak, thanks for your reply.

Yes I am using the rest api for controlling the lights etc, it was just notifying the other controllers in my network that an update had happened.

I was trying to build a json string with the info, your way looks much cleaner, I will give it a go.

Thanks