Lua otherdevices help

Moderator: leecollings

Post Reply
atatistcheff
Posts: 17
Joined: Sunday 19 June 2016 18:16
Target OS: Linux
Domoticz version: 3.8153
Contact:

Lua otherdevices help

Post by atatistcheff »

I am having some issues with consistency in my Lua scripting. What would really help is a reference to the commands available. I've looked at a bunch of the documentation and examples but can't figure out why my script doesn't work for some devices. To get the status of a device I'm using the commands below:

Code: Select all

local GarageDistance = 'Miata Garage Distance'
local FireplaceWatts = 'Fireplace Light Watts'
local OfficeDesk = 'Office Desk Lamps'

commandArray = {}
print('***** script_device_garage.lua executing *****')

print('Miata Garage Bay Distance =',otherdevices[GarageDistance])
print('Fireplace Light Watts =',otherdevices[FireplaceWatts])
print('Office Desk Lamps =',otherdevices[OfficeDesk])

if (devicechanged['Miata Garage Bay Status'] == 'Door Open' ) then
   print(' - Miata Garage Bay Door Open')
elseif
   (devicechanged['Miata Garage Bay Status'] == 'Door Closed' ) then
   print(' - Miata Garage Bay Door Closed')
end
The problem is that only the 'Office Desk Lamps' device actually returns any data which tells whether the switch is On or Off. The Miata Garage Distance is a distance sensor that returns a number in centimeters and the Fireplace Light Watts is a Z-Wave switch with kWh reporting capability. Both of these display data in Domoticz but in the Lua script the values are empty. I suspect I need some other command besides "otherdevices" to get this information for something that is not a simple switch but I don't know what command/function to use to get it.

Any help would be appreciated!
trixwood

Re: Lua otherdevices help

Post by trixwood »

This is a common beginners problem.

In programming languages there is a difference between, text and number, there is even a difference between numbers (floats, the one with the point in it, and whole without point), we call them calls them types (of a variable). The most commen mistake is not to convert between types.

The computer does not know which type you want unless you specify, for the computer the raw data 48656c6c6f the string 'Hello' and the number 310939249775 are the same. For it its all 0100 1000 0110 0101 0110 1100 0110 1100 0110 1111...* it is up to you to tell it which one to use, and you did not do that, you tried to print two floats (e.g. 1.326, 4.243 are floats which have numbers after the point and 1, 2, 3 are whole numbers) without telling it to convert to string. And this programming language is not going to stop you or warn you, its not designed to do that. Switch to C and it will yell at you that you can not print floats and refused to run... it a feature called type-checking and has it's own drawbacks...


To convert between types in Lua use the following functions (see documentation):

tonumber()
tostring()

Thus...

print('Miata Garage Bay Distance = ' .. tostring(otherdevices[GarageDistance]))
print('Fireplace Light Watts = ' .. tostring(otherdevices[FireplaceWatts]))

Lua manual: https://www.lua.org/manual/5.3/

* yeah i know little indian and big indian... pfff.... it is an example :mrgreen:
atatistcheff
Posts: 17
Joined: Sunday 19 June 2016 18:16
Target OS: Linux
Domoticz version: 3.8153
Contact:

Re: Lua otherdevices help

Post by atatistcheff »

First of all thank you very much for your detailed reply. This is great info that I am sure I can use in my scripting. However, in this case the issue was that the otherdevices command/function was not returning anything at all for these devices. I did some more digging and discovered the correct function is otherdevices_svalues. Using this I am now getting values for all of those non-switch devices. So the script below returns just what I was looking for.

Code: Select all

local GarageDistance = 'Miata Garage Distance'
local GarageStatus = 'Miata Garage Bay Status'
local FireplaceWatts = 'Fireplace Light Watts'
local OfficeDesk = 'Office Desk Lamps'

commandArray = {}
print('***** script_device_garage.lua executing *****')
 

print('Miata Garage Bay Status =' .. otherdevices_svalues[GarageStatus])
print('Miata Garage Bay Distance =' .. tostring(otherdevices_svalues[GarageDistance]))
print('Fireplace Light Watts =' .. tostring(otherdevices_svalues[FireplaceWatts]))
print('Office Desk Lamps =' .. otherdevices[OfficeDesk])

if (devicechanged['Miata Garage Bay Status'] == 'Door Open' ) then
   print(' - Miata Garage Bay Door Open')
elseif
   (devicechanged['Miata Garage Bay Status'] == 'Door Closed' ) then
   print(' - Miata Garage Bay Door Closed')
end
Thanks again for the assistance!
trixwood

Re: Lua otherdevices help

Post by trixwood »

I can't sleep and I always get a bit over-explanation-wise after too many hours without it... also I was not completely correct, or complete,... lua does auto-convert between types, but not always... ow well :-)
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest