Save dimmer value

Hello,

i try to save my dimmer value before i switch off.

after i switch my light back on i will have the same dimmer value as before.

rule "Wohnzimmerschalter"
when
	Item Wohnzimmerlicht changed 
then
	if (Wohnzimmerlicht.state == OFF)
	{
		var Number altWohnzimmerlicht = WohnzimmerBrightness.state
		WohnzimmerBrightness.sendCommand("0")
	}else
	{
		WohnzimmerBrightness.sendCommand(altWohnzimmerlicht)
	}
end

i got this error

20:52:05.969 [ERROR] [untime.internal.engine.RuleEngineImpl] - Rule 'Wohnzimmerschalter': The name 'altWohnzimmerlicht' cannot be resolved to an item or type; line 626, column 36, length 18
20:52:05.970 [ERROR] [untime.internal.engine.RuleEngineImpl] - Rule 'Wohnzimmerschalter': The name 'altWohnzimmerlicht' cannot be resolved to an item or type; line 626, column 36, length 18

hi hannes,
i think you have to declare the variable outside of the function, because when light changes to off the function is called and parameter is saved. but wenn light changes on the function is called again and then no value is there (because we are in the “else”-condition only)

var altWohnzimmerlicht = 0

rule "Wohnzimmerschalter"
when
	Item Wohnzimmerlicht changed 
then
	if (Wohnzimmerlicht.state == OFF)
	{
		altWohnzimmerlicht = (WohnzimmerBrightness.state as Number).intValue
		WohnzimmerBrightness.sendCommand("0")
	}else
	{
		WohnzimmerBrightness.sendCommand(altWohnzimmerlicht)
	}
end

but normally when switching off a light with “OFF” instead of “0” then the last state might be restored when switching light “ON” again - even if it is a dimmer. at least for me that works.

thank you.

it works.

now i will try to set the brightness to 100% after i switch the lightswitch two times in 3 seconds.