House Modes plugin triggers scenes before House Mode is actually changed

I have several scenes that trigger when the house mode changes from Away to Home on a Vera Plus running the latest 1.7.4001 firmware. I use an external Geofence to run a Return scene when I re-enter the geofence - the Return scene simply sets the House Mode to Home using the House Modes plugin. The problem I am seeing is that setting the house mode to Home triggers the scenes but they won’t run because the house mode is not yet home (1), is it still away (2). In other words, the house mode plugin changes the mode that triggers the scenes BEFORE the house mode is actually changed.

If you examine the log below, you see the following:

[ol][li]External request to run Return scene (due to luup http request when entering Geofence) - OK[/li]
[li]House Mode plugin device 17 changes from 2 (Away) to 1 (Home) as a result of Return scene running - OK[/li]
[li]Several scenes are triggered due to the House Mode change? - OK[/li]
[li]Scenes 1, 8 and 18 do not run because they “think” that the House Mode is 2 (Away) and not 1 (Home) - WRONG / BUG?
[/li]
[li]Device 17 Job 9299 to set house mode completes - OK[/li][/ol]

Either I am missing something pretty basic or this is a bug where scenes configured to trigger on House Mode change to Home are triggered before the value of the House Mode plugin actually changes to from Away to Home. Anyone know a good way to work around it?

