We have moved at community.getvera.com

Author Topic: Verabridge command queuing idea  (Read 1062 times)

Offline rafale77

  • Community Beta
  • Hero Member
  • ******
  • Posts: 1749
  • Karma: +101/-27
  • HA ≠ IoT as a blue sky is cloudless.
Verabridge command queuing idea
« on: January 15, 2019, 12:13:42 pm »
Hi AK,

I posted in the general section a question on command queueing assuming that the vera was doing some and came to realize after thinking further about it, that it probably isn't managing any command queue unlike most other controllers. I could workaround it using scenes but I have come to wonder if this could be done within the verabridge which would be more elegant and could allow multiple scenes to not cause a zwave overload.
My idea would be to only allow for a fixed number of verabridge device commands and wait for that device to give any feedback before sending the next one essentially keeping say 8 or 10 commands to the vera active and waiting for feedback at a time. Thoughts?
openLuup (79 devices, 141 scenes, 19 apps) master to VeraPlus (142 zwave nodes, 8 Zigbee nodes, 221 devices,  20 scenes , 2 apps) +  Hubitat (15 Zigbee nodes) + Home-Assistant (API Integrations). Bridged to Siri and Alexa. Homewave. VeraPlus ExtRooted and mios server independent.

Offline akbooer

  • Moderator
  • Master Member
  • *****
  • Posts: 6387
  • Karma: +292/-70
  • "Less is more"
Re: Verabridge command queuing idea
« Reply #1 on: January 16, 2019, 11:22:11 am »
I posted in the general section a question on command queueing assuming that the vera was doing

Ah, you're trying to get openLuup to do the ZWave queuing that Vera should be doing, but isn't?

Quote
...wait for that device to give any feedback before sending the next one

So therein lies the problem.  I'm not sure what could be used as 'reliable feedback', and so then we get into our own issues with queuing and timeouts and repeated commands, ... ?
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P, 9x Philips Hue,
Razberry, MySensors Arduino, HomeWave, AltUI, AltHue, DataYours, Grafana, openLuup, ZWay, ZeroBrane Studio.

Offline rafale77

  • Community Beta
  • Hero Member
  • ******
  • Posts: 1749
  • Karma: +101/-27
  • HA ≠ IoT as a blue sky is cloudless.
Re: Verabridge command queuing idea
« Reply #2 on: January 16, 2019, 11:58:32 am »
I know it is not easy and I have been thinking about it. Maybe one thing we could do is to insert a minimum delay between two commands of say 200-500ms so we don't have to deal with the feedback? Maybe it would only concern devices with a device id>10000 and a parent device number of 1? Maybe the command could be parsed so the delay does not apply to arm/disarm calls?

Yes essentially I am trying to create a command queue which vera isn't doing. See this post here as well http://forum.micasaverde.com/index.php/topic,118898.0.html which is interesting as well. I am not sure whether it is now truly a zwave problem or whether it is the vera Luup API layer crashing from taking on too many requests at a time.
openLuup (79 devices, 141 scenes, 19 apps) master to VeraPlus (142 zwave nodes, 8 Zigbee nodes, 221 devices,  20 scenes , 2 apps) +  Hubitat (15 Zigbee nodes) + Home-Assistant (API Integrations). Bridged to Siri and Alexa. Homewave. VeraPlus ExtRooted and mios server independent.

Offline akbooer

  • Moderator
  • Master Member
  • *****
  • Posts: 6387
  • Karma: +292/-70
  • "Less is more"
Re: Verabridge command queuing idea
« Reply #3 on: January 16, 2019, 12:22:57 pm »
Maybe one thing we could do is to insert a minimum delay between two commands of say 200-500ms so we don't have to deal with the feedback? Maybe it would only concern devices with a device id>10000 and a parent device number of 1? Maybe the command could be parsed so the delay does not apply to arm/disarm calls?

