Lua scripts as action in scenes

Here is the API description for the Lua scripts as actions in scenes.
With the runCustomScript block, it is possible to execute custom scripts directly from the scene.

For example:

{
   "blockOptions":{
      "method":{
         "name":"runCustomScript",
         "args":{
            "script":"script"
         }
      }
   },
   "blockType":"then",
   "fields":[
      {
         "name":"script",
         "type":"scriptId",
         "value":"5cf0dc8c7f000068d223e88a"
      }
   ]
}

Commands:
hub.scenes.scripts.add
hub.scenes.scripts.set
hub.scenes.scripts.get
hub.scenes.scripts.delete
hub.scenes.scripts.list
hub.scenes.scripts.run
hub.scenes.delete
hub.scenes.edit

Can be executed:

  • as a direct API request with the script - will be executed immediately
  • as a scene with the script which can be typed and saved from UI and run by some trigger event
  • as a manually running from the app scene that contains script as action block.

Here is the example of the code which will turn on all devices which have "switch " items.
As a direct API request:

{
  "id": "_ID_",
  "jsonrpc": "2.0",
  "method": "hub.scenes.scripts.add",
  "params": {
    "name": "Switch on all lights",
    "code": "require \"core\"\n local items = core.get_items()\n if items then \n \n for _, item in ipairs( items ) do\n   if item.value_type == \"bool\" and item.has_setter then\n       core.set_item_value( item.id, true )\n   end\n  end\n end"
  }
}

As a scene with the script which can be typed and saved from UI:

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

Script_As_Action.pdf (67.0 KB)

1 Like

Who can translate this in to something that is understandable?

We all know somewhat how to use and create lua scripts on the Vera platform, there are many script snippets and examples on the forum that we can understand and expand upon.

But he might as well be talking Dutch here. I know I’m not the sharpest tool in the toolbox, but what was just stated?

How do I turn on a particular device via a script?

How do you turn it off?

How do I dim / brighten a device to a percentage?

How do I run a scene via a script?

I’m going to post on here some of my existing Vera lua scripts from my scenes.

And I’d like to see translated examples on the Ezlo platform.

Is it just me? Or have they made these new Ezlo APIs and scripts far too complex?

Vera Luup Requests and in turn its related lua scripts was simple and easy to understand for simple people like me LOL.

Translate this simple script to blink a light to an Ezlo script.

device = 291 -- Blink Globe Lamp
count = 4 -- number of times to blink
rate = 2 -- number of seconds between blinks

function blink_on()
    luup.call_action( "urn:upnp-org:serviceId:SwitchPower1", "SetTarget", { newTargetValue="1" }, device )
    luup.call_delay( 'blink_off', rate )
end
function blink_off()
    luup.call_action( "urn:upnp-org:serviceId:SwitchPower1", "SetTarget", { newTargetValue="0" }, device )
    count = count - 1
    if count > 0 then
        luup.call_delay( 'blink_on', rate )
    end
end

-- Start blinking
blink_on()
2 Likes

Or how about this even more simple script that toggles a lamp?

local device=379
local switchOnOff = luup.variable_get("urn:upnp-org:serviceId:SwitchPower1", "Status", device)
    if (switchOnOff == "1") then
      -- Switch is on
luup.call_action("urn:upnp-org:serviceId:Dimming1", "SetLoadLevelTarget", {newLoadlevelTarget = "0"}, device)
else 
luup.call_action("urn:upnp-org:serviceId:Dimming1", "SetLoadLevelTarget", {newLoadlevelTarget = "100"}, device)
end

@melih

I’m quite sure your developers are extremely talented and know what they are doing.

However your users who will be buying and using this system are struggling to follow.

But I’ve said and thought this and battled with them for some time now to add back in some simplicity.

I had to shout and protest to get them to add the “feature” "to turn off complex token authentication against the local http server API.

It’s on my LAN!

If my router is compromised I have bigger things to worry about than the local API access to my Ezlo hub.

Vera Luup Request was functional and powerful and could be integrated with virtually anything because it was simple.

Every and all previous 3rd party integration done with Vera out there on the Internet to date are going to die on the new Ezlo platform.

