davidusb-geek / emhass

emhass: Energy Management for Home Assistant, is a Python module designed to optimize your home energy interfacing with Home Assistant.
MIT License
284 stars 54 forks source link

web_server - ERROR - Exception on /action/publish-data #199

Closed BDVGitHub closed 7 months ago

BDVGitHub commented 7 months ago

Describe the bug When running curl -i -H ’Content-Type:application/json’ -X POST -d ’{}’ http://localhost:5000/action/dayahead-optim I get an internal server error 500

In Docker I have edited config_emhass.yaml and secrets_emhass.yaml

In Home Assistant I put in configuration.yaml

shell_command: dayahead_optim: "curl -i -H \"Content-Type:application/json\" -X POST -d '{}' http://localhost:5000/action/dayahead-optim" publish_data: "curl -i -H \"Content-Type:application/json\" -X POST -d '{}' http://localhost:5000/action/publish-data"

and in automations.yaml

To Reproduce root@MiniPCServer:/home/brecht# docker exec -it DockerEMHASS bash root@6185e47f85ef:/app# curl -i -H 'Content-Type:application/json' -X POST -d '{}' http://localhost:5000/action/dayahead-optim HTTP/1.1 500 INTERNAL SERVER ERROR Content-Length: 265 Content-Type: text/html; charset=utf-8 Date: Wed, 14 Feb 2024 20:58:38 GMT Server: waitress

<!doctype html>

500 Internal Server Error

Internal Server Error

The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.

