How to detect who/what activated a LUA script

Moderator: leecollings

Post Reply
lupo2a
Posts: 31
Joined: Thursday 29 September 2016 10:55
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

How to detect who/what activated a LUA script

Post by lupo2a »

Dear all,
in a Lua (device) script how can I detect if the script was activated by a user action or by a timer associated to the control?
I need it in order to adapt the behavior of the script without adding lot of "virtual switch" with dedicated timers


Version: 3.7165
Platform: Raspberry PI3
Hardware: Fibaro FGRM222 Roller Shutter Controller 2
Raspberry PI 4
Domoticz: V2023.2
- Aeotec Gen5 EU
- Neo CoolCam Door Detector and plug
- FIBARO FGWPE Wall Plug
- 6 FIBARO System FGRM222 Roller Shutter Controller 2
- Kodi SamnsungTV Yamaha plugin
- pluginn manager
- Netatmo
User avatar
bueno79
Posts: 83
Joined: Wednesday 08 March 2017 10:26
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.9700
Location: France
Contact:

Re: How to detect who/what activated a LUA script

Post by bueno79 »

hi,

it could be great to know if user A or user B activate a specific switch...very good for security purpose.

regards
georgesattali
Posts: 84
Joined: Saturday 05 March 2016 16:40
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: France
Contact:

Re: How to detect who/what activated a LUA script

Post by georgesattali »

Hello,
to test whether a script was called by a user or a timer, I would create a second script calling the one you already have (in the exemple below, named "myinitialscript.lua").

the second script is

Code: Select all

calledByUser=true
dofile("/home/pi/domoticz/scripts/lua/myinitialscript.lua")
Then, in myinitialscript.lua you can test :

Code: Select all

if calledByUser == true then
	-- code if I was called by the user
	dothis
	dothat
else
	-- code if I was called by the timer
	dothingsfortimer
end
is good?
See U, GD
lupo2a
Posts: 31
Joined: Thursday 29 September 2016 10:55
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: How to detect who/what activated a LUA script

Post by lupo2a »

Sorry but I didn't get the answer.

How can I create the first script so that it's runned only if a user activate a switch?

If I create the LUA script (Setup->More Options->events) "first_script" - LUA - DEVICE this will be run either by the user pressing the switch or by the timer associated with the switch

Within first_script I can check whether the switch has changed

Code: Select all

if devicechanged[name_of_switch] then
    calledByUser=true
    dofile("/home/pi/domoticz/scripts/lua/myinitialscript.lua")
 end
 
but this will be true for both the cases:
- user pushed the switch
- the timer associated with the switch as fired an event

What I'm missing?

Tx

Version: 3.7165
Platform: Raspberry PI3
Hardware: Fibaro FGRM222 Roller Shutter Controller 2
Raspberry PI 4
Domoticz: V2023.2
- Aeotec Gen5 EU
- Neo CoolCam Door Detector and plug
- FIBARO FGWPE Wall Plug
- 6 FIBARO System FGRM222 Roller Shutter Controller 2
- Kodi SamnsungTV Yamaha plugin
- pluginn manager
- Netatmo
gerard76
Posts: 53
Joined: Wednesday 22 March 2017 9:13
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8153
Contact:

Re: How to detect who/what activated a LUA script

Post by gerard76 »

You can create an extra (dummy) switch and call that dummy switch with the timer. Have that dummy switch switch the original switch.
Now you know the timer flipped the switch and not the user because the user flipped the switch directly (without going through the dummy switch).

If it's to detect manual override I store my scripts last action in a user variable and in the next run I compare current switch state with last program action. If they are not equal it's a manual override situation.
ronnyandre
Posts: 7
Joined: Monday 02 January 2017 10:14
Target OS: Raspberry Pi / ODroid
Domoticz version:
Location: Oslo, Norway
Contact:

Re: How to detect who/what activated a LUA script

Post by ronnyandre »

@gerard76 Can you show me how this script implements user variable?
Newbie just started with Domoticz and home automation.
gerard76
Posts: 53
Joined: Wednesday 22 March 2017 9:13
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8153
Contact:

Re: How to detect who/what activated a LUA script

Post by gerard76 »

Look at the manual_override bit at the end. If the program switches the fan on it stores this in the 'fan_program' variable. In subsequent runs I check to see if the fan state is the same as what the program told it to do. If not, it was switched manually.

Code: Select all

require('scripts/lua/time')
require('scripts/lua/air')

commandArray   = {}
measurements   = {}

-- settings:
fan             = 'WC ventilator'
light           = 'WC licht'
inside_sensor   = 'Woonkamer'
outside_sensor  = 'Buiten'
max_humidity    = 62 -- max humidity in room
max_run_time    = 10 -- in minutes
fan_light_delay = 40 -- second after light is turned on to enable fan
DEBUG           = false

--[[ user variables:

 wc_fan_program        - string  - Off
 
--]]

inside_hum   = tonumber(otherdevices_humidity[inside_sensor])
inside_temp  = tonumber(otherdevices_temperature[inside_sensor])
outside_temp = tonumber(otherdevices_temperature[outside_sensor])
outside_hum  = tonumber(otherdevices_humidity[outside_sensor])

if devicechanged[fan] or devicechanged[inside_sensor] or devicechanged[outside_sensor] or devicechanged[light] then
  fan_on           = otherdevices[fan]   == "On"
  light_on         = otherdevices[light] == "On"
  too_moist        = inside_hum > max_humidity
  useful           = air.abs_humidity(inside_temp, inside_hum) > air.abs_humidity(outside_temp, outside_hum) + 1
  on_too_long      = fan_on and time.minutes_since(otherdevices_lastupdate[fan]) > max_run_time
  on_by_light      = light_on and time.seconds_since(otherdevices_lastupdate[light]) > fan_light_delay
  program_wants_on = not on_too_long and (too_moist and useful) or on_by_light
  -- it's doing something else as the last program command AND it's not doing what the the program currently wants 
  manual_override  = (otherdevices[fan] ~= uservariables['fan_program']) and (fan_on ~= program_wants_on)
  
  if DEBUG then
    print("wc fan is " .. otherdevices[fan] .. " for " .. time.minutes_since(otherdevices_lastupdate[fan]) .. " mins")
    print("wc light is "..otherdevices[light].." for "..time.seconds_since(otherdevices_lastupdate[light]).." secs; on_by_light: " .. tostring(on_by_light))
    print("wc current hum: " .. inside_hum .. "% max_hum: " .. max_humidity .. "%")
    print("wc abs hum inside: " .. air.abs_humidity(inside_temp, inside_hum) .. "; abs hum outside: " .. air.abs_humidity(outside_temp, outside_hum))
    print("wc manual override = " .. tostring(manual_override) .. "; useful: " .. tostring(useful) .. "; on too long: " .. tostring(on_too_long))
    print("wc last program command: " .. uservariables['fan_program'] .. "; program wants on: " .. tostring(program_wants_on))
  end
  
  if not fan_on and program_wants_on and not manual_override then
    commandArray[fan] = "On"
    commandArray['Variable:fan_program'] = "On"
  elseif (fan_on and on_too_long) or (fan_on and not program_wants_on and not manual_override) then
    commandArray[fan] = "Off"
    commandArray['Variable:fan_program'] = "Off"
  end
end

return commandArray
If you do not use dzVents then
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest