Jump to content

Welcome to Smart Home Forum by FIBARO

Dear Guest,

 

as you can notice parts of Smart Home Forum by FIBARO is not available for you. You have to register in order to view all content and post in our community. Don't worry! Registration is a simple free process that requires minimal information for you to sign up. Become a part of of Smart Home Forum by FIBARO by creating an account.

 

As a member you can:

  •     Start new topics and reply to others
  •     Follow topics and users to get email updates
  •     Get your own profile page and make new friends
  •     Send personal messages
  •     ... and learn a lot about our system!

 

Regards,

Smart Home Forum by FIBARO Team


HC2 scene emulator


jgab

Recommended Posts

This is a very simple to use, but also very powerful Scene emulator.

  • Download

    Please login or register to see this link.

    Lua development environment (free for Windows, MacOS, Linux)
  • Install ZeroBrane and open it.
  • Create a simple scene in ZeroBrane (File->New) and save it

Please login or register to see this code.

  • Set the project directory in ZeroBrane to where you saved the scene (Project -> Project Directory -> Set From Current File)
  • Download

    Please login or register to see this link.

    (that is the Emulator). Put the file in the same directory.
  • Go back to the scene in ZeroBrane. Run it with F5 or [>] button.

If done right the output should be 

Please login or register to see this code.

We see it loads the scene (with default sceneID 99)  and prints the debug statement "My scene Autostarted!" (a video showing how to use the emulator is

Please login or register to see this link.

)

More debug tips 

  • Please login or register to see this link.

  • Please login or register to see this link.

 

 

The reason I have developed this emulator is to be able to develop some quite advanced scenes, and I have been missing a good development environment for HC2 scenes.

I have a rather complex framework with several scenes that sends events between each other and it is time consuming to debug it on the HC2. This emulator allows me to run scenes off-line in parallell and allow them to react on triggers and call each other with fibaro:startScene. It also allows me to receive live triggers from the HC2 to the emulator by using a proxy scene. 

It's general enough to run most scenes, as-is without modification, so other that develop demanding scenes may find it useful.

There is no VD support currently as it needs a GUI, but hybrids can be made to work, see below.

It tries to mimic the HC2 behaviour the best it can...

 

Features:

  • Can run most scenes as is without modification. (and I try to implement missing features as they are discovered...)
  • Can use a mix of devices simulated in the emulator and devices controlled on the HC2
  • Scenes in emulator can interact with scenes on the HC2 (startScene or setting globals/labels/sliders)
  • Can create scene triggers at specific times in the future for testing how scenes react
  • Scenes can be debugged and code stepped through
  • Emulator can be run faster than real-time, making it convenient to run scenes like schedulers that do stuff at long intervals. 
  • Many scenes can be run at the same time making it possible to debug the interaction between them
  • Simple web GUI to manually set off triggers and start/stop scenes (will be extended, and maybe support VDs some day...)
  • ZBS plugin (goes into ZBS 'packages' directory)

    Please login or register to see this link.

    that gives editor shortcut to open web GUI
  • Flexible usage, either run the emulator that register scenes, or run a scene that includes the emulator.

 

Documentation:

-

Please login or register to see this link.

-

Please login or register to see this link.

-

Please login or register to see this link.

- Setup (TBD) (see video 

Please login or register to see this link.

)

- Debugging (TBD) (see video 

Please login or register to see this link.

)

- Examples (TBD) (see video 

Please login or register to see this link.

)

- Implementation notes (TBD)

 

Quick setup looks like this:

Please login or register to see this code.

The web GUI

Please login or register to see this attachment.

Main page where you can "speed" the emulator, or jump 1 or 24 hour forward

Triggers of type property/global in the registered scene headers are listed and can be set, causing sourceTriggers to be sent to the emulator, that then will trigger scenes.

You can also copy setup from HC2 and create a HC2.data file

 

Please login or register to see this attachment.

 

Please login or register to see this attachment.

This is how it works.
1. Configuration is copied from the HC2 (devices, scenes, rooms, globals).
-HC2.copyConfigFromHC2(_HC2_FILE), will copy data from HC2 and store in file HC2.data (default)

-HC2.loadConfigFromFile(_HC2_FILE), will read in data form HC2.data (default)

You can also use the button in the Web GUI which is recommended (see 

Please login or register to see this link.

)

-etc...

 

2. Resources, devices, scenes, globals, rooms with the attribute ._local will be handled locally.
All other resources will be remotely accessed (true api calls to the HC2).

Initially all resources are set to local.
Resources can be set to remote with HC2.setRemote(<resource type>,<id>)
Local devices can be created with HC2.createDevice.

Local fibaro globals can be created with HC2.createGlobal.

 

3. Local scenes are then registered with HC2.registerScene. If the same sceneID is used as a remote HC2 scene, 
the locally registered scene will override the real scene i the emulator. Its also possible to use

HC2.loadScenesFromDir(<dir>) to load all files with name <ID>_<name>.lua from dir, Ex. 11_MyScene.lua.

 

4. The emulator fire off a {type='autostart'} trigger to kickstart the emulation. Scenes with a '%% autostart' in the
header will then be started. Any trigger generated by local scenes manipulating devices or globals will start any 
 local scenes that have those triggers in their scene headers. etc etc
Scenes can also do fibaro:startScene(devideID[,args]) to start local or remote scenes.

 

5. In the HC2 main() function, triggers can be posted with HC2.post to simulate trigger in the future to make scenes react.

 

6. The emulator has only been tested with the Lua IDE ZeroBrane Studio (ZBS). It requires 'mobdebug' and the possibility to 
debug Lua coroutines, but that's included in a default ZBS installation. This

Please login or register to see this link.

is a good introduction to ZeroBrane and fibaro.
The link talks about FibaroSceneAPI.lua and a json library but there is no need to install them as HC2.lua has it's own modified version
of FibaroSceneAPI.lua and a json library.

 

7. In ZBS it is convenient to set the HC2.lua as start file. (right click in Project tab on HC2.lua and chose 
"Set HC2.lua As Start File". Then add the HC2.registerScene statements for the scene to be run. Open the file in 
another tab and hit F5 (start debugging). Break-points can be set in the scene, and it can be stepped through, almost
like an ordinary debugged Lua file. If you happen to "step into" a fibaro:* function you will step into the emulator but it's just to 
"step out" of the current function and your'e back in your scene. Normally just do "step over" fibaro:* functions.

 

8. Scenes running on the HC2 can send triggers to HC2 emulator. This makes it possible to setup a scene on the HC2
that proxy triggers on the HC2 to the HC2 emulator and make it possible to debug scenes triggering on live triggers.

 

Supported HC2 functions:

Please login or register to see this code.

To send triggers from the HC2 to the emulator

Please login or register to see this code.

To start a scene on the emulator from the HC2

Please login or register to see this code.


Things to do - 

-VD support (needs a GUI). In principle, the VD could run on the HC2 and a supporting scene on the emulator, with a HC2 scene forwarding triggers (globals, labels etc)

 

Will update this post with additional features and documentation.

 

File HC2.lua is available in my Github <

Please login or register to see this link.

>

Edited by jgab
  • Like 4
Link to comment
Share on other sites

Jan Hi!

do you test this lib in VStudio (yes, I know you use ZB)

 

Thank you!

Link to comment
Share on other sites

  • Topic Author
  • Sorry, I'm also a Mac user :-)

    The only thing that could be an issue is 'mobdebug' and (maybe) the socket library - everything else is vanilla Lua...

     

    Edited by jgab
    Link to comment
    Share on other sites

  • Topic Author
  • HC2.* commands available to setup the emulator. Used inside 'main()' of HC2.lua

     

    Please login or register to see this code.

    Copy scenes, devices,globals etc from HC2 and stores it in file. 

    When done the first time it can be commented out to not run every time.

    If the emulator has information on resources in the HC2 it can make better emulate the fibaro:* functions.

    Please login or register to see this code.

    Load HC2 data from file.

    Please login or register to see this code.

    Register a local scene with 'name' and 'id' from file 'file'.
    Optionally creates fibaro globals.
    Ex. HC2.registerScene('MyScene',11,'myscene.lua',{test=42,foo=66})

     

    Please login or register to see this code.

    Load scene calling HC2.lua.

    If the line

    Please login or register to see this code.

    is included at the top of the scene it will call the emulator and load the scene into it and start-up. This is an alternative way instead of starting the HC2.lua scene. It is harmless to keep the line when moving the scene to the HC2 as there is no 'dofile' defined there. Name and sceneID can be passed in the _EMBEDDED variable.

    Please login or register to see this code.

    Load all scenes from directory
    Filename need to be on format <sceneID>_<name>.lua
    Ex. '11_MyScene.lua'

     

    Please login or register to see this code.

    Create a local fibaro global with name and value

    Please login or register to see this code.

    Create a local device with id and name. All other values have defaults.

     

    Please login or register to see this code.

    Tags resources as remote or local.
    Ex.
    HC2.setRemote("devices",true) -- tags all devices as remote
    HC2.setRemote("devices",66) -- tags device 66 as remote
    HC2.setLocal("globalVariables","Test") -- tags global "Test"  as local

     

    Please login or register to see this code.

    List known devices to the console

     

    Please login or register to see this code.

    List known scenes to the console

     

    Please login or register to see this code.

    Enables logging of fibaro:* calls to the console

     

    Please login or register to see this code.

    Adds output filter to fibaro:debug. Useful for skipping noisy output or trim output before being written to copnsole.
    Ex. removing html tags in output. The filter "f" is a regexp that should return what should be printed.
    Removing html tags would be 
    HC2.addDebugFilter("%b<>(.*)</.*>")
    If 'skip' is tyrue the whole output is ignored if there is a match.
    Removing all log output starting with "waiting" would be
    HC2.addDebugFilter("^waiting",true)

    Edited by jgab
    Link to comment
    Share on other sites

  • Topic Author
  • (This is an old post and the setup has changed a bit see 

     

    Simple get-starting example.

    -Download

    Please login or register to see this link.

    (ZBS) and install it. It is a free Lua IDE for Windows, MacOS and Linux

    -Download

    Please login or register to see this link.

    and

    Please login or register to see this link.

    from my

    Please login or register to see this link.

    and put them in a working directory

    -Open both files in the ZBS editor.

    -Select the HC2.lua window and add the credentials to log in to your HC2 (IP, email, and password)

    Please login or register to see this attachment.

    Please login or register to see this code.

    If you you don't have access to the box, change the line 

    Please login or register to see this code.

    to

    Please login or register to see this code.

    so it doesn't try to download data from the HC2. It may complain that ,"No HC2 data file found" but don't worry, this example doesn't need it. (After you have run the first time connected to the box it will create a file HC2.data with scenes, devices, and globals from the HC2. You can then set the last argument to false again, because reading the data from the HC2 at every run takes some time)

     

    -have a look at the sceneTest file in ZBS

    Please login or register to see this attachment.

    Please login or register to see this spoiler.

    Output from the emulator is prefix with "#[HC2]"

     

    It loads a scene with name SceneTest and sceneID=99 from the file 'sceneTest.lua'. All header triggers are listed.

    Please login or register to see this code.

    At 16:38:28 someone is turning on the trigger (or breaching a motion sensor)

    Please login or register to see this code.

    We see that the emulator is starting the scene

    Please login or register to see this code.

    and the scene is turning on a lamp, the prefix '#[99:1]' means scene with id 99, and instance 1.

    Please login or register to see this code.

    After a minute the motion sensor is turned off, causing a scene trigger again

    Please login or register to see this code.

    ...however, the scene will ignore it as it is already running. Instead after motion sensor been safe for 4minutes it will turn off the lamp

    Please login or register to see this code.

    The reason our scene gets triggers (the motion sensor turning on and off) is that we inside the HC2.lua file, where we register to load the scene 'sceneTest.lua' ,we also add triggers should be fired in  the future. Here in the form of fibaro:call commands (the 'fibaro' is left out).

    Please login or register to see this code.

    Here we wait 10min an do a fibaro:call(66,'turnOn') , works with motion sensors in the emulator :-)

    and another after 11 min to turn of the sensor.

    This way we can test our scene under controlled circumstances with the triggers we choose at the time we select.

     

    -Run again and this time set breakpoints in the sceneTest.lua window. It will break at those lines and you can step through the code and inspect variables etc...

    Please login or register to see this attachment.

     

    When the emulator runs you can also open the web browser at 

    Please login or register to see this link.

     and there is a simple GUI to list scenes and devices and set off some triggers. The GUI will be developed with more features.

     

    Here is

    Please login or register to see this link.

    by @riemers, (but you don't need to download the files in the post)

    TaDa!

    Edited by jgab
    Link to comment
    Share on other sites

    @jgab , testing. Noticed also a statement in line 38: _HC2_FILE = "HC2.data". Is this file missing?

     

    Program starting as '"C:\Program Files (x86)\ZeroBraneStudio\bin\lua52.exe" -e "io.stdout:setvbuf('no')" "C:\Users\F\AppData\Local\Temp\.3A1B.tmp"'.
    Program 'lua52.exe' started in 'C:\Program Files (x86)\ZeroBraneStudio\myprograms' (pid: 18300).
    Debugging session started in 'C:\Program Files (x86)\ZeroBraneStudio\myprograms\'.
    #[HC2]17:45:04, Wed Mar 06: HC2 SceneRunner v0.3 fix8
    #[HC2]17:45:04, Wed Mar 06: Reading configuration from H2C...
    #[HC2]17:45:04, Wed Mar 06: HC2 returned error '302 ' - URL: 'http://192.198.1.118/api/globalVariables/'.
    Program completed in 4.71 seconds (pid: 18300).
    Debugging session completed (traced 0 instructions).
     

    Link to comment
    Share on other sites

  • Topic Author
  • 8 minutes ago, FrankT said:

    @jgab , testing. Noticed also a statement in line 38: _HC2_FILE = "HC2.data". Is this file missing?

     

    Program starting as '"C:\Program Files (x86)\ZeroBraneStudio\bin\lua52.exe" -e "io.stdout:setvbuf('no')" "C:\Users\F\AppData\Local\Temp\.3A1B.tmp"'.
    Program 'lua52.exe' started in 'C:\Program Files (x86)\ZeroBraneStudio\myprograms' (pid: 18300).
    Debugging session started in 'C:\Program Files (x86)\ZeroBraneStudio\myprograms\'.
    #[HC2]17:45:04, Wed Mar 06: HC2 SceneRunner v0.3 fix8
    #[HC2]17:45:04, Wed Mar 06: Reading configuration from H2C...
    #[HC2]17:45:04, Wed Mar 06: HC2 returned error '302 ' - URL: 'http://192.198.1.118/api/globalVariables/'.
    Program completed in 4.71 seconds (pid: 18300).
    Debugging session completed (traced 0 instructions).
     

    No, it creates that file by copying data from the HC2. Are you connected to your box?

    otherwise comment out the  

    Please login or register to see this code.

    or set the arguments to false,false. If you are not connected also set _REMOTE to false too.

    Thanks, will add some extra belts and suspenders for the next release so this not so confusing.

     

    Edited by jgab
    Link to comment
    Share on other sites

    Please login or register to see this code.

    Hmm still there, @jgab

    Cannot find where it comes from.

    Edited by jompa68
    Link to comment
    Share on other sites

  • Topic Author
  • 6 hours ago, jompa68 said:

    Please login or register to see this code.

    Hmm still there, @jgab

    Cannot find where it comes from.

    Sorry, I had left some debug code that always returned scene 11 - which you don't have.... I fixed it and pushed a new version.

    The pub/sub kind of works but only between scenes on the emulator - the HC2 scenes don't know how to answer back to the emulator - yet. It will come any day.

    To be really safe turn off _REMOTE. 

    Oh, and at startup the second argument to HC2.setupConfiguration(true,true) , tells the emulator to copy data from the HC2, if you have done that and a HC2.data file has been created it can be good to set it to false, because it takes a little bit of time to read the data from HC2 every time. It goes much quicker to start the emulator without copying from the HC2. 

    If you change scenes,devices,globals on the HC2 just rerun with the second argument to true to create an up to date HC2.data file.

     

    HC2 is some ~2000 lines of new code (and some borrowed and some old code from EventRunnerDebug) so we will have some issues before it runs smooth again for all of us - thanks for your patience :-)

     

    iOSLocator.lua also had the "11th scene" bug - pushed a new version.

     

    A bit of a roadmap for the coming days;

    - Will fix pub/sub between emulator and HC2 scenes. (However I would not recommend running a copy of the Supervisor on the emulator as it would confuse the Supervisor running on the HC2)

    - Will optimise the log output when running ER scenes. Unnecessary to see the scene starting for every new trigger, i.e we are only interested in debug messages from the main instance.

    - Make ER scenes accept a "proxy event" being sent to them so that ER scenes running on the HC2 starts to forward all triggers to its copy in the emulator instead of handling the triggers themselves. 

    - and I will fix all the bugs you discover :-) 

     

    If you have any idea about how the setup and the debug workflow could be easier let me know.

     

    Edited by jgab
    • Like 2
    Link to comment
    Share on other sites

    Great, will test shortly. 

    Good work :)

     

    Tested stuff

    pub/sub between scenes in emulator - OK

    No more error about scene/11 - OK

     

    Still looks that iOSLocator.lua it is not updated.

    this looks to be the getValue for a sensor

    Please login or register to see this code.

    howto set a value?

    Edited by jompa68
    Link to comment
    Share on other sites

  • Topic Author
  • On 3/7/2019 at 5:15 AM, jompa68 said:

    Great, will test shortly. 

    Good work :)

     

    Tested stuff

    pub/sub between scenes in emulator - OK

    No more error about scene/11 - OK

     

    Still looks that iOSLocator.lua it is not updated.

    this looks to be the getValue for a sensor

    Please login or register to see this code.

    howto set a value?

    Thanks. I seemed to have failed to push ioslocator - done now.

    How to set values and set off triggers I'm still looking for the best solution. 

    For now we can do like this in the emulator

    Please login or register to see this code.

    I just add the prefix "fibaro:" to the command and let Lua run it.

    This list of commands can also be an argument to HC2.registerScene, see the 'sceneTest' example.

    Probably we would like to have some more high-level commands, like breach a motion sensor with auto-safe after x sec etc.

    Inside ER we can post events like before.

     

    Oh, and there is a new GUI

    When the emulator runs you can also open the web browser at 

    Please login or register to see this link.

     and there is a simple GUI to list scenes and devices and set off some triggers. The GUI will be developed with more features.

    Edited by jgab
    Link to comment
    Share on other sites

    @jgab did test Hue.monitor in new ER 2.0 B1 and still the same error message

    Please login or register to see this code.

    Could it be some strange response from bridge?

     

    Link to comment
    Share on other sites

  • Topic Author
  • 13 minutes ago, jompa68 said:

    @jgab did test Hue.monitor in new ER 2.0 B1 and still the same error message

    Please login or register to see this code.

    Could it be some strange response from bridge?

     

    What baffles me is that we didn't manage to get any log output of the URL.

    Can you change the to self.request function in the Hue section to this (just adding the url to the error handler)

    Please login or register to see this code.

    and run so we hopefully can see what url causes the error. It's seems like it is a bad url behind the "bad file descriptor". Don't think it depends on the payload.

    Link to comment
    Share on other sites

  • Topic Author
  • 16 minutes ago, jompa68 said:

    Please login or register to see this code.

    masked last part of username

    It's trying to get a light with the Hue id 1, does it exist?

    Call Hue.dump() before you do monitor. It will list all the Hue resources with name and id.

    (Btw, we can move this discussion the the ER thread because it is not emulator specific)

     

    Link to comment
    Share on other sites

    Got this error after new 2.0 B2 version

    Please login or register to see this code.

     

    Edited by jompa68
    removed test scene from HC2.lua
    Link to comment
    Share on other sites

  • Topic Author
  • 1 hour ago, jompa68 said:

    Got this error after new 2.0 B2 version

    Please login or register to see this code.

     

     

    Did you fix it? What was the error? It seems to be something in the json.decode that crashed, but the error message isn't very clear...

    Edited by jgab
    Link to comment
    Share on other sites

    No, did not have this before installed new version B2.
    I will check more tonight when back home.

    Link to comment
    Share on other sites

  • Topic Author
  • 19 minutes ago, jompa68 said:

    No, did not have this before installed new version B2.
    I will check more tonight when back home.

    Ok, it crashed in json.decode, that I can see from "Error loading rules:C:\zerobranestudio\myprograms\EventRunner\HC2.lua:1984:" but that is not so helpful. I have pushed a new version of HC2.lua that will also give the line where json.decode was called from - try that one.

     

    Link to comment
    Share on other sites

    Join the conversation

    You can post now and register later. If you have an account, sign in now to post with your account.

    Guest
    Reply to this topic...

    ×   Pasted as rich text.   Paste as plain text instead

      Only 75 emoji are allowed.

    ×   Your link has been automatically embedded.   Display as a link instead

    ×   Your previous content has been restored.   Clear editor

    ×   You cannot paste images directly. Upload or insert images from URL.

    ×
    ×
    • Create New...