REST API not changing state or reverting back to old state

  • Platform information:

    • Hardware: _Raspberry Pi Zero W
    • OS: Latest Stable OpenHABian SD card image
    • Java Runtime Environment: openjdk version “1.8.0_265”
    • openHAB version: 2.5.8 (Build)
  • Issue of the topic:
    I am attempting to use the REST API to change the state of an item. I have tried multiple items linked to things that use different bindings, all of which I can successfully control via Paper UI, so I assume it is the REST API config that is the issue and not my basic configuration.
    In the REST API Documentation I have used the “Try it out” button to send the “PUT /items/{itemname}/state” request. If I enter invalid data it fails to parse it correctly. If I enter valid data I get a 202 response code.
    All of this generates activity in the log files indicating that what is supposed to happen has happened, however a second or two following any state change, a second line is added to the events log which indicates that the state has been put back to what it was previously.
    What am I doing wrong?

  • Please post configurations (if applicable):

    • Items configuration related to the issue
    • Sitemap configuration related to the issue
    • Rules code related to the issue
    • Services configuration related to the issue
  • If logs where generated please post these here using code fences:

Invalid Command

curl -X PUT --header "Content-Type: text/plain" --header "Accept: application/json" -d "FOO" "http://openhab:8080/rest/items/Heater_Power/state"

Response Code

{
  "error": {
    "message": "State could not be parsed: FOO",
    "http-code": 400
  }
}

openhab.log

2020-09-05 16:25:51.903 [WARN ] [rest.core.internal.item.ItemResource] - Received HTTP PUT request at 'items/Heater_Power/state' with an invalid status value 'FOO'.

Valid command

curl -X PUT --header "Content-Type: text/plain" --header "Accept: application/json" -d "ON" "http://openhab:8080/rest/items/Heater_Power/state"

Response Code

202

events.log

2020-09-05 16:22:45.806 [vent.ItemStateChangedEvent] - Heater_Power changed from OFF to ON
2020-09-05 16:23:04.024 [vent.ItemStateChangedEvent] - Heater_Power changed from ON to OFF

Tested it on my sytem, working properly!
The log you posted shows the second ChangegEvent 19 seconds later, is that the one you are talking about?
However, if the linked device is sendiing a state back to openhab, that would be seen as the change back in the log!

It is the second line that I’m talking about that looks to me like it’s reverting back, but not changing at all and some weirdness with how the event log works is perfectly plausible too.
So I guess then my question becomes what is causing the API to not actually change the state, but ping the device in such a way as to get a state update (unless that’s just caused by the API checking to make sure the state has changed or something).
I’ve tried it both with the TP-Link smart power plug that my heater is attached to in the example above, but also with the color of some Nanoleaf panels, and in both cases I can manually change their state via Paper UI, but the API won’t do it, it just reports that it has (and then I guess that it hasn’t a few seconds later).

Use post to change the state of an item

curl -X POST --header "Content-Type: text/plain" --header "Accept: application/json" -d "ON" "http://openhabianpi:8080/rest/items/Heater_Power"

2 Likes

Get supported hardware first. No doubt the system is memory starved. I found it a struggle on a Pi 3B+.

@denominator has it.
Item commands are not states.
Sending a command to an Item (REST using POST) may or may not eventually result in a state change, depending on configuration.
Sending a state update to an Item (REST using PUT) should immediately update state (but of course something else may change it back again)

1 Like

Thanks, that works.

Cool

Mark the post as the solution as it will help other people easily find it in the future.

1 Like

I’m having the same problem when using PHP Curl. When I request a state change via PUT it changes state but then it changes back after a second. When I try using POST I get the following error:
2021-11-21 14:34:39.354 [WARN ] [s.impl.WebApplicationExceptionMapper] - javax.ws.rs.ClientErrorException: HTTP 405 Method Not Allowed

So PUT is allowed but doesnt maintain the state I want, and POST doesnt seem to be allowed :expressionless:

That’s all it is supposed to do. Have you looked in your events.log to confirm it really changes Item state?

“it” is all important here. It’s not the PUT you sent some time previously. So, got anything linked to the mystery Item? Rules that update it?

Thanks for the reply/
The log file for the PUT looks good. It shows the state changes when requested. But then they are overwritten a second later.
There are no other rules or scripts applied. I’m only using Habian as an API to get/post my items.
The item I am trying to control is a TADO radiator switch. “Radiator_HeatingPower” is either 0.00 or 100.
http://openhabian:8080/rest/items/Radiator_TargetTemperature/state

Log file:
2021-11-21 14:36:14.141 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item ‘Radiator_HeatingPower’ changed from 0.00 to 100 <------received after my post event

2021-11-21 14:36:17.716 [INFO ] [openhab.event.ItemStateChangedEvent ] - Item ‘Radiator_HeatingPower’ changed from 100 to 0.00 <------occurs1-2 seconds after the above log

So, your Item is linked to a device. Whatever you do to your Item state, the device will come along a bit later and update the Item state to reflect its truth. That’s all working as it is supposed to.

Did you mean to send a command? A command is an instruction, and will get passed along to the device for it to carry out. You’ll find openHAB makes more sense if you can understand the difference between command and state.

I figured the problem out. The API request doesn’t need the /status at the end.

http://openhabian:8080/rest/items/Radiator_HeatingPower

the above works as a POST request.