08      01/08/19 19:18:37.343   JobHandler_LuaUPnP::HandleActionRequest device: 0 service: urn:micasaverde-com:serviceId:HomeAutomationGateway1 action: ^[[36;1mRunScene^[[0m <0x728f7520>                                                                                                           
08      01/08/19 19:18:37.343   JobHandler_LuaUPnP::HandleActionRequest argument action=RunScene <0x728f7520>                                                                                                                                                                                        
08      01/08/19 19:18:37.344   JobHandler_LuaUPnP::HandleActionRequest argument serviceId=urn:micasaverde-com:serviceId:HomeAutomationGateway1 <0x728f7520>                                                                                                                                         
08      01/08/19 19:18:37.344   JobHandler_LuaUPnP::HandleActionRequest argument SceneNum=14 <0x728f7520>                                                                                                                                                                                            
08      01/08/19 19:18:37.344   Scene::RunScene running 14 Return <0x728f7520>                                                                                                                                                                                                                       
08      01/08/19 19:18:37.349   JobHandler_LuaUPnP::HandleActionRequest device: 17 service: urn:micasaverde-com:serviceId:HouseModes1 action: ^[[36;1mSetHMode^[[0m <0x728f7520>                                                                                                                     
08      01/08/19 19:18:37.349   JobHandler_LuaUPnP::HandleActionRequest argument newHModeValue=1 <0x728f7520>                                                                                                                                                                                        
06      01/08/19 19:18:37.363   Device_Variable::m_szValue_set device: 17 service: urn:micasaverde-com:serviceId:HouseModes1 variable: ^[[35;1mHMode^[[0m was: 2 now: 1 #hooks: 8 upnp: 0 skip: 0 v:0x1004820/NONE duplicate:0 <0x775c8320>                                                          
07      01/08/19 19:18:37.363   Event::Evaluate 1  scene Sunset-50m Lighting wrong house mode 2 repeat 0/0 <0x775c8320>                                                                                                                                                                              
07      01/08/19 19:18:37.363   Event::Evaluate 9  scene Sunset+20m Evening Lighting wrong house mode 2 repeat 0/0 <0x775c8320>                                                                                                                                                                      
07      01/08/19 19:18:37.364   Event::Evaluate 13  scene HVAC @Sleep is false repeat 0/0 <0x775c8320>                                                                                                                                                                                               
07      01/08/19 19:18:37.364   Event::Evaluate 14  scene HVAC @Wake/Home is true users: allow:1 <0x775c8320>                                                                                                                                                                                        
08      01/08/19 19:18:37.364   Scene::RunScene running 21 HVAC @Wake/Home <0x775c8320>                                                                                                                                                                                                              
08      01/08/19 19:18:37.364   JobHandler_LuaUPnP::HandleActionRequest device: 9 service: urn:upnp-org:serviceId:TemperatureSetpoint1_Heat action: ^[[36;1mSetCurrentSetpoint^[[0m <0x775c8320>                                                                                                     
08      01/08/19 19:18:37.365   JobHandler_LuaUPnP::HandleActionRequest argument NewCurrentSetpoint=73 <0x775c8320>                                                                                                                                                                                  
08      01/08/19 19:18:37.366   JobHandler_LuaUPnP::HandleActionRequest device: 9 service: urn:upnp-org:serviceId:TemperatureSetpoint1_Cool action: ^[[36;1mSetCurrentSetpoint^[[0m <0x775c8320>                                                                                                     
08      01/08/19 19:18:37.366   JobHandler_LuaUPnP::HandleActionRequest argument NewCurrentSetpoint=74 <0x775c8320>                                                                                                                                                                                  
08      01/08/19 19:18:37.367   JobHandler_LuaUPnP::HandleActionRequest device: 7 service: urn:upnp-org:serviceId:TemperatureSetpoint1_Heat action: ^[[36;1mSetCurrentSetpoint^[[0m <0x775c8320>                                                                                                     
08      01/08/19 19:18:37.367   JobHandler_LuaUPnP::HandleActionRequest argument NewCurrentSetpoint=72 <0x775c8320>                                                                                                                                                                                  
08      01/08/19 19:18:37.368   JobHandler_LuaUPnP::HandleActionRequest device: 7 service: urn:upnp-org:serviceId:TemperatureSetpoint1_Cool action: ^[[36;1mSetCurrentSetpoint^[[0m <0x775c8320>                                                                                                     
08      01/08/19 19:18:37.368   JobHandler_LuaUPnP::HandleActionRequest argument NewCurrentSetpoint=74 <0x775c8320>                                                                                                                                                                                  
07      01/08/19 19:18:37.369   Event::Evaluate 17  scene Palm Christmas OFF is false repeat 0/0 <0x775c8320>                                                                                                                                                                                        
07      01/08/19 19:18:37.369   Event::Evaluate 18  scene Sunset-20m Lighting wrong house mode 2 repeat 0/0 <0x775c8320>                                                                                                                                                                             
07      01/08/19 19:18:37.370   Event::Evaluate 19  scene Ground-Level @night is true users: allow:1 <0x775c8320>                                                                                                                                                                                    
08      01/08/19 19:18:37.370   Scene::RunScene running 27 Ground-Level @night <0x775c8320>                                                                                                                                                                                                          
08      01/08/19 19:18:37.370   JobHandler_LuaUPnP::HandleActionRequest device: 101 service: urn:upnp-org:serviceId:Dimming1 action: ^[[36;1mSetLoadLevelTarget^[[0m <0x775c8320>                                                                                                                    
08      01/08/19 19:18:37.370   JobHandler_LuaUPnP::HandleActionRequest argument newLoadlevelTarget=15 <0x775c8320>                                                                                                                                                                                  
06      01/08/19 19:18:37.371   Device_Variable::m_szValue_set device: 101 service: urn:upnp-org:serviceId:Dimming1 variable: ^[[35;1mLoadLevelTarget^[[0m was: 0 now: 15 #hooks: 0 upnp: 0 skip: 0 v:0xfd6e30/NONE duplicate:0 <0x775c8320>                                                         
01      01/08/19 19:18:37.373   ^[[31;1mluup_log:20: SensorMqtt plugin: Device: 17 Variable: HMode Value 2 => 1^[[0m <0x775c8320>                                                                                                                                                                    
01      01/08/19 19:18:37.376   ^[[31;1mluup_log:20: SensorMqtt plugin: Publish MQTT message on topic: Vera/50000796/HouseModes1/17 with value:{"DeviceId":17,"DeviceName":"House Modes Plugin","DeviceType":"urn:schemas-micasaverde-com:device:HouseModes:1","HMode":1,"OldHMode":2,"RoomId":0,"Roo
06      01/08/19 19:18:37.378   Device_Variable::m_szValue_set device: 20 service: urn:upnp-sensor-mqtt-se:serviceId:SensorMqtt1 variable: ^[[35;1mmqttLastMessage^[[0m was: {"Payload":{"Alias":87,"City":"Wrightsville Beach","DeviceId":87,"DeviceName":"Landscape 1-4","DeviceType":"urn:schemas-
08      01/08/19 19:18:37.379   JobHandler_LuaUPnP::HandleActionRequest device: 0 service: urn:micasaverde-com:serviceId:HomeAutomationGateway1 action: ^[[36;1mSetHouseMode^[[0m <0x775c8320>                                                                                                       
08      01/08/19 19:18:37.379   JobHandler_LuaUPnP::HandleActionRequest argument Mode=1 <0x775c8320>                                                                                                                                                                                                 
06      01/08/19 19:18:37.380   Device_Variable::m_szValue_set device: 46 service: urn:micasaverde-com:serviceId:SecuritySensor1 variable: ^[[35;1mArmed^[[0m was: 1 now: 0 #hooks: 0 upnp: 0 skip: 0 v:0xcb82a8/NONE duplicate:0 <0x775c8320>                                                       
06      01/08/19 19:18:37.381   Device_Variable::m_szValue_set device: 56 service: urn:micasaverde-com:serviceId:SecuritySensor1 variable: ^[[35;1mArmed^[[0m was: 1 now: 0 #hooks: 0 upnp: 0 skip: 0 v:0xcb82a8/NONE duplicate:0 <0x775c8320>                                                       
06      01/08/19 19:18:37.382   Device_Variable::m_szValue_set device: 52 service: urn:micasaverde-com:serviceId:SecuritySensor1 variable: ^[[35;1mArmed^[[0m was: 1 now: 0 #hooks: 0 upnp: 0 skip: 0 v:0xcb82a8/NONE duplicate:0 <0x775c8320>                                                       
06      01/08/19 19:18:37.383   Device_Variable::m_szValue_set device: 41 service: urn:micasaverde-com:serviceId:SecuritySensor1 variable: ^[[35;1mArmed^[[0m was: 1 now: 0 #hooks: 0 upnp: 0 skip: 0 v:0xcb82a8/NONE duplicate:0 <0x775c8320>                                                       
06      01/08/19 19:18:37.385   Device_Variable::m_szValue_set device: 17 service: urn:micasaverde-com:serviceId:HouseModes1 variable: ^[[35;1mMessage^[[0m was:  now:  #hooks: 0 upnp: 0 skip: 0 v:(nil)/NONE duplicate:1 <0x775c8320>                                                              
04      01/08/19 19:18:37.386   <Job ID="9299" Name="" Device="17" Created="2019-01-08 19:18:37" Started="2019-01-08 19:18:37" Completed="2019-01-08 19:18:37" Duration="0.35969000" Runtime="0.23410000" Status="Successful" LastNote=""/> <0x775c8320>                                             

Reading through your log I am concluding that you assume that the device variable change of the house mode plugin is equal to your vera’s housemode attribute.
They are in fact two different variables and the plugin syncs the variable to the attribute. Unfortunately the more time I spend on the vera, the more I am convinced that the luup engine was written and tested on a much faster machine and then built for openWRT MIPS without consideration of a lot of timing issues due to the much slower CPU.

Your house mode triggered scenes are triggered by the housemode plugin device variable but the scenes are checking for the vera’s housemode which has not yet been updated by the plugin yet.

My suggestion to you would be to put a delay in the lua code for these scene to trigger.
For example, trigger these scenes from the return scene using the following code:

local scnid= ##your scene number##
luup.sleep (200)
luup.call_action("urn.micasaverde-com:serviceId:HomeAutomationGateway1","RunScene",{SceneNum="scnid"},0)

or maybe a luup.call_delay.

Definitely not a luup.sleep(). A delay of that long will likely restart the Luup engine. Use luup.delay() every time.

200 is only 200ms… It works for one of my plugins. And it seems like it is not freezing

Oops, yes. Not paying attention! However, I do always try to avoid luup.sleep() as a matter of policy!

Yeah I am with you. It is just a lazy way to do it. I generally would recommend the luup.call_delay which requires creating a function. In later version of the luup engine it seems like luup.sleep no longer gets the entire thread to sleep but I may be wrong.

There are two threads per device plugin. Not sure how the work is allocated between, but there is some specialisation.

[quote=“rafale77, post:2, topic:200398”]Reading through your log I am concluding that you assume that the device variable change of the house mode plugin is equal to your vera’s housemode attribute.
They are in fact two different variables and the plugin syncs the variable to the attribute.
Your house mode triggered scenes are triggered by the housemode plugin device variable but the scenes are checking for the vera’s housemode which has not yet been updated by the plugin yet.[/quote]

Hi Patrick,

Thanks for the suggestion. Yes, I realize that the plugin device 17 ultimately sets the house mode on device 0 (the controller) after some delay that can be configured under Modes; the delay defaults to 30 seconds and is currently set to 1.

That said, IMHO the settings, triggering and mode querying should be self-consistent because the scenes are all configured to either set or trigger on the House Modes Plugin (device 17). I have included screenshots below for a pretty simple event-driven scene execution scenario.

Return (manually run in this use case) - sets House Mode Plugin to Home
Sunset-50m Lighting (triggers on House Mode change Home, valid in Home mode)

If the scene mode short-circuiting uses device 0 for scenes that are hooked / triggers on device 17, then I would consider that a bug with either the house modes plugin or the scene mode filtering because they should be semantically equivalent - if you guys agree, I will contact Support to confirm.

As a work-around I considered using luup to run the scenes directly using the Return scene luup code but that usurps the foundational event triggering and filtering paradigm built into the Vera eliminating the possibility to use luup code within the house mode change trigger on the “triggered” scenes (like only continue after dark) and buries the triggering logic in scene Luup blocks making it difficult to review/debug - I so wish the Vera had built-in support for documenting scenes using markup.

In order to try to preserve the scene triggering as closely to what I think Vera intended, I will try changing the Return scene Luup Code to set the house mode on device 0 (controller ). However, given that the scene triggers are hooked to the House Modes device 17 variable, I suspect that I will need to set the plugin value after setting is on device 0 so I suspect I will have to move it from the Return scene’s “Immediately” to the luup code block or change immediately to use a 1 second delay (yuck).

Thanks,
Bruce

Looking at the code for the HouseModes plugin, it looks like if you use the SetHMode action on that plugin to set the house mode, it immediately changes its own variable HMode. That in turn will trigger scenes that use the HouseModes plugin as a trigger (immediately).

This is defective, IMO, because it ignores the time delay that Vera goes through when changing the house mode. It is also very odd because the plugin also attempts to go into a wait loop to wait until the delay expires, and then does nothing when it does–the code that sets the plugin’s HMode variable there is commented out. So it looks like the intent was there to handle it right, but the way it works now is either an original bug, or a well-intentioned change made later that isn’t correct.

The remainder of the plugin works properly (scanning for house mode changes made by facilities other than the plugin). So, I would simply not use the SetHMode action on the plugin to change the house mode–do not use the HouseModes plugin to change the house mode, just to monitor/trigger on it. Use the HomeAutomationGateway action SetHouseMode to change the house mode instead.

[quote=“rigpapa, post:9, topic:200398”]Looking at the code for the HouseModes plugin, it looks like if you use the SetHMode action on that plugin to set the house mode, it immediately changes its own variable HMode. That in turn will trigger scenes that use the HouseModes plugin as a trigger (immediately).

This is defective, IMO, because it ignores the time delay that Vera goes through when changing the house mode. It is also very odd because the plugin also attempts to go into a wait loop to wait until the delay expires, and then does nothing when it does–the code that sets the plugin’s HMode variable there is commented out. So it looks like the intent was there to handle it right, but the way it works now is either an original bug, or a well-intentioned change made later that isn’t correct.

The remainder of the plugin works properly (scanning for house mode changes made by facilities other than the plugin). So, I would simply not use the SetHMode action on the plugin to change the house mode–do not use the HouseModes plugin to change the house mode, just to monitor/trigger on it. Use the HomeAutomationGateway action SetHouseMode to change the house mode instead.[/quote]

I could be a huge cad and just recommend Reactor as a fix for this, since Reactor handles housemodes with aplomb, but I’ll readily admit if you’re already using the HouseModes plugin in conjunction with… ahem… other tools… installing Reactor is a big hammer for a small nail.

It appears the original author of this plugin is no longer here (messages now associated with a locked guest account), so in the interest of being a good community citizen, here is my take at an update HouseModes plugin. Changes made:

  • The plugin’s house mode state variable HMode is now only set when it changes, at the point where the change is detected, meaning scenes will only fire on actual change and when Vera has completed the delay and any attribute-based references or triggers will align;
  • Improve the messages displayed by the plugin and those logged (both debug and non-debug modes);
  • Separate current state from target state on the UI7 dashboard/control panel buttons so they’re not jumping around during the delay period;
  • Add a new PrevHMode state variable containing the previous house mode, so you can check in your code for specific transitions (e.g. went from Night to Home);
  • Cleaned up the code. It’s much smaller.

ZIP attached.

EDIT: Note for future readers: install the plugin from the plugin marketplace before updating with this ZIP file; the ZIP file is not a complete set of files, it’s just an update.

Thanks Patrick - I’ll take a look at your refactored plugin. For now, changing my Return scene to simply use Luup Code, and only that, as follows:

local homeMode = 1
luup.call_action("urn:micasaverde-com:serviceId:HomeAutomationGateway1", "SetHouseMode", { Mode=homeMode }, 0)

actually triggers the cascade of events needed to run all my scenes as can be seen below.

08      01/09/19 11:31:58.110   JobHandler_LuaUPnP::HandleActionRequest device: 0 service: urn:micasaverde-com:serviceId:HomeAutomationGateway1 action: RunScene <0x73229520>
08      01/09/19 11:31:58.110   JobHandler_LuaUPnP::HandleActionRequest argument serviceId=urn:micasaverde-com:serviceId:HomeAutomationGateway1 <0x73229520>
08      01/09/19 11:31:58.110   JobHandler_LuaUPnP::HandleActionRequest argument action=RunScene <0x73229520>
08      01/09/19 11:31:58.111   JobHandler_LuaUPnP::HandleActionRequest argument SceneNum=14 <0x73229520>
08      01/09/19 11:31:58.111   JobHandler_LuaUPnP::HandleActionRequest argument rand=0.9434472640243414 <0x73229520>
08      01/09/19 11:31:58.111   Scene::RunScene running 14 Return <0x73229520>
08      01/09/19 11:31:58.112   JobHandler_LuaUPnP::HandleActionRequest device: 0 service: urn:micasaverde-com:serviceId:HomeAutomationGateway1 action: SetHouseMode <0x73229520>
08      01/09/19 11:31:58.112   JobHandler_LuaUPnP::HandleActionRequest argument Mode=1 <0x73229520>
06      01/09/19 11:31:58.113   Device_Variable::m_szValue_set device: 46 service: urn:micasaverde-com:serviceId:SecuritySensor1 variable: Armed was: 1 now: 0 #hooks: 0 upnp: 0 skip: 0 v:0xa4b270/NONE duplicate:0 <0x73229520>
07      01/09/19 11:31:58.939   RAServerSync::SendAlert 0xfb5db0 PK_Event 20244025572 retries 0 URL https://vera-us-oem-event11.mios.com/event/event/event age: 0 file:  err: 0 sess: 000000006C89005C36463CFF4336B13D1293E8 serv: vera-us-oem-event11.mios.com/vera-us-oem-event11.mios.com <0x7746a520>
06      01/09/19 11:32:03.348   Device_Variable::m_szValue_set device: 31 service: urn:micasaverde-com:serviceId:EnergyMetering1 variable: Watts was: 0.000 now: 0.000 #hooks: 1 upnp: 0 skip: 0 v:0xd59fa8/NONE duplicate:1 <0x7726a520>
06      01/09/19 11:32:05.101   Device_Variable::m_szValue_set device: 17 service: urn:micasaverde-com:serviceId:HouseModes1 variable: HMode was: 2 now: 1 #hooks: 8 upnp: 0 skip: 0 v:0xd972f0/NONE duplicate:0 <0x75a6a520>
07      01/09/19 11:32:05.101   Event::Evaluate 1  scene Sunset-50m Lighting lua returned false repeat 0/0 <0x75a6a520>
07      01/09/19 11:32:05.101   Event::Evaluate 9  scene Sunset+20m Evening Lighting is true users: allow:1 <0x75a6a520>
08      01/09/19 11:32:05.102   Scene::RunScene running 12 Sunset+20m Evening Lighting <0x75a6a520>
07      01/09/19 11:32:05.111   Event::Evaluate 13  scene HVAC @Sleep is false repeat 0/0 <0x75a6a520>
07      01/09/19 11:32:05.111   Event::Evaluate 14  scene HVAC @Wake/Home is true users: allow:1 <0x75a6a520>
08      01/09/19 11:32:05.112   Scene::RunScene running 21 HVAC @Wake/Home <0x75a6a520>
07      01/09/19 11:32:05.129   Event::Evaluate 17  scene Palm Christmas OFF is false repeat 0/0 <0x75a6a520>
07      01/09/19 11:32:05.130   Event::Evaluate 18  scene Sunset-20m Lighting lua returned false repeat 0/0 <0x75a6a520>
07      01/09/19 11:32:05.130   Event::Evaluate 19  scene Ground-Level @night lua returned false repeat 0/0 <0x75a6a520>
01      01/09/19 11:32:05.131   luup_log:20: SensorMqtt plugin: Device: 17 Variable: HMode Value 2 => 1 <0x75a6a520>
01      01/09/19 11:32:05.137   luup_log:20: SensorMqtt plugin: Publish MQTT message on topic: Vera/50000796/HouseModes1/17 with value:{"DeviceId":17,"DeviceName":"House Modes Plugin","DeviceType":"urn:schemas-micasaverde-com:device:HouseModes:1","HMode":1,"OldHMode":2,"RoomId":0,"RoomName":"No Room","ServiceId":"urn:micasaverde-com:serviceId:HouseModes1","Time":1547051525,"Variable":"HMode"} <0x75a6a520>

I was under the impression that the HouseModes plugin was developed/maintained by MIOS/Vera.

Hi Patrick, I reviewed your plugin changes and the improvements are good. It is unfortunate that the house mode is an attribute of the vera and not a variable that can be hooked to avoid polling. Any thoughts on making your changes more broadly available?

I have stumbled across your Reactor plugin in the past and really liked what I saw but haven’t taken the time to install it or play with it because I tend to keep my Vera close to bare metal and have found that simply decorating triggers and scenes with a little bit of lua typically works well for me. I’ll try to take Reactor for a spin in the near future…

Thanks again for your help.

Any thoughts on making your changes more broadly available?[/quote]

I have no problems distributing the changes. I’m not sure what the original author’s license is, and it’s not listed in the plugin marketplace. If you’re talking about putting it in the marketplace, I’ll leave that to others, I was just trying to help out quickly here.

how do i get the updated file you have made Patrick??? I cant seem to find it here>???

It’s still on the old forums, in this post: http://forum.micasaverde.com/index.php/topic,118346.msg437774.html#msg437774

And now here (permanently): housemodes-190109-rigpapa.zip (8.2 KB)

Thanks Pat.

Im hoping your new version might resolve an issue ive had popup recently.

I use the DSC plugin to trigger my house modes via Vera Scenes. When it arms home it sets house mode night, arm away and sets house away… the issue i have is it now does not set house mode to home when i disarm. Worked pretty much flawlessly for 2 years and seems to have just stopped.

I deleted the scene and recreated it with no joy.

Josh