HTTP help for a Newbie

I have never done HTTP before, but I have managed to create an html file that works fine from my browser. I would like to do this from OH3. I have installed the Binding and I think I configured the Thing, but I have not found any examples of how to do it. I have gone through the docs and a bunch of posts, but still have not found anything that really shows how to do it. It would be nice if someone has a doc that is HTTP for OpenHab a beginners tutorial. Once I see how to do it, I can comprehend pretty quickly.
My goal is to turn on my TV and get the status. Here are the 2 commands that work in the browser. I did it as a form so I could actually change the options before hitting submit.

TV on-off.txt (243 Bytes) TV power.txt (338 Bytes)

Any help would be appreciated. Thank you

What are you trying to do here, create a simple UI (webpage) of your own that does something to openHAB?

I am trying to send commands to my IR controller that turns on my TV and then verify that it is on. I have to use http post to do this. Those commands work through the browser, but I would like to send them using OH3.

Let me share an example. Here is my thing:

UID: http:url:e5a14ec3b0
label: Lareira
thingTypeUID: http:url
configuration:
  authMode: BASIC
  ignoreSSLErrors: true
  baseURL: https://192.168.129.10:8000/recepcion_datos_4.cgi
  delay: 0
  stateMethod: POST
  refresh: 120
  commandMethod: POST
  contentType: text/plain
  timeout: 3000
  bufferSize: 2048
channels:
  - id: switch
    channelTypeUID: http:switch
    label: Switch
    description: ""
    configuration:
      onValue: "1"
      mode: READWRITE
      stateContent: idOperacion=1002
      commandTransformation: JS:cmdEcoforestSwitch.js
      offValue: "0"
      stateTransformation: JS:getEcoforestSwitch.js

A small explanation:

  1. OH uses ‘0’ and ‘1’ as a switch status. So, when I query the device status (using a POST with idOperacion=1002), I have to convert its answer to a ‘0’ or a ‘1’. I use getEcoforestSwitch.js for that. It receives the device answer, parses it, and returns either a ‘0’ or a ‘1’. Therefore, even when I switch the device on/off using another method, OH is able to collect the actual status
  2. When I want OH to switch the device on or off, I use cmdEcoforestSwitch.js for that. It receives either ‘0’ or ‘1’ as input and converts it to the string that the device expects to switch on or off. The binding sends this output to the device with a POST

This requires an HTTP binding version that, at the time of writing, is only available in snapshots. Probably it will be included in OH3.1.0.M2 by month end. My device accepts text/plain for the commands, in your case you should probably use text/xml or text/html.

My actual configuration has more channels. It’s a pellet stove and I use these other channels to control temperature and alarms. The concept is the similar to the switch shown above (the main difference is that you are not obliged to have ‘0’ and ‘1’ states).

  - id: temp
    channelTypeUID: http:string
    label: Temperature
    description: null
    configuration:
      mode: READONLY
      stateTransformation: JS:getEcoforestTemp.js
      stateContent: idOperacion=1002
  - id: stat
    channelTypeUID: http:string
    label: Status
    description: null
    configuration:
      mode: READONLY
      stateTransformation: JS:getEcoforestStat.js
      stateContent: idOperacion=1002
  - id: temp_obj
    channelTypeUID: http:string
    label: Set Temperature
    description: null
    configuration:
      mode: READWRITE
      stateContent: idOperacion=1002
      commandTransformation: JS:cmdEcoforestTempObj.js
      stateTransformation: JS:getEcoforestTempObj.js
  - id: alarm
    channelTypeUID: http:string
    label: Alarm
    description: null
    configuration:
      mode: READONLY
      stateTransformation: JS:getEcoforestAlarm.js
      stateContent: idOperacion=1079

In the case of a IR remote additional channels can be used to control other TV functions.

Hope this helps.

1 Like

This looks great. I will play with it and see what I can do. Thank you

Thank you for the help. This really got me started. I was actually able to send a command and turn on my TV using the following thing. When I turned on the switch, it sent the command and turned on the TV. Could you possibly give me some example of your scripts for idOperacion.

UID: http:url:25ab5409b9
label: WACI NX+
thingTypeUID: http:url
configuration:
  authMode: BASIC
  ignoreSSLErrors: true
  baseURL: http://192.168.1.70/rpc/
  refresh: 30
  commandMethod: POST
  encoding: application/x-www-form-urlencoded
  contentType: text/plain
  timeout: 3000
  bufferSize: 2048
channels:
  - id: TVpower
    channelTypeUID: http:string
    label: TV Power
    description: ""
    configuration:
      onValue: Method=IR_SendCommand&Param1=1&Param2=PhilipsTV&Param3=power

Thank you

Good. But it seems that you are using the 3.0 http binding and not the snapshot one. With the 3.0 binding you will be able to send commands using POST but will not be able to read the TV status using POST (it only supports GET for state).

But one thing at the time. In my case I have onValue as “1” (and offValue as “0”) and use a js transformation to convert from 0 or 1 to the command that the device expects. Here is the code for cmdEcoforestSwitch.js (that I’ve created in conf/transform)

(function(i) {
    var resultado = "idOperacion=1013&on_off=" + i;
    return resultado.trim();
})(input)

You can achieve something similar with onValue as “1” and offValue as “0” (this is just a guess)

(function(i) {
    var resultado = "Method=IR_SendCommand&Param1=" + i + "&Param2=PhilipsTV&Param3=power";
    return resultado.trim();
})(input)

If/when you use the enhanced http binding you will be able to query the state using POST. For now you can only use GET. The main difference is that you can use a body for POST but not for GET. In my example, to get the device status I send a POST with idOperacion=1002. The device returns something like

error_MODO_on_off=0
on_off=1
modo_operacion=1
modo_func=1
estado=4
consigna_potencia=5
consigna_temperatura=22.5
temperatura=20.4
temperatura_ext=---.-
0

I have to parse this in order to get 0 or 1 from on_off (line 2) with getEcoforestSwitch.js

(function(i) {
    var array = i.split("\n");
    if (array.length > 6) {return array[1].split("=")[1];}
})(input)

If you find a way to read your device state with GET then you can use a transform without the need to use the enhanced http binding.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.