Openhab Configuration
## Release = Debian GNU/Linux 12 (bookworm)
## Kernel = Linux 6.1.0-10-amd64
## Platform = VMware Virtual Platform/440BX Desktop Reference Platform
## CPU Usage = 1.49% avg over 2 cpu(s) (1 core(s) x 2 socket(s))
## CPU Load = 1m: 0.08, 5m: 0.02, 15m: 0.01
## Memory = Free: 0.88GB (23%), Used: 3.16GB (77%), Total: 3.82GB
## Swap = Free: 0.95GB (100%), Used: 0.00GB (0%), Total: 0.95GB
## Root = Free: 10.20GB (73%), Used: 3.73GB (27%), Total: 14.70GB
Problem Statement
I’m using the http binding to query and set a media device. It uses XML which has been a challenge for me. I’ve been able to create a time-based js script that turns on the radio, sets the tuner frequency, and the default volume. With the http binding, I’m able to query the settings on the radio, parse the xml data, and update items, The challenge is when trying to control the radio via Openhab. To configure it, I must be able to send the appropriate request payload. I could use a dummy item and trigger the change with scripts but I wish to learn how to use the transformation functions.
Steps Tried
- Created a http channel thing for turning on/off the device. Parsed the state with XPATH expression
- Linked it to an item without using any profile transformation
- Verified with Wireshark that request went out on the wire with the respective ON/OFF value
- Created a js tranformation via the GUI
- Went back into http channel and click on existing item and change the transformation to js transformation. Assigned script.
- Enabled TRACE on http binding and verified no errors.
- Turn switch ON and OFF and verified incoming data and returned value from js script transformation on console.
- Verified with Wireshark that no request packet was sent out on the wire.
Questions
- Why is the item profile display only when I access the item from the thing channel?
- What is being passed to the profile transformation? Is it an object or string? I presume it is the latter because I’m able to successfully do a comparison.
- Can the returnValue be a long string? I’m suspecting that somehow, the binding is treating it like a null.
- Is there anymore diagnostic data available from the http binding?
Configuration items are shown below.
http Thing Configuration
UID: http:url:Yamaha_CRX-N560
label: Yamaha CRX-N560
thingTypeUID: http:url
configuration:
authMode: BASIC
headers:
- content-type=application/xml
ignoreSSLErrors: false
baseURL: http://ipaddress/YamahaRemoteControl/ctrl
delay: 5000
stateMethod: POST
refresh: 30
commandMethod: POST
contentType: text/xml
timeout: 30000
bufferSize: 2048
location: Kitchen
channels:
- id: PowerStatus
channelTypeUID: http:string
label: Power Status
description: ""
configuration:
mode: READONLY
stateTransformation: XPATH:/YAMAHA_AV/System/Power_Control/Power
stateContent: <?xml version="1.0" encoding="utf-8"?><YAMAHA_AV
cmd="GET"><System><Power_Control><Power>GetParam</Power></Power_Control></System></YAMAHA_AV>
- id: PowerControl
channelTypeUID: http:switch
label: Power Control
description: ""
configuration:
mode: READWRITE
onValue: On
stateContent: <?xml version="1.0" encoding="utf-8"?><YAMAHA_AV
cmd="GET"><System><Power_Control><Power>GetParam</Power></Power_Control></System></YAMAHA_AV>
offValue: Standby
stateTransformation: XPATH:/YAMAHA_AV/System/Power_Control/Power
- id: InputSel
channelTypeUID: http:string
label: Input Select
description: ""
configuration:
mode: READONLY
stateTransformation: XPATH:/YAMAHA_AV/System/Basic_Status/Input/Input_Sel
stateContent: <?xml version="1.0" encoding="utf-8"?><YAMAHA_AV
cmd="GET"><System><Basic_Status>GetParam</Basic_Status></System></YAMAHA_AV>
JS script transformation created via GUI
(function(data) {
PowerSet = '<?xml version="1.0" encoding="utf-8"?><YAMAHA_AV cmd="PUT"><System><Power_Control><Power>data</Power></Power_Control></System></YAMAHA_AV>';
console.log("Incoming data is:", data);
if (data == "ON") {DesiredPower = "On"} else (DesiredPower = "Standby");
returnValue = PowerSet.replace("data", DesiredPower);
console.log("Transformation is:", returnValue);
return returnValue
})(input)
Console log when js transformation script is used. Incoming data is OFF and a return string is returned.
==> /var/log/openhab/openhab.log <==
2023-08-26 09:48:09.975 [TRACE] [.HttpDynamicStateDescriptionProvider] - returning new stateDescription for http:url:Yamaha_CRX-N560:PowerControl
2023-08-26 09:48:09.975 [INFO ] [org.openhab.automation.script ] - Incoming data is: OFF
==> /var/log/openhab/events.log <==
2023-08-26 09:48:09.975 [INFO ] [openhab.event.ItemStateUpdatedEvent ] - Item 'Power_CRX_N560' updated to OFF
2023-08-26 09:48:09.975 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item 'Power_CRX_N560' changed from ON to OFF
==> /var/log/openhab/openhab.log <==
2023-08-26 09:48:09.975 [INFO ] [org.openhab.automation.script ] - Transformation is: <?xml version="1.0" encoding="utf-8"?><YAMAHA_AV cmd="PUT"><System><Power_Control><Power>Standby</Power></Power_Control></System></YAMAHA_AV>
Wireshark Capture on Openhab installation shows outgoing request with no transformation. No request goes out if a js transformation is used.
Hypertext Transfer Protocol
POST /YamahaRemoteControl/ctrl HTTP/1.1\r\n
[Expert Info (Chat/Sequence): POST /YamahaRemoteControl/ctrl HTTP/1.1\r\n]
[POST /YamahaRemoteControl/ctrl HTTP/1.1\r\n]
[Severity level: Chat]
[Group: Sequence]
Request Method: POST
Request URI: /YamahaRemoteControl/ctrl
Request Version: HTTP/1.1
Accept-Encoding: gzip\r\n
User-Agent: Jetty/9.4.50.v20221201\r\n
Content-Type: text/xml\r\n
Content-Type: application/xml\r\n
Host: kitchen.radio.ip\r\n
Content-Length: 2\r\n
[Content length: 2]
\r\n
[Full request URI: http://kitchen.radio.ip/YamahaRemoteControl/ctrl]
[HTTP request 1/1]
File Data: 2 bytes
eXtensible Markup Language
On