Hello everyone,
As you know you can add lua codes in scenes as actions. Here you can see how if you have not tried that yet
You can add your sample lua codes by going to Settings → LUA Scripts → and writing the code with a name. This name will appear in scenes, when you want to add a LUA Script as an action.
You can do all sorts of powerful actions that you might require with adding lua code to your scenes. However if you have not yet tried this powerful tool below you can see some basic examples of codes you can copy paste and try right away:
Turn on all devices that have a basic on/off switch:
require "core"
local items = core.get_items()
if items then
for _, item in ipairs( items ) do
if item.value_type == "bool" and item.has_setter then
core.set_item_value( item.id, true )
end
end
end
Turn off all devices that have a basic on/off switch:
require "core"
local items = core.get_items()
if items then
for _, item in ipairs( items ) do
if item.value_type == "bool" and item.has_setter then
core.set_item_value( item.id, false )
end
end
end
Turn on all lights :
require "core"
local items = core.get_items()
if items then
for _, item in ipairs( items ) do
if item.name == "switch" then
local device = core.get_device( item.device_id )
if device and ( device.category == "dimmable_light" or ( device.category == "switch" and ( device.subcategory == "interior_plugin" or device.subcategory == "exterior_plugin" or device.subcategory == "in_wall" ) ) ) then
core.set_item_value( item.id, true )
end
end
end
end
Turn off all lights :
require "core"
local items = core.get_items()
if items then
for _, item in ipairs( items ) do
if item.name == "switch" then
local device = core.get_device( item.device_id )
if device and ( device.category == "dimmable_light" or ( device.category == "switch" and ( device.subcategory == "interior_plugin" or device.subcategory == "exterior_plugin" or device.subcategory == "in_wall" ) ) ) then
core.set_item_value( item.id, false )
end
end
end
end
Turn on one light with a known ID :
For this to work you just need to change ID_OF_DEVICE to the id of the device you want to turn on.
require "core"
local items = core.get_items()
if items then
for _, item in ipairs(items) do
if item.name == "switch" and item.device_id == "<ID_OF_DEVICE>" then
core.set_item_value(item.id, true)
end
end
end
Turn off one light with a known ID :
For this to work you just need to change ID_OF_DEVICE to the id of the device you want to turn off.
require "core"
local items = core.get_items()
if items then
for _, item in ipairs(items) do
if item.name == "switch" and item.device_id == "<ID_OF_DEVICE>" then
core.set_item_value(item.id, false)
end
end
end
Turn on one light by the name :
For this to work you just need to change NAME_OF_DEVICE to the the unique name of the device you want to turn on.
require "core"
local items = core.get_items()
if items then
for _, item in ipairs(items) do
if item.name == "switch" then
local device = core.get_device(item.device_id)
if device and device.name == "NAME_OF_DEVICE" then
core.set_item_value(item.id, true)
end
end
end
end
So lets assume we have in the app one lamp with the name Bedroom Lamp. So the lua code will be as shown below if we want to turn it on with this code.
require "core"
local items = core.get_items()
if items then
for _, item in ipairs(items) do
if item.name == "switch" then
local device = core.get_device(item.device_id)
if device and device.name == "Bedroom Lamp" then
core.set_item_value(item.id, true)
end
end
end
end
Turn off one light by the name :
For this to work you just need to change NAME_OF_DEVICE to the the unique name of the device you want to turn off.
require "core"
local items = core.get_items()
if items then
for _, item in ipairs(items) do
if item.name == "switch" then
local device = core.get_device(item.device_id)
if device and device.name == "NAME_OF_DEVICE" then
core.set_item_value(item.id, false)
end
end
end
end
Set the temperature for all thermostats to 72F :
Please remember the temperature value you use here is in the same unit type that you set your hub (Fahrenheit / Celcius)
require "core"
local items = core.get_items()
if items then
for _, item in ipairs( items ) do
if item.name == "thermostat_setpoint" and item.has_setter then
core.set_item_value( item.id, 72 )
end
end
end
Set the temperature of one thermostats with a known ID to 72F :
For this to work you just need to change ID_OF_DEVICE to the id of the thermostat you want to set.
Please remember the temperature value you use here is in the same unit type that you set your hub (Fahrenheit / Celcius)
require "core"
local items = core.get_items()
if items then
for _, item in ipairs( items ) do
if item.name == "thermostat_setpoint" and item.has_setter and item.device_id == "<ID_OF_DEVICE>" then
core.set_item_value( item.id, 72 )
end
end
end
Set the temperature of one thermostats to 72F by the name of it :
For this to work you just need to change NAME_OF_DEVICE to the the unique name of the thermostat you want to turn set.
Please remember the temperature value you use here is in the same unit type that you set your hub (Fahrenheit / Celcius)
require "core"
local items = core.get_items()
if items then
for _, item in ipairs( items ) do
if item.name == "thermostat_setpoint" and item.has_setter then
local device = core.get_device(item.device_id)
if device and device.name == "NAME_OF_DEVICE" then
core.set_item_value( item.id, 72 )
end
end
end