update device status with LUA Topic is solved

Moderator: leecollings

Post Reply
TheWoodenGamer
Posts: 7
Joined: Sunday 29 January 2017 22:03
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

update device status with LUA

Post by TheWoodenGamer »

Hi,

I can't seem to update a device status with LUA script.

Situation
- I have a user variable 'var-security'
- A device named 'Alarm' setup as a 'selector' with two levels:
10: AAN
20: UIT

I try to run a LUA script to update the device status to AAN or UIT when the user variable 'var-security' is updated from 1 to 0 or the other way around.

Code: Select all

commandArray = {}

 if ( uservariables['var-security'] == '1' ) then
  commandArray ['Alarm'] = 'Set Level: 10'
elseif ( uservariables['var-security'] == '0' ) then
    commandArray ['Alarm'] = 'Set Level: 20'
end

return commandArray
Unfortunately, the status does not change.
Using commandArray ['Alarm'] = 'AAN' or 'On' or '1' does not work either.

Any tips or solutions for this?
A completely new code is welcome too.

Thanks,

Roel
User avatar
G3rard
Posts: 669
Joined: Wednesday 04 March 2015 22:15
Target OS: -
Domoticz version: No
Location: The Netherlands
Contact:

Re: update device status with LUA

Post by G3rard »

Is your script a file in the LUA scripts folder? In that case the name should be like script_variable_name.lua.

Or is your script in the local editor in Domoticz? In that case you should select UserVariable.
Not using Domoticz anymore
User avatar
jvdz
Posts: 2333
Joined: Tuesday 30 December 2014 19:25
Target OS: Raspberry Pi / ODroid
Domoticz version: 4.107
Location: Netherlands
Contact:

Re: update device status with LUA

Post by jvdz »

Think you need to set the value to 10 or 20 like:

Code: Select all

commandArray ['Alarm'] = '10' 
Jos
New Garbage collection scripts: https://github.com/jvanderzande/GarbageCalendar
TheWoodenGamer
Posts: 7
Joined: Sunday 29 January 2017 22:03
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: update device status with LUA

Post by TheWoodenGamer »

G3rard wrote:Is your script a file in the LUA scripts folder? In that case the name should be like script_variable_name.lua.

Or is your script in the local editor in Domoticz? In that case you should select UserVariable.
I use the web editor. Do you mean to replace 'uservariables' with 'UserVariable'?
That seems wrong, but I tried it anyway. Does not work.

thanks
TheWoodenGamer
Posts: 7
Joined: Sunday 29 January 2017 22:03
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: update device status with LUA

Post by TheWoodenGamer »

jvdz wrote:Think you need to set the value to 10 or 20 like:

Code: Select all

commandArray ['Alarm'] = '10' 
Jos

Hi Jos

Tried that. Does not work :(

Thanks
User avatar
G3rard
Posts: 669
Joined: Wednesday 04 March 2015 22:15
Target OS: -
Domoticz version: No
Location: The Netherlands
Contact:

Re: update device status with LUA

Post by G3rard »

TheWoodenGamer wrote: I use the web editor. Do you mean to replace 'uservariables' with 'UserVariable'?
That seems wrong, but I tried it anyway. Does not work.

thanks
I mean the pulldown menu just Above the tick box Event active, that should be set to UserVariable for this script.

So no changes to the LUA code :)
Not using Domoticz anymore
User avatar
Westcott
Posts: 423
Joined: Tuesday 09 December 2014 17:04
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: UK - Glos
Contact:

Re: update device status with LUA

Post by Westcott »

What you want is -
commandArray ['Alarm'] = 'Set Level 20'
without the ':'
Zwave - Sigma Z+ stick, Fibaro, Horstmann, Neo Coolcam, EUROtronic
RFlink - IR detectors and temperatures
Wifi - YeeLights, ESP32s, Anoop sockets
Zigbee - lots with zigbee2mqtt and ZbBridge
TheWoodenGamer
Posts: 7
Joined: Sunday 29 January 2017 22:03
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: update device status with LUA

Post by TheWoodenGamer »

Westcott wrote:What you want is -
commandArray ['Alarm'] = 'Set Level 20'
without the ':'
Hi Westcott,

No, that does not matter. This is however not as documented.
Documentation says that "Set Level xx" is for dimmers and for selectors, you should use "Set Level: xx"
It seems that both work fine.

The issue was that my variable is an integer, but I used '1' in my script. Once I removed the quotes, my script went into the functions.
And as you say, then without ':' it sets the level correctly.

Thank you all for thinking with me!

Cheers,

Roel
TheWoodenGamer
Posts: 7
Joined: Sunday 29 January 2017 22:03
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: update device status with LUA

Post by TheWoodenGamer »

Final script that worked:

Code: Select all

commandArray = {}

-- print to log
print("script running: alarm");

 if (uservariables['var-security'] == 1 ) then
    
    -- set device level to 10
    commandArray ['Alarm'] = 'Set Level: 10'

  -- print to log
  print ("Alarm Turned On");
  
elseif (uservariables['var-security'] == 0 ) then

    -- set device level to 20
    commandArray ['Alarm'] = 'Set Level: 20'
    
    -- print to log
    print("Alarm Turned Off");
end

return commandArray
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest