Repository: pymodbus-dev/pymodbus

Issue:
pymodbus.simulator does not start
<!--

Before opening a new issue, make sure you do the following:
    * check that your issue isn't already filed: https://github.com/pymodbus-dev/pymodbus/issues
    * check the discussions forum https://github.com/pymodbus-dev/pymodbus/discussions
    * prepare a short, runnable example that reproduce the issue with the latest development version of Pymodbus

    Before opening a new issue, make sure you do the following
-->

### Versions


* Python: 3.10.9
* OS: Fedora 36
* Pymodbus: head of dev branch, commit 217469a234bc023a660acb9c448900288131022b

### Description
* start pymodbus.simulator, using the included setup.json file
* stops with error message
```
pymodbus.simulator --json_file pymodbus/server/simulator/setup.json 
22:17:54 INFO  logging:74 Start simulator
Traceback (most recent call last):
  File "/usr/bin/pymodbus.simulator", line 33, in <module>
    sys.exit(load_entry_point('pymodbus==3.1.3', 'console_scripts', 'pymodbus.simulator')())
  File "/home/chkr/projects/pymodbus/pymodbus/server/simulator/main.py", line 118, in main
    asyncio.run(run_main(), debug=True)
  File "/usr/lib64/python3.10/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/usr/lib64/python3.10/asyncio/base_events.py", line 649, in run_until_complete
    return future.result()
  File "/home/chkr/projects/pymodbus/pymodbus/server/simulator/main.py", line 112, in run_main
    task = ModbusSimulatorServer(**cmd_args)
  File "/home/chkr/projects/pymodbus/pymodbus/server/simulator/http_server.py", line 142, in __init__
    device, custom_actions_dict or None
UnboundLocalError: local variable 'custom_actions_dict' referenced before assignment
```
* problem was introduced by commit 4d70ba559eeab598bffcdb241452a6aaaae30834 , PR #1369

### Comments
* after I reverted that commit, the simulator started
 * however, the web GUI did not show any registers and I was unable to retrieve the values of any register or coil (probably I misinterpreted setup.json) 
   * Should there be some data available with the default setup.json? Should the web GUI show some registers?
   * If yes, do you have an example how I can access them (e.g. via python.console)?



Discussion:
What a precise analysis, I am pleasantly surprised (NOT over the bug, but just how easy you made it to repair it).

The WEB GUI did show registers and allowed you to change values etc, but I have not tested it for a while. Btw. I am working on some extended documentation with web screen prints.

Resolving patch:
diff --git a/pymodbus/server/simulator/http_server.py b/pymodbus/server/simulator/http_server.py
--- a/pymodbus/server/simulator/http_server.py
+++ b/pymodbus/server/simulator/http_server.py
@@ -131,6 +131,8 @@ def __init__(
         if custom_actions_module:
             actions_module = importlib.import_module(custom_actions_module)
             custom_actions_dict = actions_module.custom_actions_dict
+        else:
+            custom_actions_dict = None
         server = setup["server_list"][modbus_server]
         server["loop"] = asyncio.get_running_loop()
         if server["comm"] != "serial":
@@ -502,8 +504,8 @@ async def build_json_server(self, params, json_dict):
 
     def helper_build_filter(self, params):
         """Build list of registers matching filter."""
-        range_start = params.get("range_start", -1)
-        range_stop = params.get("range_stop", range_start)
+        range_start = int(params.get("range_start", -1))
+        range_stop = int(params.get("range_stop", range_start))
         reg_action = int(params["action"])
         reg_writeable = "writeable" in params
         reg_type = int(params["type"])
diff --git a/pymodbus/server/simulator/main.py b/pymodbus/server/simulator/main.py
--- a/pymodbus/server/simulator/main.py
+++ b/pymodbus/server/simulator/main.py
@@ -109,6 +109,8 @@ def get_commandline():
 async def run_main():
     """Run server async."""
     cmd_args = get_commandline()
+    cmd_args["http_port"] = 8081
+    cmd_args["json_file"] = "./pymodbus/server/simulator/setup.json"
     task = ModbusSimulatorServer(**cmd_args)
     await task.run_forever()
 


Test patch: