Backup/save the value/state of a switch

Moderator: leecollings

Post Reply
bldewit
Posts: 61
Joined: Tuesday 09 May 2017 23:31
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: NL
Contact:

Backup/save the value/state of a switch

Post by bldewit »

Hi,
I want to save the value/state of a switch to a dummy switch when flipping another switch, so that I can reset it to that value later. I have tried to simply state SET <backupswitch> = <primaryswitch>, but that didn't work.

So I created a new blocky triggered by one switch and checking the value of the primary, then based on that setting the backup. Since that didn't work either I added a "fake" check for time, resulting in the following blocky:
[img]
blocky bckp.jpg
blocky bckp.jpg (23.61 KiB) Viewed 1448 times
[/img]

Can anyone see the reason why this blocky does not work? (I have obviously activated it... :D and remember that I also tried this without the time-check )

Thanx!
-‐----------------------------------------------------------
Running on rPi 3b (main) and rPi 2b (somfy), both RFLink
IthoRemote, Xiaomi Gateway, KAKU
ben53252642
Posts: 543
Joined: Saturday 02 July 2016 5:17
Target OS: Linux
Domoticz version: Beta
Contact:

Re: Backup/save the value/state of a switch

Post by ben53252642 »

The way I do this is to save the state to a variable using Lua:

See below:

Code: Select all

commandArray = {}
if (devicechanged['DEVICENAME'] == 'On') then
commandArray['Variable:DishwasherState'] = 'inuse'
end
return commandArray
The way you access the variable state is like this:

Code: Select all

commandArray = {}
if (uservariables["DishwasherState"] == 'notinuse') then
WHAT YOU WANT TO DO
end
return commandArray
This is my entire script for the dishwasher which uses a variable to store the state you mention:

Code: Select all

commandArray = {}

 -- Change Watt Device value to number and remove the word Watt
wattdevice = otherdevices_svalues['Dishwasher Watts']
function stripchars(str, chrs)
   local s = str:gsub("["..chrs.."]", '')
   return s end
wattvalue = stripchars( wattdevice, " Watt" )

if (uservariables["DishwasherState"] == 'notinuse' and tonumber(wattvalue) >= 3 ) then
commandArray['Variable:DishwasherState'] = 'inuse'
os.execute ('sh /root/domoticz/speak.sh "speaker3 Washing the dishes."')
end

if (uservariables["DishwasherState"] == 'inuse' and tonumber(wattvalue) <= 2 ) then
commandArray['Variable:DishwasherState'] = 'notinuse'
os.execute ('sh /root/domoticz/speak.sh "speaker3 The dishwasher has finished."')
commandArray['SendNotification']='Hawknet#The dishwasher has finished.'
end

return commandArray
Change the IP to your IP: http://192.168.0.5/#/UserVariables

The above URL is where you can set uservariables in Domoticz.

In the above example the variable type is "String", you can set it on the uservariables page.

Hope these examples help.
Unless otherwise stated, all my code is released under GPL 3 license: https://www.gnu.org/licenses/gpl-3.0.en.html
bldewit
Posts: 61
Joined: Tuesday 09 May 2017 23:31
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: NL
Contact:

Re: Backup/save the value/state of a switch

Post by bldewit »

Looks like a sound suggestion. Adding some nested if's could just do the job. I'll be checking this out these next days, thx!
Just to increase my understanding: What is the trigger for the bottom script in your answer?
-‐----------------------------------------------------------
Running on rPi 3b (main) and rPi 2b (somfy), both RFLink
IthoRemote, Xiaomi Gateway, KAKU
bldewit
Posts: 61
Joined: Tuesday 09 May 2017 23:31
Target OS: Raspberry Pi / ODroid
Domoticz version: Stable
Location: NL
Contact:

Re: Backup/save the value/state of a switch

Post by bldewit »

Ok, so thanks to your suggestion I have been able to solve my problem while actually learning something about Lua scripts, thx! I didn't use a uservariable as you did, but another dummy switch.

I have found that using Lua actually gives you more grip on which switches of events are actuaally used as a trigger and which are just used for reference.
-‐----------------------------------------------------------
Running on rPi 3b (main) and rPi 2b (somfy), both RFLink
IthoRemote, Xiaomi Gateway, KAKU
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest