You guys are ahead of me!!!
I’m so busy actually writing it, I don’t get time to even set my own system up to use it! lol
Ok, I did and example on mine which works to show you. First add this little utilities to help make the json easier.
/*
* Utility functions
*/
def makeButton(bid,label,type,icon=null,state=null,next=null) {
var str = ""<<((bid==1)?"":",")
str<<'{"bid":'<<bid<<',"label":"'<<label<<'","type":'<<type
if (next!=null) {
str<<',"next":'<<next
}
if (state!=null) {
str<<',"state":'<<state
}
if (icon!=null) {
str<<',"icon":'<<icon
}
str<<'}'
return str
}
def makePage(pid,name,format) {
var str = new StringBuilder('{"refresh":')
str<<'{"pid":'<<pid<<',"name":"'<<name<<'","format":'
return str
}
def makeEmptySync(pid) {
var str = new StringBuilder('{"sync":')
str<<'{"pid":'<<pid<<'}}'
return str
}
def makeSyncButtonStart(pid,bid,state) {
var str = new StringBuilder('{"sync":')
str<<'{"pid":'<<pid
str<<',buttons:[{"bid":'<<bid<<',"state":'<<state<<'}'
return str
}
def addSyncButton(bid,state) {
var str = ',{"bid":'<<bid<<',"state":'<<state<<'}'
return str
}
/*
* Utility functions
*/
you will have most but I’ve put them all in that I have.
Then for you page build
case "10" :
movie = ir.getItem("movie_room_lights").state==ON?1:0
lounge = ir.getItem("LoungeLights_LoungeLights").state==ON?1:0
if (refresh) {
json = makePage(id,'Lounge',format)
json<<format<<',buttons:['
json<<makeButton(1,"Movie",1,1,movie)
json<<makeButton(2,"Lounge",1,1,lounge)
json<<makeButton(3,"Hall",2,6)
json<<makeButton(4,"Bedroom",10,5,5,11)
json<<makeButton(5,"Temp",10,9,9,12)
json<<makeButton(6,"Light",1,3)
json<<makeButton(7,"Heat",10,9,3,15)
json<<makeButton(8,"Status",10,16,15,17)
json<<"]}}"
} else {
//json = makeEmptySync(10)
json = makeSyncButtonStart(10,1,movie)
json<<addSyncButton(2,lounge)
json<<"]}}"
}
events.sendCommand("nxpanel_command",json.toString())
break
You will see;
- You get the state of your items at the start
- You set the state, in both refresh and sync messages
This keeps it right every time you open the page.
I suggest you could also send another sync form a trigger when your item changes too. That way, if the page it open, it will update in real-time too.
Shortly I will add an auto-sync, every minute or so. It will mean you do not need to trigger it like that, but it might be 1 minute out of sync. You can choose how you wish. For me, I’d do the extra work and send the sync when the item changes, as I prefer it live, even if more work. Just depends how much effort you want to put in.
I hope that makes if clearer, so you guys can continue configuring while I keep developering.
As as a reminder, the rule for the other direction. When you press a button on the panel and you want it to turn the light off/on on your system would be this;
import org.slf4j.LoggerFactory
def logger = LoggerFactory.getLogger("org.openhab.core.automation.nspanel")
def str = event.getEvent()
if (str.indexOf('{"button": ')!=0) {
return
}
// {"button": {"pid": 15, "bid": 1, "state": 1, "next": 0}}
var i = str.indexOf("\"pid\"")
var i2 = str.indexOf(",",i+7)
var pid = str.substring(i+7,i2) as int
i = str.indexOf("\"bid\"")
i2 = str.indexOf(",",i+7)
var bid = str.substring(i+7,i2) as int
i = str.indexOf("\"state\"")
i2 = str.indexOf(",",i+9)
var state = str.substring(i+9,i2) as int
if (pid==10 && bid==1) {
events.sendCommand("movie_room_lights",state==1?"ON":"OFF")
}
else if (pid==10 && bid==2) {
//
}
So that’s both parts.
- The light changing and the panel knowing
- You changing the panel and your system being changed.
If you can help other newer users here too as you learn more it would help me, so I dont need to answer everything… it’s exhausting. lol…
This thread is by far the most active in openhab community and I’m feeling it!!