-
-
Notifications
You must be signed in to change notification settings - Fork 429
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Toggle functionality for "boolean" types #4381
base: main
Are you sure you want to change the base?
Conversation
Signed-off-by: Martin Grześlowski <[email protected]>
How about a |
Is there a toggle command? |
Currently not - I am suggesting it might make sense to implement one. |
No way. I already saw long discussions about it. If you want you can do it #947 |
Umm. I have an issue with your word 'toggle' .. its dictionary definition is "to switch a feature on a computer on and off by pressing the same button or key" .. in other words the 'toggle' operation executes TWO operations e.g. ON=>OFF=>ON. So I suggest some other word like 'invert' or 'reverse' or 'flip' .. |
I think you misunderstand the Cambridge dictonary (or I did). Look at examples of usage:
Edit: And definition from Google (of course without link to source...) |
I believe |
At least in American English at least, "toggle" is the correct term for something that switches the current state of a switch regardless of the state it's already in. That's the common usage for that term. I cannot speak for other English dialects. I imagine if a way to implement a TOGGLE command in a way that doesn't require modifyinmg all the add-ons that implement Switch Channels it might be more acceptable than where the past discussions went. And most of the controversy in the ESH thread at least was around whether discussion about whether it was a good idea or not should be allowed or if the PR should just be merged without discussion. I personally am not convinced by the arguments against a TOGGLE command. It should be up to the admin of each individual OH instance to decide whether TOGGLE is a dangerous command or not and configure their OH instance accordingly.
You'd still need a test for NULL and UNDEF as that would obviously fail with an error. I'm not sure this approach buys us much. In fact
is preferable becuase if the state is NULL or UNDEF the switch will be commanded OFF instead of throwing an error. |
JRuby helper library currently already offers a It also overrides the JSScripting has |
Toggling on NULL and UNDEF is something I would not expect. The toggle scope is limited to ON -> OFF and OFF -> ON. That the suggestions above are wrong is imho the only reason to standardize the behavior: |
What's a practical example of when a script would issue a It is usually called by a momentary button press: press it to turn it on, and press it again to turn it off, and press it again to turn it back on. In this instance, physical light is already on, openhab just loaded, the item state is still NULL, so you press the button, it would send ON command, nothing happens (because it's already on). So user presses the button again, but this time because the item state would've already been initialised (either by item autoupdate, or by sending the command, the item would poll the current state), so the subsequent press turns it off. Now if you instead throw an error, the item state will remain NULL, and the button is not "working" and you'll have to wait until the item state gets updated by the binding. |
I get why you would choose the pragmatic approach. |
To be fair in this use case
This rule will suffer same problems as toggle method |
I have a physical button that only clicks - it does not stay in on state, just send and one time event with on and then goes off. |
"most of the time" implies that the command is unreliable: sometimes it works, sometimes it doesn't. The implementation I described would only "not toggle the actual device" during the initial load and only if the current state is unknown. If the state is known prior to the call, it would behave the same way, with the same reliability of toggling every time too. The only difference is that it handles an unexpected state in a pragmatic way, so that user need not code additional workarounds in most cases. Should they insist on not toggling when the state is unknown, simply add a check for that
As a result, when you press the button, nothing happens, the light stays ON (which is the same behaviour anyway). And even if you pressed the button 10 more times, as long as the device state remains unknown, no "toggling" would happen. That's far from "I expect it to toggle every time". So which is worse? Either implementation still allows the user to achieve either behaviour, with or without some additional code. At the end of the day it's a design decision. Getting back to this PR though: it would introduce another issue
|
I just found that JS rules also has toggle option: https://github.com/openhab/openhab-js/blob/main/src/items/items.js#L341 |
The
Control question: You always assume that the target state should be on.
Correct |
You need to know current state to send |
Nothing suggested here eliminates the edge cases. They all still exist no matter the approach. The design decision should be to handle the most common edge cases without imposing extra work on the end user. I think the use case where the TOGGLE command is expected to send a command to the device every time is the most common so I would choose it as the default (i.e. send ON command if the Item is UNDEF/NULL). If I press my garage door opener or a momentary button to toggle a light, if nothing happens, I'll press it again. Wanting the TOGGLE command to do nothing or error if the Item is NULL/UNDEF is IMO the less common use case. However, just because it is less common doesn't mean that an approach should be chosen that precludes handling that use case. And I would even go so far as to say that handling it should be no more involved than the if statement oneliners we are trying to replace with this issue (i.e. setting up a proxy Item to intercept the TOGGLE command in a rule is significantly more involved than the if statements we have now). Given that jRuby, JS Scripting, and HABApp all support toggle shows there is a demand for this capability (I don't think the JS Scripting capability is in the docs though). But it is worth ensuring we don't impose more work on a subset of users than they have now. |
Hey,
Writing DSL functions I found out that it would be really nice to be able to toggle types to opposite, i.e. You got command
ON
from item x and you want to send commandOFF
(opposite command) to item y:vs
WDYT?