Switch binding with feedback

I’m stuck trying to get a switch to bind with an external http call:

So far I have 2 Items working nicely:

String or_office_music_status "Music Switch [MAP(switch.map):%s]" (Music) {http="<[http://192.168.0.16:8080/controller/rest/status/5572173:60:XSLT(office_music.xsl)]"}

This polls and brings back the values on and off which are mapped to ON and OFF in switch.map

Next I have

Switch or_office_music "OR Office" (Music) {http=">[ON:POST:http://192.168.0.16:8080/controller/rest/control/2502/on] >[OFF:POST:http://192.168.0.16:8080/controller/rest/control/2502/off]"}
This can turn on an off the switch on the remote system.

My problem is how to combine the two so that the switch updates when the external system is updated. I know I need to use rules but I can’t find an example that matches what I am doing.

Do you really need two items? The switch item can send commands and receive updates (with transformation and polling). You’d also want to include the autoupdate=“false” (in the bindings definition) that tells openHAB to not automatically set the state of the switch when a command is processed. With autoupdate=“false”, the switch state will only be updated from the http binding.

I’m not sure how this looks in the UI. For example, the UI might still show the switch to be in the position it was last set until the next http update arrives or the UI reload.

Why you dont use mqtt to communicate with your node?

Hi Steve thanks for the reply. That would be perfect, I thought I found a thread that explained this was possible but I can’t find it anymore. Do you know the syntax for this?

I have all the commands but just not in one item!

I’ve never come across mqtt so will have a look!

It’s very good to comunicate beetwen IOT devices…

Look for examples and google it.

:smile:

I had a quick look and it looks clever but at the moment I am already working with 3 layers to get this working! I’ve got Openremote which I’ve invested quite a lot of time which IMHO has gives you a better gui than Openhab however it is a pain to configure which is where Openhab seems to have the advantage. So for now I am just trying to get them to work together and interfacing to all my devices. mqtt would be another complication for now.

So I tried the following which half works! This allows me to switch on/off the music but it does not get updated if I turn on the switch directly. In the console there is a warning saying could not find mapping for ON in switch.map

The xml brings back the values off and on which I map in switch.map to equal ON and OFF. I have also been able to uppercase the xml results so mapping is not required as the results are ON or OFF.

Switch or_office_music_status "Music Switch [MAP(switch.map):%s]" (Music) {http=">[ON:POST:http://192.168.0.16:8080/controller/rest/control/2502/on] >[OFF:POST:http://192.168.0.16:8080/controller/rest/control/2502/off]" <[http://192.168.0.16:8080/controller/rest/status/5572173:60:XSLT(office_music.xsl)]", autoupdate="false"}

Hi Steve - I am still having trouble with this - can you have a look at what I’ve done?? I feel I am very close but the openhab documentation is pretty limited so I am going in circles!

Julian, the autoupdate=“false” clause is just another comma-separated binding configuration in the bindings part of the item. You can see some examples of it being used at https://github.com/openhab/openhab/wiki/Samples-Binding-Config. I agree that the documentation is very weak for this feature. It took me a while to figure it out what it really did. I didn’t fully understand it until I looked at the binding source code.

I’ve put in the autoupdate function but I think the issue is that it then only takes into account what you do at the openhab end. So I can turn on an off the switch on my Openremote application but if I turn the switch on and off directly in Openremote this is not reflecting in the Openhab front end. I am not really a programmer and I looked at the source code I can see something being referenced about an autoupdatebinding but it does not make much sense to me! Please let me know if you can help more or whether I should bounce this elsewhere to see if I can resolve it?

Are you watching the log files closely? I’d also recommend turning on debug level logging for http. Looking again at the item definition you posted earlier, it appears there is an extra double quote in the http binding configuration (between the input and output configurations). I’d expect to see some errors in the log file related to that if it’s actually in your item definition. On the other hand, it might just silently ignore part of the http binding (like the inbound config after the quote).

Ha Steve - well spotted! It is such a long command line but it was staring me in the face…In the meantime I’d just got it working with Rules but this looks a better solution as it is all in one place. I’ll test for a bit but it looks to be working fine.

Thanks for the help!