Page 1 of 1

turn a light to "Last state"?

Posted: Monday 18 July 2016 8:41
by Evelen
Hi.

Somtimes I want to change the status of a lamp, f.eks set the light to blue 100% dim for 1 sec, then change it back (as a notification, facebook ect)

eks:
- Color is red 24% dim
- facebook notification = set color blue 100% dim for 1sec
- Set Color back to red 24% dim
or:
- light is off
- facebook notification = set color blue 100% dim for 1sec
- light off


Is this posible? as easy as possible.
I only use Blockly until now, but maybe LUA must be used here?

Re: turn a light to "Last state"?

Posted: Wednesday 20 July 2016 6:49
by Egregius
First store the current value in a variable or other cache system.
Set the new color and then read the variable again to set the old one.

Re: turn a light to "Last state"?

Posted: Thursday 21 July 2016 8:20
by Evelen
It sounds logical, but how?

Re: turn a light to "Last state"?

Posted: Thursday 21 July 2016 14:01
by toreandre
Evelen wrote:It sounds logical, but how?
Search for "store variable lua domoticz", i searched for this some time ago and found some examples og storing values with LUA.

Re: turn a light to "Last state"?

Posted: Thursday 21 July 2016 21:39
by jvdz
I use this function to temporary change a switch to something and then have it return to its previous state:

Code: Select all


commandArray = {}

-- Set lights on for a certain time and then revert back to the original state
function TempSwitchChange(DeviceName, Command, Level, TimeSecs)
	local dstatus = otherdevices[DeviceName]
	local dvalue = otherdevices_svalues[DeviceName]
	print("!###  TempSwitchChange DeviceName:" ..DeviceName .."   Command:" .. Command .." Level:" ..Level .." TimeSecs:" .. TimeSecs)
	print("                   Current status:" ..dstatus .."   dvalue:" .. dvalue)
	if dstatus == nil then
		-- invalid switch
		print(" ==>  Invalid switchname " .. DeviceName .. "  (" .. ScriptName .. ")")
	elseif (dstatus == Command and dstatus == "Set Level" and dvalue == Level) then
		-- no change so don't do anything
		print(" ==>  Nothing to change for " .. DeviceName .. "  (" .. ScriptName .. ")")
	elseif (dstatus == Command and dstatus ~= "Set Level") then
		-- no change so don't do anything
		print(" ==>  Nothing to change for " .. DeviceName .. "  (" .. ScriptName .. ")")
	else
		--
		if Command == "Set Level" then
			-- Set future status thorugh LUA
			print(" ==> Set " .. DeviceName .. " to:" .. Command .. " " .. Level .. "   inx=" .. #commandArray + 1 .. "  (" .. ScriptName .. ")")
			commandArray[#commandArray + 1] = {[DeviceName] = Command .. " " .. Level}
		else
			print(" ==> Set " .. DeviceName .. " to:" .. Command .. "   inx=" .. #commandArray + 1 .. "  (" .. ScriptName .. ")")
			commandArray[#commandArray + 1] = {[DeviceName] = Command}
		end
		if dstatus == "Set Level" then
			-- Set future status thorugh LUA
			print(" ==> Set " .. DeviceName .. " to:" .. dstatus .. " " .. dvalue .. " AFTER " .. tostring(TimeSecs).. "   inx=" .. #commandArray + 1 .. "  (" .. ScriptName .. ")")
			commandArray[#commandArray + 1] = {[DeviceName] = dstatus .. " " .. dvalue .. " AFTER " .. tostring(TimeSecs) }
		else
			print(" ==> Set " .. DeviceName .. " to:" .. dstatus .. " AFTER " .. tostring(TimeSecs).. "   inx=" .. #commandArray + 1 .. "  (" .. ScriptName .. ")")
			commandArray[#commandArray + 1] = {[DeviceName] = dstatus .. " AFTER " .. tostring(TimeSecs) }
		end
	end
	return
end

-- set lamp to 100 % for 5 minutes and than back to its original value
TempSwitchChange("LAMP","Set Level", "100","300")
-- set lamp on for 5 minutes and than back to its original state
TempSwitchChange("Lamp2","On", "","300")
return commandArray
Jos

Re: turn a light to "Last state"?

Posted: Thursday 21 July 2016 23:46
by Evelen
thanks jvdz.
where in the script should I put in information?

Re: turn a light to "Last state"?

Posted: Friday 22 July 2016 16:55
by jvdz
Have you scrolled all the way down in the code and seen the examples I put in?

Jos

Re: turn a light to "Last state"?

Posted: Saturday 23 July 2016 0:09
by G3rard
jvdz wrote:I use this function to temporary change a switch to something and then have it return to its previous state:

Code: Select all


commandArray = {}

-- Set lights on for a certain time and then revert back to the original state
function TempSwitchChange(DeviceName, Command, Level, TimeSecs)
	local dstatus = otherdevices[DeviceName]
	local dvalue = otherdevices_svalues[DeviceName]
	print("!###  TempSwitchChange DeviceName:" ..DeviceName .."   Command:" .. Command .." Level:" ..Level .." TimeSecs:" .. TimeSecs)
	print("                   Current status:" ..dstatus .."   dvalue:" .. dvalue)
	if dstatus == nil then
		-- invalid switch
		print(" ==>  Invalid switchname " .. DeviceName .. "  (" .. ScriptName .. ")")
	elseif (dstatus == Command and dstatus == "Set Level" and dvalue == Level) then
		-- no change so don't do anything
		print(" ==>  Nothing to change for " .. DeviceName .. "  (" .. ScriptName .. ")")
	elseif (dstatus == Command and dstatus ~= "Set Level") then
		-- no change so don't do anything
		print(" ==>  Nothing to change for " .. DeviceName .. "  (" .. ScriptName .. ")")
	else
		--
		if Command == "Set Level" then
			-- Set future status thorugh LUA
			print(" ==> Set " .. DeviceName .. " to:" .. Command .. " " .. Level .. "   inx=" .. #commandArray + 1 .. "  (" .. ScriptName .. ")")
			commandArray[#commandArray + 1] = {[DeviceName] = Command .. " " .. Level}
		else
			print(" ==> Set " .. DeviceName .. " to:" .. Command .. "   inx=" .. #commandArray + 1 .. "  (" .. ScriptName .. ")")
			commandArray[#commandArray + 1] = {[DeviceName] = Command}
		end
		if dstatus == "Set Level" then
			-- Set future status thorugh LUA
			print(" ==> Set " .. DeviceName .. " to:" .. dstatus .. " " .. dvalue .. " AFTER " .. tostring(TimeSecs).. "   inx=" .. #commandArray + 1 .. "  (" .. ScriptName .. ")")
			commandArray[#commandArray + 1] = {[DeviceName] = dstatus .. " " .. dvalue .. " AFTER " .. tostring(TimeSecs) }
		else
			print(" ==> Set " .. DeviceName .. " to:" .. dstatus .. " AFTER " .. tostring(TimeSecs).. "   inx=" .. #commandArray + 1 .. "  (" .. ScriptName .. ")")
			commandArray[#commandArray + 1] = {[DeviceName] = dstatus .. " AFTER " .. tostring(TimeSecs) }
		end
	end
	return
end

-- set lamp to 100 % for 5 minutes and than back to its original value
TempSwitchChange("LAMP","Set Level", "100","300")
-- set lamp on for 5 minutes and than back to its original state
TempSwitchChange("Lamp2","On", "","300")
return commandArray
Jos
Thanks for sharing this script, very handy :D
I only get an error because ScriptName is not defined, so I added

Code: Select all

local ScriptName = "TempSwitchChange"