The idea of inserting delays in a 'real-time' system in order to get it to work is a bit of an anathema, however, I'd have to say that it wouldn't be the first time I'd done this (as a last resort.)

Implemented in VeraBridge, it would, by definition, only apply to remote devices, and I do like the idea of doing it only for native Zwave devices. Do you have a reliable test for the problem it's trying to solve?
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P, 9x Philips Hue,
Razberry, MySensors Arduino, HomeWave, AltUI, AltHue, DataYours, Grafana, openLuup, ZWay, ZeroBrane Studio.

Offline rafale77

  • Community Beta
  • Hero Member
  • ******
  • Posts: 1749
  • Karma: +101/-27
  • HA ≠ IoT as a blue sky is cloudless.
Re: Verabridge command queuing idea
« Reply #4 on: January 16, 2019, 12:31:56 pm »
Yes I do. I have a particular scene which sends a total of 6 zigbee and 20 zwave commands within the scene (scene obviously is on openLuup). When I run this scene, I have a high probability of causing a Luup reload and I get an error 137 in my vera log. This probability is increased to 100% if while the scene is still running I try to poll the vera using homewave mobile app and send another command (ie, unlock a deadbolt). For now I created a delay of 20s so that the scene sends 10 and 16 commands but the 16 commands still occasionally give me trouble.
openLuup (79 devices, 141 scenes, 19 apps) master to VeraPlus (142 zwave nodes, 8 Zigbee nodes, 221 devices,  20 scenes , 2 apps) +  Hubitat (15 Zigbee nodes) + Home-Assistant (API Integrations). Bridged to Siri and Alexa. Homewave. VeraPlus ExtRooted and mios server independent.

Offline akbooer

  • Moderator
  • Master Member
  • *****
  • Posts: 6387
  • Karma: +292/-70
  • "Less is more"
Re: Verabridge command queuing idea
« Reply #5 on: January 16, 2019, 01:08:29 pm »
The easiest way to test this at first would simply be to add a delay to any action request sent by the bridge.

In the following routine, make this adjustment...

Code: [Select]
local function generic_action (serviceId, name)
    ...

    local url = table.concat (request, '&')
    luup.sleep (200)    -- ADD THIS LINE
    wget (url)
    return 4,0
  end

...can't believe I'm actually recommending you try this !!
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P, 9x Philips Hue,
Razberry, MySensors Arduino, HomeWave, AltUI, AltHue, DataYours, Grafana, openLuup, ZWay, ZeroBrane Studio.

Offline akbooer

  • Moderator
  • Master Member
  • *****
  • Posts: 6387
  • Karma: +292/-70
  • "Less is more"
Re: Verabridge command queuing idea
« Reply #6 on: January 16, 2019, 04:18:58 pm »
It turns out that there's a much more subtle way to let the scheduler queue the requests (and later, if necessary, delay them.)

You might try, as an alternative, this change:

Code: [Select]
--
local function generic_action (serviceId, name)
  ...
 
  return {job = job}    -- was {run = job}, now {job = job}, queues for execution rather than runs immediately.
end

I would be very interested in the result from both of these tests.  If this latter one is not as successful, then it can be finessed.  The huge advantage here is that the processor is free to do other things whilst the request is queued, and there is total control over the minimum delay between requests (or, indeed, whether they are delayed at all.)
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P, 9x Philips Hue,
Razberry, MySensors Arduino, HomeWave, AltUI, AltHue, DataYours, Grafana, openLuup, ZWay, ZeroBrane Studio.

Offline rafale77

  • Community Beta
  • Hero Member
  • ******
  • Posts: 1749
  • Karma: +101/-27
  • HA ≠ IoT as a blue sky is cloudless.
Re: Verabridge command queuing idea
« Reply #7 on: January 16, 2019, 05:13:16 pm »
Thank you. I will test this after work. I don't really understand the second alternative though. I honestly never really looked into the job functions.

