Check activity of events using LUA script?


#1

How to check and set the variable depending on the activity of events using LUA script?
I tried both codes but the variable does not change.

version 1

local Timeout = alias['device'].alias['Timeout']
local Timeout_T = alias['device'].alias['Timeout_T']

Timeout.value = 0

while true do
if(Timeout_T.value == false) then
    Timeout.value = 10
    end
  
  if(Timeout_T.value == true) then 
     Timeout.value = 50
    end
    
end

version 2

local Timeout = alias['device'].alias['Timeout']
local Timeout_T = alias['device'].alias['Timeout_T']
    Timeout.value = 0
    
    while true do
    if(Timeout_T.active == false) then
        Timeout.value = 10
        end
      
      if(Timeout_T.active == true) then 
         Timeout.value = 50
        end
        
    end

#2

Can you print out the value of Timeout_T.value as well as its type to ensure the value is a boolean?

var tmp = Timeout_T.value
debug(type(tmp), tmp)

Tip - please format (indent) your code consistently so it doesn’t require extra cognitive burden to parse for your would-be responders.


#3

A datarule in the platform (an Event) will always be a value of 0 or 1. In Lua scripts - if you compare a 0 or 1 number value to true or false, it will always be false.

Here is a script that waits on a datarule value change.

local timeout  = alias['timeout'] -- assumes you have a datarule (event) with alias timeout.
while true do
    local ts = timeout.wait() --triggers when datarule changes state
    local event_state = timeout[ts] --gets the value at this timestamp of the event/datarule
    if event_state == 0 then
      -- do something
    else
      -- do something else, making assumption that a datarule is always 0 or 1.
     end
end

#4

Тhank you @csrl and @MikeA.
With debugging variables saw that returns 1 or to 0.
I managed with my examples, but the script constantly sending data.
But with the example of MikeА I managed to solve that problem, the data are sent only when a change of events.
Thanks Mike, helped me a lot with this example.


#5

Awesome - glad to hear this helped!