Telegram - Setting Up Remote Control - Lua Functions
Introduction
Once you have set up the Telegram Remote Control Foundations, then you can extend by adding your own Lua functions into recieve.lua - Telegram - Setting Up Remote Control - Foundations
Built in Lua Functions
The scripts you downloaded on the Telegram - Setting Up Remote Control - Foundations page come with a fairly well featured receive.lua, which has a number of powerful functions built-in with an extensible framework which is described in the next section.
Telegrams Commands: Help - will list all available commands implemented both via Lua and Bash. Help command - will give usage instructions for that command i.e. Help On
devices - will return a numbered list of all devices on your Domoticz system devices sub-string - will return a numbered list of all devices on your Domoticz system that start with sub-string i.e. devices St
scenes - is the same as devices but for scenes and groups
On - will turn on the named or numbered device On device name - will turn on device name i.e. On Hall Light On number - will turn the device / scene / group with that number from the last devices or scenes command i.e. On 5
Battery - will return the battery level Battery device name - will return the battery level for device name i.e. Battery Attic
Adding Your Own Lua Functions
To add your own function into receive.lua and have it automatically picked up by Telegram, you need to follow this framework.
The function needs to be called the same as the command you want to type, so Battery and keep the same capitalisation, so battery would not work as the command if your function is called Battery.
The command is made visible in Telegram by adding a description to the Commands array where the key is the command name and the value is a description of the command.
Commands.Battery = "Battery - Battery Devicename - retrieves current battery level from device"
The function needs to written to expect variables in a standard order:
1st parameter - SendTo - the Telegram user name - Joe_Bloggs 2nd parameter - Command - the name of the command, this allows simple cover functions to call a more complex function, so On and Off are simple cover functions which call a more complex switch function. Also if you need to pass a command parameter to Domoticz then keeping the same name for your command may well simplify programming and extensibility later 3rd parameter - DeviceName - the Domoticz device name, group name or scene name
Not all parameters need to be used, but the order needs to be maintained.
function Battery(SendTo, command, DeviceName)
UpdateList("devices",DeviceFileName)
DeviceID = IDFromName(DeviceName, DeviceFileName)
-- Determine battery level
local handle = io.popen("curl 'http://" .. DomoticzIP .. ":" .. DomoticzPort .. "/json.htm?type=devices&rid=" .. DeviceID .. "' 2>/dev/null | /usr/local/bin/jq -r .result[].BatteryLevel")
local BattLevel = string.gsub(handle:read("*a"), "\n", "")
handle:close()
-- Determine last update
local handle = io.popen("curl 'http://" .. DomoticzIP .. ":" .. DomoticzPort .. "/json.htm?type=devices&rid=" .. DeviceID .. "' 2>/dev/null | /usr/local/bin/jq -r .result[].LastUpdate")
local LastUpdate = string.gsub(handle:read("*a"), "\n", "")
handle:close()
print(DeviceName .. ' batterylevel is ' .. BattLevel .. "%")
-- The following commented line won't work, as this script is running under Telegram not Domoticz
-- so all interaction needs to be via Json as the Domoticz Lua variable are not avaialble to you
-- difference = timedifference(otherdevices_lastupdate[DeviceName]) / 3600
send_msg(SendTo,DeviceName..' battery level was '..BattLevel..' when last seen '..LastUpdate,ok_cb,false)
end