Edit: I tested the luup sleep with 100ms and it seems to work. It seems like any delay I add to sending actions to the vera helps. I am suspecting it to be less of a zwave problem and more of a CPU speed/thread limitation or vera engine limitation preventing the luup engine from being able to handle and queue so many commands at a time. I tested unlocking my door while the rest of the scene was still running and in spite of a short delay, my lock did unlock which is unusual. The failure rate for it was quite high previously and I would often have to repeat the command several times and eventually would see a luup reload. Let me test a few more days and the second alternative too.
I think a big part of the problem if my theory verifies is that my openLuup runs on a much faster machine than the vera... So my commands come in burst.
« Last Edit: January 16, 2019, 09:49:55 pm by rafale77 »
openLuup (79 devices, 141 scenes, 19 apps) master to VeraPlus (142 zwave nodes, 8 Zigbee nodes, 221 devices,  20 scenes , 2 apps) +  Hubitat (15 Zigbee nodes) + Home-Assistant (API Integrations). Bridged to Siri and Alexa. Homewave. VeraPlus ExtRooted and mios server independent.

Offline reneboer

  • Beta Testers
  • Hero Member
  • *****
  • Posts: 1574
  • Karma: +110/-31
Re: Verabridge command queuing idea
« Reply #8 on: January 17, 2019, 05:35:12 am »
Hi raf,

The Vera Luup engine has two ways of running activities, with run or job. Run is only to be used for (very) short actions that need to return a value, like getting a state. They are executed immediately (sort of). A job is to be used in all other cases and are executed when possible. A properly written plugin does this control in the I..xml file, and used correctly will avoid a lot of reloads. This is an example of the two:
Code: [Select]
<action>
<serviceId>urn:rboer-com:serviceId:Harmony1</serviceId>
<name>StartActivity</name>
<job>
Harmony_StartActivity(lul_settings.newActivityID or "")
return 4,nil
</job>
<jobname>StartActivity</jobname>
    </action>
<action>
<serviceId>urn:rboer-com:serviceId:Harmony1</serviceId>
<name>GetCurrentActivityID</name>
<run>
local status, actID = Harmony_GetCurrentActivtyID()
return actID
</run>
    </action>

This is what akbooer is referring to. The change to generic_action will run actions as jobs on the Vera, i.e. scheduled, rather than immediately. It could be the correct solution. I'd be interested too in your test result.

Cheers Rene
2xVeraLite, VeraEdge, openLuup, ALTUI, 20 switches, 10 dimmers, 20 sensors, 10 scene controllers, 1 Harmony Hub, many plug-ins. Not enough time.

Offline akbooer

  • Moderator
  • Master Member
  • *****
  • Posts: 6387
  • Karma: +292/-70
  • "Less is more"
Re: Verabridge command queuing idea
« Reply #9 on: January 17, 2019, 08:25:38 am »
This is true, but jobs offer so much more than this.  It's possible, via cooperative scheduling, to suspend and reenter jobs according to various events such as incoming I/O or specified delay times, or a call which explicitly reschedules the job. 

A job can be in, or set itself into, a number of states:

Code: [Select]

local state =  {
    NoJob=-1,
    WaitingToStart=0,         --  If you return this value, 'job' runs again in 'timeout' seconds
    InProgress=1,
    Error=2,
    Aborted=3,
    Done=4,
    WaitingForCallback=5,     -- This means the job is running and you're waiting for return data
    Requeue=6,
    InProgressPendingData=7,
 }

The above states are the first return code.  The second one is a (safe) delay parameter which can be associated with a number of the states.  This is all part of the normal task action mechanism in Vera (and openLuup.)  I've used technique like this for long-running tasks like transferring files from Vera or downloading updates in AltAppStore.

However, even with a zero delay return, as I suggested you try initially, the openLuup scheduler has an anti-race condition mechanism which means that after a number of immediate reschedules, the job will be suspended for a short while to allow other tasks to run.  This may be sufficient for Vera to keep up.
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P, 9x Philips Hue,
Razberry, MySensors Arduino, HomeWave, AltUI, AltHue, DataYours, Grafana, openLuup, ZWay, ZeroBrane Studio.

