How to change a switch "unknown" state to OFF?

I do use the (switch) Item “Taster_Wohnen_Schlafen_Eingang_Taster_Wohnen_Schlafen_Eingang” which has its OFF-state “unknown”. I would like to change “unknown” to “OFF” but do not know how can I do that.

I also use following script which triggers mentioned item when it goes to “ON”. I thought I could change the rule to set the item with the postupdate when the rule does not triggers but it is not working like this.

I tried also tho use for the switch an “autoupdate” but it was not working for the item.

I do have following script:

var Timer tTaste = null

rule “TasterRule_WohnenSchlafen”

when
Item Taster_Wohnen_Schlafen_Eingang_Taster_Wohnen_Schlafen_Eingang changed
then
tTaste?.cancel

logInfo("Taster WohnenSchlafen", "Tastendruck erkannt")

if(newState == ON) 
{
    tTaste = createTimer(now.plusNanos(1200000000),[|
        logInfo("Taster WohnenSchlafen", "langer Tastendruck erkannt")
        Taster_WohnenSchlafen_LongPress.sendCommand(ON)	
		postUpdate(Taster_WohnenSchlafen_LongPress, OFF)
    ])
} 
else 
{
    if(tTaste.isCancelled) {
        logInfo("Taster WohnenSchlafen", "kurzer Tastendruck erkannt")
        Taster_WohnenSchlafen_ShortPress.sendCommand(ON)
		postUpdate(Taster_WohnenSchlafen_ShortPress, OFF)
    }
	else
	{
		postUpdate(Taster_WohnenSchlafen_LongPress, OFF)
		postUpdate(Taster_WohnenSchlafen_ShortPress, OFF)
	}
}

end

Any ideas?

Thanks

You can always use karaf console to set initial state for the item:

openhab:send Taster_Wohnen_Schlafen_Eingang_Taster_Wohnen_Schlafen_Eingang OFF
openhab:send Taster_Wohnen_Schlafen_Eingang_Taster_Wohnen_Schlafen_Eingang REFRESH

Insufficient information :wink:
Which addon do you use?

If Item Taster_Wohnen_Schlafen_Eingang_Taster_Wohnen_Schlafen_Eingang is of type Switch, the state is one of these: ON, OFF, NULL or maybe UNDEF, but I’m pretty sure it never will be unknown as this state is non existent for Switch Items.

Why on earth did you duplicate the item name to this monstrosity?

Why do you need the state to be OFF?

Please don’t use the action postUpdate(Item as String, State as String) but better use the method Item.postUpdate(State).
The only (at least valid) reason to use the action would be if you got the items name as a result of a calculation in a string variable.

Which information are missing? I am using OH4.1 (windows) and no specific addon for this topic.

I updated the rule according your input to this:

var Timer tTaste = null

rule "TasterRule_WohnenSchlafen"

when 
    Item Taster_Wohnen_Schlafen_Eingang_Taster_Wohnen_Schlafen_Eingang changed 
then
    tTaste?.cancel
	
	logInfo("Taster WohnenSchlafen", "Tastendruck erkannt")
	
    if(newState == ON) 
	{
        tTaste = createTimer(now.plusNanos(1200000000),[|
            logInfo("Taster WohnenSchlafen", "langer Tastendruck erkannt")
            Taster_WohnenSchlafen_LongPress.sendCommand(ON)	
			Taster_WohnenSchlafen_LongPress.postUpdate(OFF)
        ])
    } 
	else 
	{
        if(tTaste.isCancelled) {
            logInfo("Taster WohnenSchlafen", "kurzer Tastendruck erkannt")
            Taster_WohnenSchlafen_ShortPress.sendCommand(ON)
			Taster_WohnenSchlafen_ShortPress.postUpdate(OFF)
        }
		else
		{
			Taster_WohnenSchlafen_ShortPress.postUpdate(OFF)
			Taster_WohnenSchlafen_LongPress.postUpdate(OFF)
		}
    }
end

I need to have the state “OFF” in order to use this state information in other rules for example. Although the state “UNDEF” is possible, it sounds not correct to me because a switch should be “ON” or “OFF”.

Anyway, the item is still “UNDEF” with the updated rule. I thought the rule will change it to “OFF” when the rule do not trigger. Do I miss somehting?

The rule will not change Item Taster_Wohnen_Schlafen_Eingang_Taster_Wohnen_Schlafen_Eingang at all, and it’s a bad idea to even try, as the rule is triggered by the change. And even more… The rule depends on external changes…

Well, the addon. This is really important, as under normal circumstances, you won’t have an item for a real wall push button.
Instead, nowadays most addons provide a trigger channel for that purpose.
The push button has no “state” as it will always be OFF, unless it’s pressed, which is an event, so no need to get this information via Item. The trigger event most likely will be something like PRESSED, SHORT_PRESS, DOUBLE_PRESS, LONG_PRESS, RELEASED… THis is dependent on the addon for sure.

If I got it right, you want to build this information from the Button by yourself. Then you’ll have to use hardware which does not only provide ON when pressed, but also OFF when released.

I think there is a missunderstanding for my use case. I do use this rule to get a short/long press of the “Taster” Taster_Wohnen_Schlafen_Eingang_Taster_Wohnen_Schlafen_Eingang. When the rule triggers it shall set the item Taster_WohnenSchlafen_LongPress switch to “ON” and in case of not using this item (or not trigger occurs) it shall be “OFF”.

I thought to do this the rule could be a good place. Is there another possability to set this item to “OFF” when not used?

I do not understand which addon can be used in my case or I need to use to accomplish that use case.

Thanks for the support

So it’s not about setting Taster_Wohnen_Schlafen_Eingang_Taster_Wohnen_Schlafen_Eingang to OFF, but Taster_WohnenSchlafen_LongPress or Taster_WohnenSchlafen_ShortPress?
But when should the Item set to OFF?
Maybe you want a short ON pulse, then automatic OFF again?
Easiest way to do so would be to use the expiration timer in Item metadata.

Maybe you want a short ON pulse, then automatic OFF again?

Yes correct. When the rule triggers Taster_Wohnen_Schlafen_Eingang_Taster_Wohnen_Schlafen_Eingang goes to “ON” for a short or long press then the item Taster_WohnenSchlafen_LongPress or Taster_WohnenSchlafen_ShortPress shall automatically go from “ON” to “OFF” again.

When I use the expiration timer I have two options: “Update state” or “send command”. Which one shall I use?

That’s up to the external circumstances :slight_smile: Do you need the OFF as a command (e.g. a rule which shall trigger, a channel which shall send the command to hardware)? Then use sendCommand.
If it’s only about “reset the switch item”, use postUpdate.

Perfect and understood - now it works as expected! Thanks for the help…

1 Like