Expression and Activities (Run Lua) Functions

Would there be any guidance on the functions available for expressions and activities via ‘Run Lua’? Tested obtaining a string number value via the following method under expression:

getstate(52, "urn:micasaverde-com:serviceId:SecuritySensor1", "Tripped")

This works fine, so then I wanted to forego a variable declaration and use this directly within Activities via ‘Run Lua’:

if (getstate(52, "urn:micasaverde-com:serviceId:SecuritySensor1", "Tripped") == "1") then
	luup.call_action('urn:micasaverde-com:serviceId:ZWaveNetwork1','SendData',{Node='12',Data='112 4 3 1 2'},1)
else
	luup.call_action('urn:micasaverde-com:serviceId:ZWaveNetwork1','SendData',{Node='12',Data='112 4 3 1 3'},1)
end

Well, I get the classic invalid / issue icon for reactor :confused: Do I actually have to use a variable (and how can I reference it in Run Lua) or can I call this (getstate) directly in some manner within Run Lua? Appreciate it!

The syntax for expressions in the “Expressions” tab is not Lua. It may be Lua-like in some respects, but it is not Lua (it’s also JavaScript-like, Python-like, C-like, etc. in many respects).

What works in a “Run Lua” action in an activity is pure Lua, plus the Luup functions found under the global luup object (such as luup.variable_get( serviceId, variableName, deviceNumber ) ). There are also some Reactor extensions, found in the global Reactor object available (only) inside “Run Lua” activity code. If the variable name (for example) into which you are receiving the tripped state of your sensor is sensor_state, then you can access that expression result directly by referencing Reactor.variables.sensor_state in your “Run Lua” code.

if Reactor.variables.sensor_state == "1" then
	luup.call_action('urn:micasaverde-com:serviceId:ZWaveNetwork1','SendData',{Node='12',Data='112 4 3 1 2'},1)
else
	luup.call_action('urn:micasaverde-com:serviceId:ZWaveNetwork1','SendData',{Node='12',Data='112 4 3 1 3'},1)
end

More here: https://www.toggledbits.com/static/reactor/docs/Run-Lua-Action/

And here: https://www.toggledbits.com/static/reactor/docs/Expressions-&-Variables/

1 Like

Does the expression/variable builder support ternary operators? For instance, checking a value if it is equal to ‘1’ then set the variable to string ‘occupancy’ otherwise set the variable to string ‘vacancy’. I then want to export this variable and reference in another reactor sensor. Would I access an exported variable just the same, via Reactor.variables.{name}? Can we use the same variable name in local and exported sensors with normal rules for scoping where a local variable would take precedence?

Use the if() function:

if( something == "1", "one", "not one" )

If you export the variable to use in a condition in a different RS, you have to use a Device State condition, specify the RS that owns the variable/expression, and choose the variable.

The Reactor.variables.<name> syntax is only available in Run Lua actions, and only in the same RS in which the variable/expression is defined.

You cannot directly use a variable defined in one RS in an action on a different RS. You would need to use define a “local” variable (in the RS where you want to use it) and use a getstate() condition to retrieve the value of the exported variable from the other RS, creating a local copy of the value.

I can’t work out what you mean here, but with regard to scope, an expression/variable’s scope is limited to the ReactorSensor in which it is defined. Exporting it only causes a state variable copy of the value to exposed on the ReactorSensor, so it can be seen by other devices in the system (including other ReactorSensors).

1 Like

Think that answers all of the questions I had! I did see that the variable/expression builder can reference the exported variable on another device. So this should all work now and not give me radioactive symbols, lol. In terms of the last point, I wasnt sure how you were exporting the variables - to the parent Reactor sensor or to each created reactor device individually. Since everything is segmented and uses normal lua/referencing between devices, this question doesnt make sense. If you were exporting the variable directly to the master sensor, I was wondering if the child sensors can use those variables directly, and if a locally created variable from trump a global variable… not valid in this case/implementation.

Really nice extensible work on this module. You have practically created a working API for ‘true’ Vera automation that should just replace Scenes. Thanks again.

1 Like

I hadn’t really considered the possibility of doing “global” expressions/variables on the master device becoming likely, but it seems to keep coming up in various forms. The master device has a long list of ideas in my notes; I think it’s under-utilized as a coordinating element. More to think about…

1 Like

Best Home Automation shopping experience. Shop at getvera!

© 2020 Vera Control Ltd., All Rights Reserved. Terms of Use | Privacy Policy | Forum Rules