Offline rafale77

  • Community Beta
  • Hero Member
  • ******
  • Posts: 1749
  • Karma: +101/-27
  • HA ≠ IoT as a blue sky is cloudless.
Re: Verabridge command queuing idea
« Reply #10 on: January 17, 2019, 11:33:15 am »
Thank you both akbooer and reneboer. I understand better that piece of code now. I have switched to the job implementation and will test it. It seems much more elegant than the luup.sleep. I will report back later today.
openLuup (79 devices, 141 scenes, 19 apps) master to VeraPlus (142 zwave nodes, 8 Zigbee nodes, 221 devices,  20 scenes , 2 apps) +  Hubitat (15 Zigbee nodes) + Home-Assistant (API Integrations). Bridged to Siri and Alexa. Homewave. VeraPlus ExtRooted and mios server independent.

Offline reneboer

  • Beta Testers
  • Hero Member
  • *****
  • Posts: 1574
  • Karma: +110/-31
Re: Verabridge command queuing idea
« Reply #11 on: January 18, 2019, 04:18:31 am »
Hi akbooer,

I know the jobs can do much more, but I guess that is only for the happy few that truly grasp all this like you. I am still in the simple model  ;D

Cheers Rene
2xVeraLite, VeraEdge, openLuup, ALTUI, 20 switches, 10 dimmers, 20 sensors, 10 scene controllers, 1 Harmony Hub, many plug-ins. Not enough time.

Offline akbooer

  • Moderator
  • Master Member
  • *****
  • Posts: 6387
  • Karma: +292/-70
  • "Less is more"
Re: Verabridge command queuing idea
« Reply #12 on: January 18, 2019, 06:34:55 am »
I know the jobs can do much more, but I guess that is only for the happy few that truly grasp all this like you. I am still in the simple model  ;D

Rene, it wasn't a criticism of your excellent post, just an elaboration.   :D

Simple is good - it has more chance of working first time, every time.
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P, 9x Philips Hue,
Razberry, MySensors Arduino, HomeWave, AltUI, AltHue, DataYours, Grafana, openLuup, ZWay, ZeroBrane Studio.

Offline rafale77

  • Community Beta
  • Hero Member
  • ******
  • Posts: 1749
  • Karma: +101/-27
  • HA ≠ IoT as a blue sky is cloudless.
Re: Verabridge command queuing idea
« Reply #13 on: January 18, 2019, 01:57:24 pm »
So far the job code has been working ok. I have not been able to crash the luup engine. However the zwave queue remains a problem in the sense that the vera misses sensor status updates while the commands are running. This of course has a large dependance on how quickly the zwave device/network is able to respond to these actions.
openLuup (79 devices, 141 scenes, 19 apps) master to VeraPlus (142 zwave nodes, 8 Zigbee nodes, 221 devices,  20 scenes , 2 apps) +  Hubitat (15 Zigbee nodes) + Home-Assistant (API Integrations). Bridged to Siri and Alexa. Homewave. VeraPlus ExtRooted and mios server independent.

Offline akbooer

  • Moderator
  • Master Member
  • *****
  • Posts: 6387
  • Karma: +292/-70
  • "Less is more"
Re: Verabridge command queuing idea
« Reply #14 on: January 18, 2019, 05:47:03 pm »
Glad that works to a point, anyway.  Some Vera problems simply can't be fixed, or at least accommodated, remotely!
3x Vera Lite-UI5/Edge-UI7, 25x Fibaro, 23x TKB, 9x MiniMote, 2x NorthQ Power, 2x Netatmo, 1x Foscam FI9831P, 9x Philips Hue,
Razberry, MySensors Arduino, HomeWave, AltUI, AltHue, DataYours, Grafana, openLuup, ZWay, ZeroBrane Studio.