How to post command to Item using HTML form

  • Platform information:
    • Hardware: Windows 10 Laptop
    • OS: Windows 10
    • Java Runtime Environment: Zulu Java 8
    • openHAB version: 2.5.6

I would like to create a HTML form that consists of a button, that when clicked, posts to the REST API and sets the temperature of a thermostat to 20 degrees Celcius.

On my local host REST API docs page, I can successfully change the state of the item using the “Try it Out” button located under “Items” and “Post.” On that page, the CURL command sends a value of “20” to my item and it works.

curl -X POST --header “Content-Type: text/plain” --header “Accept: application/json” -d “20” “https://localhost:8443/rest/items/zwave_device_1c17d314_node3_thermostat_setpoint_cooling

Also on the REST API page, if I issue a GET command to the item, I get the following response:

{
“link”: “https://localhost:8443/rest/items/zwave_device_1c17d314_node3_thermostat_setpoint_cooling”,
“state”: “23.3333333333333333333333333333333 °C”,
“stateDescription”: {
“pattern”: “%.0f”,
“readOnly”: false,
“options”: []
},
“editable”: false,
“type”: “Number:Temperature”,
“name”: “zwave_device_1c17d314_node3_thermostat_setpoint_cooling”,
“label”: “Setpoint (cooling)”,
“category”: “Heating”,
“tags”: [],
“groupNames”: []
}

What should my HTML form parameter names and values be to change the status of the thermostat?

Here is my HTML without the brackets <> until I figure out how to use a code fence to display the HTML.

form method=“post” enctype=“text/plain” action=“not allowed to post a third link since I’m a newbie”
input type=“hidden” name=“status” value=“20”/
input type=“submit” value=“Submit”/
/form

The openhab.log file confirms my HTML form posts are received, but the status value is not accepted. The log shows:

[WARN ] [rest.core.internal.item.ItemResource] - Received HTTP POST request at ‘items/zwave_device_1c17d314_node3_thermostat_setpoint_cooling’ with an invalid status value ‘status=20.0’.

How to use code fences - Tutorials & Examples - openHAB Community

1 Like

I think most browsers will not allow you to post form data without it being in key=value format.
You might need a little javascript to do what you want.

If you must use an HTML form, you could post to an interim String type Item and have a rule parse out the bits you are interested in.

A standard HTML form with parameters are submitted as key=value pairs. Are you saying I shouldn’t be using a key=value pair? If that’s true, I see what you’re saying.

I’m using a HTML input element with type set to hidden, name set to state and value set to 20. When the form is submitted, the receiving end will receive the parameter as state=20. I shouldn’t need any Javascript. I can’t find in the documentation what the name of key in the key=value pair should be to update the state of an Item. Is it “state=20” or something else? Or no key?

My end goal is to build my own HTML page for managing my home automation. So getting a simple HTML form to post a command is step one.

That’s it.

the endpoint that receives the POSTed web page is the ITEM. So in the body of the POSTed data is to be the value only. To achieve this the enctype of the form action needs to be text/plain.
But it’s not possible to suppress the key from being submitted by using a plain web form without javascript.
See e.g. https://stackoverflow.com/questions/6063916/is-it-possible-to-post-data-to-an-html-form-without-it-being-in-a-key-value-coll

Thank you @rossko57 and @Wolfgang_S both of your replies helped. The key is not to provide a key ha ha. I can submit just a value using JavaScript.