**Expected behavior** What do I need to do to run curl -i -H 'Content-Type:application/json' -X POST -d '{}' http://localhost:5000/action/dayahead-optim without error? **Home Assistant installation type** - Home Assistant Supervised - Home Assistant Core **Your hardware** - OS: Linux (Debian) - Architecture: x86_64 **EMHASS installation type** - Docker Standalone **Additional context** Docker info lient: Docker Engine - Community Version: 25.0.3 Context: default Debug Mode: false Plugins: buildx: Docker Buildx (Docker Inc.) Version: v0.12.1 Path: /usr/libexec/docker/cli-plugins/docker-buildx compose: Docker Compose (Docker Inc.) Version: v2.24.5 Path: /usr/libexec/docker/cli-plugins/docker-compose Server: Containers: 14 Running: 12 Paused: 0 Stopped: 2 Images: 14 Server Version: 25.0.3 Storage Driver: overlay2 Backing Filesystem: extfs Supports d_type: true Using metacopy: false Native Overlay Diff: true userxattr: false Logging Driver: journald Cgroup Driver: cgroupfs Cgroup Version: 1 Plugins: Volume: local Network: bridge host ipvlan macvlan null overlay Log: awslogs fluentd gcplogs gelf journald json-file local splunk syslog Swarm: inactive Runtimes: io.containerd.runc.v2 runc Default Runtime: runc Init Binary: docker-init containerd version: ae07eda36dd25f8a1b98dfbf587313b99c0190bb runc version: v1.1.12-0-g51d5e94 init version: de40ad0 Security Options: apparmor seccomp Profile: builtin Kernel Version: 6.1.0-17-amd64 Operating System: Debian GNU/Linux 12 (bookworm) OSType: linux Architecture: x86_64 CPUs: 4 Total Memory: 7.637GiB Name: MiniPCServer ID: f5877512-ed00-4d57-8a35-5f8145d7b5f0 Docker Root Dir: /var/lib/docker Debug Mode: false Experimental: true Insecure Registries: 127.0.0.0/8 Live Restore Enabled: false log file 2024-02-14 20:29:06,597 - web_server - INFO - Launching the emhass webserver at: http://0.0.0.0:5000 2024-02-14 20:29:06,597 - web_server - INFO - Home Assistant data fetch will be performed using url: http://localhost/ 2024-02-14 20:29:06,597 - web_server - INFO - The data path is: /app/data 2024-02-14 20:29:06,598 - web_server - INFO - Using core emhass version: 0.7.7 waitress INFO Serving on http://0.0.0.0:5000 2024-02-14 20:30:00,073 - web_server - INFO - Setting up needed data 2024-02-14 20:30:00,094 - web_server - INFO - >> Publishing data... 2024-02-14 20:30:00,094 - web_server - INFO - Publishing data to HASS instance 2024-02-14 20:30:00,104 - web_server - ERROR - Exception on /action/publish-data [POST] Traceback (most recent call last): File "/usr/local/lib/python3.11/site-packages/urllib3/connection.py", line 198, in _new_conn sock = connection.create_connection( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/urllib3/util/connection.py", line 85, in create_connection raise err File "/usr/local/lib/python3.11/site-packages/urllib3/util/connection.py", line 73, in create_connection sock.connect(sa) ConnectionRefusedError: [Errno 111] Connection refused The above exception was the direct cause of the following exception: Traceback (most recent call last): File "/usr/local/lib/python3.11/site-packages/urllib3/connectionpool.py", line 793, in urlopen response = self._make_request( ^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/urllib3/connectionpool.py", line 496, in _make_request conn.request( File "/usr/local/lib/python3.11/site-packages/urllib3/connection.py", line 400, in request self.endheaders() File "/usr/local/lib/python3.11/http/client.py", line 1281, in endheaders self._send_output(message_body, encode_chunked=encode_chunked) File "/usr/local/lib/python3.11/http/client.py", line 1041, in _send_output self.send(msg) File "/usr/local/lib/python3.11/http/client.py", line 979, in send self.connect() File "/usr/local/lib/python3.11/site-packages/urllib3/connection.py", line 238, in connect self.sock = self._new_conn() ^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/urllib3/connection.py", line 213, in _new_conn raise NewConnectionError( urllib3.exceptions.NewConnectionError: : Failed to establish a new connection: [Errno 111] Connection refused The above exception was the direct cause of the following exception: Traceback (most recent call last): File "/usr/local/lib/python3.11/site-packages/requests/adapters.py", line 486, in send resp = conn.urlopen( ^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/urllib3/connectionpool.py", line 847, in urlopen retries = retries.increment( ^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/urllib3/util/retry.py", line 515, in increment raise MaxRetryError(_pool, url, reason) from reason # type: ignore[arg-type] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='localhost', port=80): Max retries exceeded with url: /api/states/sensor.p_pv_forecast (Caused by NewConnectionError(': Failed to establish a new connection: [Errno 111] Connection refused')) During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/usr/local/lib/python3.11/site-packages/flask/app.py", line 1463, in wsgi_app response = self.full_dispatch_request() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/flask/app.py", line 872, in full_dispatch_request rv = self.handle_user_exception(e) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/flask/app.py", line 870, in full_dispatch_request rv = self.dispatch_request() ^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/flask/app.py", line 855, in dispatch_request return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args) # type: ignore[no-any-return] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/app/src/emhass/web_server.py", line 53, in action_call _ = publish_data(input_data_dict, app.logger) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/emhass-0.7.7-py3.11.egg/emhass/command_line.py", line 474, in publish_data input_data_dict['rh'].post_data(opt_res_latest['P_PV'], idx_closest, File "/usr/local/lib/python3.11/site-packages/emhass-0.7.7-py3.11.egg/emhass/retrieve_hass.py", line 332, in post_data response = post(url, headers=headers, data=json.dumps(data)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/requests/api.py", line 115, in post return request("post", url, data=data, json=json, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/requests/api.py", line 59, in request return session.request(method=method, url=url, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/requests/sessions.py", line 589, in request resp = self.send(prep, **send_kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/requests/sessions.py", line 703, in send r = adapter.send(request, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/usr/local/lib/python3.11/site-packages/requests/adapters.py", line 519, in send raise ConnectionError(e, request=request) requests.exceptions.ConnectionError: HTTPConnectionPool(host='localhost', port=80): Max retries exceeded with url: /api/states/sensor.p_pv_forecast (Caused by NewConnectionError(': Failed to establish a new connection: [Errno 111] Connection refused'))
davidusb-geek commented 7 months ago

Just test that curl command without going inside the docker container. Give it a try from a simple new terminal. Your HA url looks weird though. Is it just localhost? No 8123 port?

BDVGitHub commented 7 months ago

Hey, thanks for the advice, changed in secrets_emhass.yaml hass_url: http://localhost to hass_url: http://172.17.0.1:8123/ and is now working fine