From rich folks who used Vera hubs as Z-Wave gateways with their fancy and expensive Control4 systems to open source systems like Home Assistant that also used Vera hubs for the very same purpose.

To the many 3rd party dashboard control apps and the many other types of integrations which I can’t even think about or remember right now.

The Ezlo APIs are overly complex and the Lua scripting I don’t know where to even start with it.

I know things have to change and for reasons I might not understand.

But I had to sit down and try to translate for many days and work out for myself pretty much and I am no coder or developer…
How to use the new “user” facing http server API in my idiots guide I wrote and I still don’t know how to do simple variable value get requests dispite asking more than numerous occasions on the forum.

It’s all well and good them writing high level documentation but the pdf attached here means nothing to me, it shows me nothing for real world lua script usage.

I urge all Vera users to provide sample lua scripts from their existing Vera scenes and we’d like to see working examples of Ezlo equivalent Lua scripts in simple step by step terms.

2 Likes

Never mind GET I don’t even know how to SET a devices particular variable via http code.

I can’t even see the devices variables in the Vera mobile app as we have no UI7 Web GUI replacement that use to display them.

Luup Requests worked and was simple.

Seems you are just adding barriers to entry and complexity for what advantage?

http://wiki.micasaverde.com/index.php/Luup_Requests#device

Let’s ask a simple question?

I have a virtual motion sensor device on the Ezlo hub.

I read one of CW’s guides on the forum to work out how to create such a virtual device.

Now…

How do I set that virtual motion sensor to tripped via a simple one line http command from a 3rd party application such as Blue Iris?

1 Like

OK I do really know how to set variables on the Ezlo API, but how many others do know how to do that? From official documentation?

GET variables however is still a mystery…

I’ve only seen Rene do it once before a 3rd party dev.

Still my point stands its far too complex and breaks all previous 3rd party integrations with the existing platform.

And Ezlo maybe a new island but its losing traction by killing all previous 3rd party integrations with Vera from all other HA systems and apps out there that currently integrate with Vera.

Wanting Blue Iris to control a virtual motion sensor on an Ezlo hub is a very simple integration from a 3rd party application.

But not so simple by the fact of these new complex APIs. And not so simple to even create that virtual sensor in the first place.

…

How will Ezlo integrate with Harmony hubs as far as the very limited official Logitech “Home Control” aspect of the Harmony remotes is concerned?

Vera had no direct supported previously.

But guess what?

A 3rd party Java HA Bridge software did support Vera via its simple http luup requests.

Said Java HA Bridge acts as an emulated Hue Bridge which Logitech Harmony “Home Control” does support and can see.

So you linked your Harmony software to to HA Bridge / emulated Hue Bridge and now all your Vera devices and scenes you added on to the Java HA Bridge are exposed to the Harmony remote.

What is Harmony “Home Control”?

It’s the little buttons on their remotes that can be assigned to control smart home devices. And also on the Harmony Elite LCD screen there is access and control to all these devices.

This integration with Harmony is now not possible with an Ezlo hub.

And official Logitech Harmony “Home Control” is only the tip of a very big iceberg.

When compared to full and proper integration with Vera via the current 3rd party plugin for Harmony by Rene.

It’s features are extremely powerful and even expose a full http server Api via Vera to communicate with IR devices on the Harmony hub.

This allowing even more none related 3rd party apps to be able to send simple http requests to Vera to then relay those commands on to the Harmony hub to control those IR devices either at just the Harmony Activity level on / off etc or even down to per IR device per button command level.

Let’s set my IR only AVRs audio mode for example from Yatse at 3rd party control app for Kodi all relayed through and via Vera.

The levels of 3rd party integrations you are breaking I’m not sure you even understood they existed…

Reactor Multi System logic engine is well timed.

Im going to be moving all my complex PLEG rules off the Vera box and on to the Raspberry Pi running Multi System Reactor.

Least then I’ll maybe have some options in the near future to move to other systems like Home Assistant with a Z-Wave USB stick or Hubitat…

3 Likes

I totally agree. The userfriendliness is below zero and the learning curve seems very steep :frowning_face:

Dev story is still too early to really follow it. You can’t compare 10+ years with less than 1 week of scene scripting.

I still think an editor with auto completion could make a huge difference.
Something based on VS Code could be a huge improvement. Some link:

