Page 1 of 1

Get variable value

Posted: Thursday 25 January 2018 18:05
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

Re: Get variable value

Posted: Thursday 25 January 2018 18:19
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.

Re: Get variable value

Posted: Thursday 25 January 2018 21:02
by mischa
Is it possible to use the variable as an event trigger?

on = {
devices={domoticz.variables('MYVARIABLE').value}
},

Re: Get variable value

Posted: Thursday 25 January 2018 21:21
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=...
}

Re: Get variable value

Posted: Thursday 25 January 2018 21:37
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.

Re: Get variable value

Posted: Friday 26 January 2018 1:20
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
}



Re: Get variable value

Posted: Friday 26 January 2018 7:40
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.

Re: Get variable value

Posted: Friday 26 January 2018 17:02
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
  }