I have a gate that I open and close that pressing virtually a button.
My configuration looks like this:
Sitemap
Switch item=Open_Gate icon="door" label="Gate" mappings=[ON="Open"]
Items
Switch Open_Gate "Open Gate"
Rule
rule "Open Gate"
when
Item Open_Gate received command ON
then
if (H_B1.state == ON)
{
return
}
sendCommand(H_B1, ON)
createTimer(now.plusNanos(400000000), [|
sendCommand(H_B1, OFF)
])
end
Now I’ve installed a contact that return the Open/Close state of the Gate and I would like to transform my “Button” into a real switch based on the contact value.
What does the state of the button have to do with the state of the contact? The button is never really in any meaningful state at all. It’s just a means to convey an event to cause the gate motor to turn on for 4 seconds.
I would recommend keeping the three items separate still. That way you can keep track of whether the gate is opened or closed, tell whether the gate is in motion, and trigger the gate to move. And when triggered, you can use the status of the other two items in deciding what to do about the trigger.
If you try to combine and button and motor into I’ve Item, you lose that.
Well I want to keep both indeed. A button that “inverts” the state of the gate because it doesn’t know if it’s open or closed. For the record, the rule mimics a push on a button on my Nikobus, so it presses it and releases after 400ms like a human would do.
But I also would like to add a switch that does both: ON when the gate is opened, and OFF when it isn’t and open it when I manually switch it on and close it when I switch it off.
Visually it’s more immediate and also pressing a button lacks feedback imo. Imagine having to deal with a single button (inverter) and a state for each light in your house ?
Anyway I’m not trying to convince you… Just wanted to know if the answer I found is up to date with what v4 of OpenHab can do.
But you don’t control the lights of your house with a push button usually. It’s a switch which has a state. Push buttons though don’t usually have a meaningful state. They generate events. Most bindings don’t even provide a state Channel for these sorts of interactions. They provide event Channels which are directly used to trigger rules and cannot be linked to an Item at all (except by using a special profile).
You already know that the gate is open or closed based on the contact. You can change how the button is shown on the UI based on the state of the contact (e.g. when the contact Item says the gate is open, the button reads “Close” and when the contact says the gate is closed the button reads “Open”). You don’t need to mirror the state of the contact for the Item whose only purpose is to convey a command event to a rule.
In fact, in MainUI, you don’t even need that Item. The UI widget can call your rule directly without going through an Item. So you could create a widget that bypasses the Switch Item entirely that still has all the information it needs to show “Open/Close” on the button based on the state of the contact.
It’s not really because the up-to-date approach would be to not try to synchronize this Switch Item with the Contact in the first place. It would be to use all the relevant Items to show the widget as necessary on the UI or, depending on other stuff, eliminate the need for the Switch Item in the first place.