The library definition provides the Hubitat Elevation hub with information about the library, which can be added to Libraries Code in the hub Developer Tools. Libraries provide a way to share code among multiple apps or drivers by defining a library with variables, methods, etc. just as they would appear in app/driver:
library ( author: "myName", category: "samples", description: "Some description", name: "SampleLibrary1", namespace: "myNamespace", documentationLink: "http://www.example.com/" ) // put methods, etc. here
namespace can include only letters (upper or lower case), numbers, dots, dashes, and underscores. It is recommended the namespace be unique to you as a developer. All name plus namespace combinations must be unique for each library on a given hub. The
description parameters are the only required parameters at this time, though the additional parameters may be used in the future.
To use a library in an app or driver, begin a line with
#include (no spaces are allowed before the
# symbol), followed by a space, the library namesapce, a period, and the library name. For example, to include the above library, write the following line in app or driver code:
When the app or driver is saved, the “included” code will be inserted behind the scenes automatically. The included code will be appended to the end of the app or driver file (regardless of where
#include appears in the app or driver). All apps or drivers using a library are recompiled when the library is changed.
You can select the download icon (downward arrow) next to Import button in the Apps Code or Drivers Code editor to see exactly how the included code appears in the final the source code. Each line in the library produces two lines in the resulting full code file: the first line is a comment with the line number in the original library, and the second is the actual line of code (note that this may affect the use of certain features such as multi-line strings). The full code may be helpful for troubleshooting errors in logs that refer to specific line numbers, etc. You can also use this feature to download a full version for distribution to others if you do not want to share the app/driver and library code separately.