I agree that luup engine APIs are not bad at all and maybe a compatibility layer built on the new APIs could have been better than starting from scratch, but it is what it is now, and you have to adapt (or leave for a complete different platform, in fact).

2 Likes

Come on guys, a lot of complaining and no reading of Oleh’s post. I have limited coding skills, however reading the code example I read the following 2 lines of code to turn on a device from a Lua script:

require "core"
core.set_item_value( item.id, true )

Same, but now turning of the device:

require "core"
core.set_item_value( item.id, false )

item.id would be the device id.

This code looks a lot more elegant and readable than the Luup.call_action alternative!

Ok it can be done albeit it is cumbersome. The code to toggle a switch is:

require "core" 
local itemId = "5f6210c4124c4110f359e042"
local item =core.get_item(itemId)
core.set_item_value(itemId, not( item.value))

So first step is to use the API tool to get the device id and then the item id for the item with name switch.
Now create the scene and add a Lua script for the action. Carefully type over the item id value.

If you need to edit the script. It is hidden under Settings, Lua Scripts. You cannot edit the script in the scene editor.

This especially makes me want a web GUI again. It is a pain to do anything but trivial on a tablet, and I will not even go here on a phone.

Have fun .

Cheers Rene

3 Likes

In my Vera Lite I could run lua right in the scene action. Now I don’t even see a way forward with this.

This isn’t working for me.

@reneboer example of a Toggle does work however.

I just found some more examples here and this one does work to turn on a “Switch”

require "core"
local items = core.get_items()
if items then
    for _, item in ipairs(items) do
        if item.name == "switch" and item.device_id == "<Device-ID-Here>" then
            core.set_item_value(item.id, true)
        end
    end
end

You have to use the device ID not the item ID of the switch component of that device.

There is one in your examples that use the device name instead of the device ID. I use it in a sceen to turn on my garage light when motion is detected by a Zooz motin sensor.

Does anybody can help me to write a script where you will get a current level from a dimmer in to a variable and than pass it to a http request?

Hello @JSNB,
You can use the code below to get the current level of a dimmer:

require "core"
local current_level = core.get_item("item_id").value

You will need to use the API to find the ID of the item with the name “dimmer”. This item is responsible for the intensity of the dimmer.
Kind regards!

Oscar

Is there an equivalent command using the HTTP server API? To retrieve a devices items value?

E.g. To send a http command to the Ezlo Controller to return the desired devices current value. Say for a temperature sensor.

Thanks.

Hello @cw-kid,
As far as I know, there is no command or API call that is equivalent to the core.get_item() function. However, it is possible to get the value of an item with HTTP requests by filtering the response. You can use the function grep for example but I recommend using jq to parse the response and get the desired value. It would be something like this:

curl --http1.1 "http://IP:17000/v1/method/hub.items.list" | jq '.result.items | .[] | select(._id=="ITEM_ID") | .value'

You would only need to replace the values IP and ITEM_ID. By the way, I used unsecured and unauthenticated access just to make the access easier. I hope that helps.
Kind regards!

1 Like

I know nothing about Lua or scripts. I did just upgrade to a Ezlo secure. on my vera edge, I was able to use a plugin (timer) to turn off a Device after a set amount of time, can I do that with ezlo? if so how? I also had a plugin called day or night which I could set outdoor light by. canU dither of these?
please help
thank you

A related thread here about running an Ezlo scene only when it’s “night”.

Unless Ezlo have improved this functionality? It does not work and seems to me they made a complete mess of it and it’s not easily possible.

None of the old Vera plugins can be used on the new Ezlo hub.

I think you will need to create a virtual switch and have a scene(s) to change it to on / off depending on if its after sunrise or after sunset and then use that virtual switch and its current state in your other scenes logic.

I will test this later what they previously said and show that the Ezlo staff were talking nonesense in that other thread I linked to.

And they think they can create a Reactor alternative? When they can’t even do a simple scene with a condition of “night”.

They don’t even have conditions or they don’t call them that at least, they say its a “Group of Events”.

An event as far as I can see it, in their Vera app is a trigger as it’s under the “Activation” heading.

It not a condition?

The whole Vera app work flow is a mess for scene logic.