Never loose the config

To make my life easier, i added the /etc/openhab2/ folder as network share which allows me to directly edit it from my favourite editors from my laptop and backup it .

As i want to add a more complicated rules using one of my EnOcean switches i based my new rules on https://community.openhab.org/t/design-pattern-switch-dimmer/66893

First i created the mySwitch.item in the /etc/openhab2/items folder. I will use the channel B for my switch as channel a is directly linked to blinds controller.

Adding the rules and a bit of renaming i see quite fast the switch have been added and all of them have new virtual channels.

rule "TurnStuffOff"
 when
     Item BedRoomSwitchBDownShortPress changed from ON to OFF
 then
     SchlafzimmerNachttisch_Switch.sendCommand(OFF)
     SchlafzimmerLampe_Brightness.sendCommand(new PercentType(0))
     SchlafzimmerLampe_Brightness.sendCommand(new PercentType(0))
     SchlafzimmerStrip_Color.sendCommand(new PercentType(0))
 end
rule "TurnStuffOn"
 when
     Item BedRoomSwitchBUpShortPress changed from ON to OFF
 then
     SchlafzimmerNachttisch_Switch.sendCommand(ON)
     SchlafzimmerLampe_Brightness.sendCommand(new PercentType(70))
     SchlafzimmerStrip_Color.sendCommand(new PercentType(70))
 end

Next step is to see if we get the dimming to work as i get only one event for long press i guess i need to start a timer and stop it as soon as i get the stop event.

Using the Timer Pattern https://community.openhab.org/t/why-have-my-rules-stopped-running-why-thread-sleep-is-a-bad-idea/47695 a dim down/up works very fast. What i dislike it seems to be flickering…

And after more investigating i figured out, the “long press” does actually resends every x ms a decrease or increase command, so it was already doing some dimming, but it was not the one i want to have.

Also the documentation is not very helpfull all the time. Using google and the forum you can find the information you want but not all the time so fast. Espeially strange seems to be that “boolean” is not working which i find very confusing…
I will also change my whole handling to not use the virtual Switch types but only the Rocker Channel directly. Depending on what has been pressed (up or down) i will do the different dimming action. This should compact the size.
What i currently have is suboptimal but at least working

import java.util.Map

var Timer timer = null
var stopDimming = true
var lastPercentage = new PercentType(70)
val Map<String, Timer> Timers = newHashMap
val Map<String, DateTime> Timestamps = newHashMap

val SwitchPressed = [ Map<String, Timer> Timers, Map<String, DateTime> Timestamps, String id |
	Timestamps.put(id, now)
	sendCommand(id, "ON")

	// Start timer if switch is held down
	val offset = now.plusMillis(500)
	Timers.put(id, createTimer(offset, [|
		// Handle long press
		sendCommand(id + "LongPress", "ON")
	]))
]

val SwitchReleased = [ Map<String, Timer> Timers, Map<String, DateTime> Timestamps, String id |
	// Deactivate the timer (if running)
	Timers.get(id)?.cancel()
	Timers.remove(id)

	// Get timestamp of switch pressed down
	var pressed = Timestamps.get(id)
	Timestamps.remove(id)

	// Handle short press
	val offset = pressed?.plusMillis(500)
	if (now.isBefore(offset)) {
		sendCommand(id + "ShortPress", "ON")
		sendCommand(id + "ShortPress", "OFF")
	}
	else
	{
		sendCommand(id + "LongPress", "OFF")
	}

	sendCommand(id, "OFF")
]

rule "Switch1Events"
when
	Channel "enocean:rockerSwitch:FT3ODAA:FEFFFF1F:rockerswitchB" triggered
then
	switch (receivedEvent.event) {
		case "DIR1_PRESSED"  : { SwitchPressed.apply(Timers, Timestamps, "BedRoomSwitchBUp") }
		case "DIR1_RELEASED"  : { SwitchReleased.apply(Timers, Timestamps, "BedRoomSwitchBUp") }
		case "DIR2_PRESSED"  : { SwitchPressed.apply(Timers, Timestamps, "BedRoomSwitchBDown") }
		case "DIR2_RELEASED"  : { SwitchReleased.apply(Timers, Timestamps, "BedRoomSwitchBDown") }
	}
end

rule "TurnStuffOff"
when
	Item BedRoomSwitchBDownShortPress changed from ON to OFF
then
	stopDimming = true
	lastPercentage = SchlafzimmerLampe_Brightness.state
	SchlafzimmerNachttisch_Switch.sendCommand(OFF)
	SchlafzimmerLampe_Brightness.sendCommand(new PercentType(0))
	SchlafzimmerStrip_Color.sendCommand(new PercentType(0))
end

rule "TurnStuffOn"
when
	Item BedRoomSwitchBUpShortPress changed from ON to OFF
then
	stopDimming = true
	SchlafzimmerNachttisch_Switch.sendCommand(ON)
	SchlafzimmerLampe_Brightness.sendCommand(lastPercentage)
	SchlafzimmerStrip_Color.sendCommand(lastPercentage)
end

rule "DimStuffDown"
when
	Item BedRoomSwitchBDownLongPress changed from OFF to ON
then
	stopDimming = "A"
	timer = createTimer(now.plusMillis(250),[|
	
	var percent = (SchlafzimmerLampe_Brightness.state as DecimalType).intValue
	percent = percent - 1
	if(percent <= 0)
	{
		stopDimming = "YES"
	}
	SchlafzimmerLampe_Brightness.sendCommand(percent)
	SchlafzimmerStrip_Color.sendCommand(percent)

	if(stopDimming != "A") timer = null
	else {
			timer.reschedule(now.plusMillis(250))
		}
	])
end

rule "DimStuffDownEnd"
when
	Item BedRoomSwitchBDownLongPress changed from ON to OFF
then
	stopDimming = true
	if(timer !== null)
		timer.cancel()
end


rule "DimStuffUp"
when
	Item BedRoomSwitchBUpLongPress changed from OFF to ON
then
	stopDimming = "A"
	timer = createTimer(now.plusMillis(250),[|
	var percent = (SchlafzimmerLampe_Brightness.state as DecimalType).intValue
	percent = percent + 1
	if(percent == 100)
	{
		stopDimming = true
		timer = null
	}
	SchlafzimmerLampe_Brightness.sendCommand(percent)
	SchlafzimmerStrip_Color.sendCommand(percent)
	if(stopDimming != "A" ) timer = null
	else {
			timer.reschedule(now.plusMillis(100))
		}
	])
end

rule "DimStuffUpEnd"
when
	Item BedRoomSwitchBUpLongPress changed from ON to OFF
then
	stopDimming = true
	if(timer !== null)
		timer.cancel()
end

0 thoughts on “Never loose the config

Leave a Reply

Your email address will not be published. Required fields are marked *