Get variable value

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
mischa
Posts: 74
Joined: Tuesday 07 April 2015 20:32
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8872
Location: Heerhugowaard, The Netherlands
Contact:

Get variable value

Post by mischa »

Perhaps I missed it in the documentation.
How do I get the stored value of a user variable?

domoticz.variables('MYVARIABLE') is returning a nil value

Thanks,
Mischa
Pi 2 - Pi 1 - Razzbery 2.0 - Fibaro FGD-211 - Fibaro FGD-212 - Fibaro FGRGBWM-441 (RGBW Module) - Fibaro FGBS321 (Universal Switch) - FGWPF-102 - TBK Home TZ67-G - Synology DS1515+ - Esp Easy (with wemos D1 mini)
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Get variable value

Post by dannybloe »

Guess you missed it indeed. It’s there: domoticz.variables('varname').value. If you get nil then likely the name or id/index is not correct.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
mischa
Posts: 74
Joined: Tuesday 07 April 2015 20:32
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8872
Location: Heerhugowaard, The Netherlands
Contact:

Re: Get variable value

Post by mischa »

Is it possible to use the variable as an event trigger?

on = {
devices={domoticz.variables('MYVARIABLE').value}
},
Pi 2 - Pi 1 - Razzbery 2.0 - Fibaro FGD-211 - Fibaro FGD-212 - Fibaro FGRGBWM-441 (RGBW Module) - Fibaro FGBS321 (Universal Switch) - FGWPF-102 - TBK Home TZ67-G - Synology DS1515+ - Esp Easy (with wemos D1 mini)
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Get variable value

Post by dannybloe »

Please read the documentation. It is all in there, step by step. And the answer is yes:

Code: Select all

return {
  on = {
     variables ={'myvar'}
  },
  execute=...
}
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
mischa
Posts: 74
Joined: Tuesday 07 April 2015 20:32
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8872
Location: Heerhugowaard, The Netherlands
Contact:

Re: Get variable value

Post by mischa »

dannybloe wrote: Thursday 25 January 2018 21:21 Please read the documentation. It is all in there, step by step. And the answer is yes:
I think my question was not clear enough.

I store the Idx of my lux sensor in a variable, so the variable would represent the Idx in devices={Idx} this way if I want to change the lux sensor used
don't have to mess with my scripts. And theoretically, everybody (aka the wife) can change this variable value easily.
Pi 2 - Pi 1 - Razzbery 2.0 - Fibaro FGD-211 - Fibaro FGD-212 - Fibaro FGRGBWM-441 (RGBW Module) - Fibaro FGBS321 (Universal Switch) - FGWPF-102 - TBK Home TZ67-G - Synology DS1515+ - Esp Easy (with wemos D1 mini)
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Get variable value

Post by waaren »

Would it satisfy your needs if you trigger your script on any of your lux sensors with something like

Code: Select all

return {
on = { devices = { '*lux*' }   }, -- All your lux devices have the string lux as part of their names. Alternative is to use a list of all your lux device names

    execute = function(domoticz, lux)
        if domoticz.variables('ActiveLuxName').value == lux.name then  -- Name of Active lux in string var ActiveLuxName
            print("This the lux I set. Better do some stuff")
        else
            print("This is not my active Lux") 
        end
    end
}
or if you prefer the idx approach

Code: Select all

return {
on = { devices = { '*lux*' }   },

    execute = function(domoticz, lux)
        if domoticz.variables('ActiveLuxIndex').value == lux.idx then   -- Index of Active lux in integer var ActiveLuxIndex
            print("This the lux I set. Better do some stuff")
        else
            print("This is not my active Lux") 
        end
    end
}


Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
dannybloe
Posts: 1355
Joined: Friday 29 August 2014 11:26
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Ermelo
Contact:

Re: Get variable value

Post by dannybloe »

Ah, I understand what you mean now. The only thing you can do is what waaren already proposes and that is creating a very general trigger rule using wild-cards and to the checking of the variable in your script. But I really wonder if this the best solution for this problem you try to solve (using variables). How many times does this change and do you want the performance penalty for having to check this variable for almost every script trigger that is generated by Domoticz? (wild-cards are more expensive than just a name or id). I find it just as easy to edit the script using the GUI editor and alter the name of the device in the trigger section. You can declare the name of the trigger at the top of the script as a local constant and use that in your on-section. It's almost trivial to edit the name when it is changed.
Creator dzVents - RPi3, loads of zwave devices, esp8266, evohome.
mischa
Posts: 74
Joined: Tuesday 07 April 2015 20:32
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8872
Location: Heerhugowaard, The Netherlands
Contact:

Re: Get variable value

Post by mischa »

@waaren thank you for your solution.
Danny is also right about the performance, the best solution would be then to use the idx or devicename.

Build-in a safety trigger with the isNightTime

Code: Select all

return {
    active = true,
    on = {	
    		devices = {24},
    		timer = {'at sunset'} 
    	 },
    execute = function(domoticz, device)
    
    if (domoticz.variables('DarkOutside').value == 'false' and
    	(domoticz.devices(24).lux < 10 or domoticz.time.isNightTime))
    then
    	domoticz.scenes('Lights Outside ON').switchOn()
    	domoticz.variables('DarkOutside').set('true')
      
    end
  end
  }
Pi 2 - Pi 1 - Razzbery 2.0 - Fibaro FGD-211 - Fibaro FGD-212 - Fibaro FGRGBWM-441 (RGBW Module) - Fibaro FGBS321 (Universal Switch) - FGWPF-102 - TBK Home TZ67-G - Synology DS1515+ - Esp Easy (with wemos D1 mini)
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest