How to set up item with unknown value

  • Platform information:
    • openHAB version: 3.1.0
  • Issue of the topic:


I have a few items in my house for which I can send commands (via HTTP) but I am unable to query the current status. E.g., I have a light I can turn on or off via an HTTP command, but I am unable to determine if the light is on or off. There’s no real alternative to assuming an initial value (i.e. the light is off when openhab starts up) and then alternating this value between on and off every time the command is triggered.

How would I implement this in openhab? I’ve had a bit of a search but couldn’t see anything similar - I assume this is a relatively common scenario though so I’m sure this has been asked before! It would be great if I could also add a 2nd button with which I can reset the value of the item manually in case it gets out of sync.

Example at end

Thanks. Still a bit confused. I figured I was going to use the HTTP binding but it looks to me from the docs for that that it assumes there is a way to query the current state via an HTTP request (the parameters for that part of the binding are non-optional). Is that right? If so is the only way to do this by writing scripts/using curl and using a binding that lets me execute shell commands?

I thought you wanted to initialize an Item state to OFF at startup. That’s what the rule does. No curl no HTTP,.

Part of it is that. But the other part is triggering a change in the light status. I would need to invoke the HTTP call and toggle the current status manually since there is no way to ever query it. I don’t THINK that’s possible with the http binding?

How are you using this normally? Have you not set it up so that commanding your Item on turns the light on?

I haven’t done this in openhab before (and the last openhab version I fiddled around with was a v2 version maybe 3 years ago, so I really don’t know much).

Would the right approach be to use the exec binding to create a thing, the command of which calls curl to make the HTTP call, and then have rules which toggle some other item’s state when the thing’s action is run (i.e. when I send on to the run channel of the thing)?

I"m really not sure why you think to get so complicated. Can we go back a step?

You’ve got some device you can send an HTTP command to turn on, a different HTTP command to turn off '- yes?

You can set up an HTTP Thing to do that, write only, and link to an Item. Now you can send commands to your Item from rule or GUI and the device follows.

Let’s get that part working, then worry about startup conditions.

No - I have a single call which toggles (I thought I mentioned that but looks like I forgot to). So if light was on it goes off and vice versa. So the state needs to be managed openHAB side.

Okay, you did say toggle a lot, but people don’t always mean exactly that.

Basically, you are goosed. There is no way to synchronize openHAB with the device without manual intervention. No amount of curl or script can change that.
Either you must provide a way in openHAB to tell it what the device is doing, or (probably easier) you make the device fit what openHAB thinks.

Good news is that configuring HTTP binding is simple, one url for any command.
Default autoupdate will have Item state track commands.

Yep, I realised that from the get go.

So what I have in my head is two switches in the UI. One simply tracks the state openhab thinks the light is in, and when you flip it, it makes the HTTP call and updates the state of the 2nd switch/item. If it turns out openhab is inverted then you can flip the 2nd one and this will update the state of the first one WITHOUT making the HTTP call.

I’m halfway through doing this with exec. I will try doing it with HTTP if you believe it will work (the docs didn’t give me much hope on that) as it would be simpler. I’m just not sure if having a rule which calls postUpdate on the 1st when the 2nd changes will avoid the HTTP call?

Sorry, to be clear this:

One simply tracks the state openhab thinks the light is in, and when you flip it, it makes the HTTP call and updates the state of the 2nd switch/item

Should read:

One simply tracks the state openhab thinks the light is in, and when you flip it, it makes the HTTP call and updates the state of the 1st switch/item AND the 2nd switch/item

I think I’d hide away a single button in the UI for “flip state”. A little rule can listen for any UI command to that dummy “flipstate” Item, and update (not command) the ‘real’ Item to opposite. No HTTP, no curl.
The real Item has a write-only HTTP channel, sends to the same URL on any command.
OH’s autoupdate maintains the ‘real’ Item state as commands are issued.

I’m trying and failing to use the HTTP binding. I tried this thing:

Thing http:url:thing
[ baseURL=“url” ]
Type switch : light “Light” [ mode=“WRITEONLY” ]

but in the web interface for the thing I am seeing status as uninitialized (HANDLER_CONFIGURATION_PENDING). I can’t see any errors in the logs…