You can now use Lua code in Ezlo scenes - How to Examples

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
2 Likes

Hi @MCakan,

I think your example of code in the app screen shot is for use in the the API tool and not how to enter it in the app it self. That is at least how I did it.

Cheers Rene

Thank you Rene, You are right. I updated it :+1:

1 Like

Can you please also give examples how to turn on/off one light or set temperature of one thermostat with id xy?

1 Like

Sure, we will also keep adding new examples to this post.

what kind of Lua code are you putting in your scenes?
would love to hear your ideas and how you are using this capability in your scenes please.

I would like examples of the equivalent to luup.variable_watch and not having it run in a scene, more like the Startup Lua on Vera.
My use case is watching humidity sensors in bathrooms/showers, comparing to other sensors in the house and control ventilation fans according.

hi
would you expand on this please. What does it do exactly I personally haven’t used that function on vera.
Is it a “Trigger” or “Action”?

It’s a function. Whenever a variable for specified device changes it calls another function, in my case my function that compare the sensors and regulate fan speed if needed.

1 Like

Yep, was just reading that…its a callback function if certain criteria is met…checking with the guys…

1 Like

Hi @Crille,

This type of functionality would more need a plugin than code in a scene. A plugin you can subscribe to events and you can set a filer for the device you are interested in. Then change a setting in another zwave device may not yet be supported in Lua as each plugin runs in its own space (called gateway) and you cannot control a device of another gateway. We have asked to allow it, but I have not yet heard it can be done. How to create your first plugin with Ezlo LUA API

Cheers Rene

I suspected the subscribe to events function but writing a plugin seems a bit overkill for this purpose and overwhelming for me.
I know it’s not suitable for a scene and maybe the only way to mimic Startup Lua is a plugin.
If I find the time, perhaps I should start digging in that direction to be prepared when we can interact between gateways, thanks for the link!

Hello @reneboer,
all provided examples change items of any gateway and you can use it in your plugins.
Ezlo Firmware provides:
core.update_item_value() - it can update items of your plugin only. It updates value in firmware database.

core.set_item_value() - it can update any item of any gateway. This call redirects to the plugin which is responsible for the target item.

Hello @Crille,
It depends how you want to see it and control. It’s possible to do via Lua in scenes if control part is enough. If you also want to see some states then better to do it via own plugin as @reneboer advised.
Could you describe expected flow for your case?

Hi @andryist,

Aha, i was trying to understand what the API documentation about the set_item_value. On it self it does not state it can work on any item of any gateway. I guess it is implied by the section “Methods for using in external gateways” i now know what that heading means. And i think that I understand what you mean with internal and external gateways. Is external also on a different hub, or is it only the gateways(plugins) on one hub?

Cheers Rene

Only on the same controller now.

Best Home Automation shopping experience. Shop at getvera!

© 2020 Vera Control Ltd., All Rights Reserved. Terms of Use | Privacy Policy | Forum Rules