Troubleshooting KeyError 'SIGENERGY2MQTT_HASS_ENABLED' In Sigenergy2mqtt

by ADMIN 73 views
Iklan Headers

Encountering a KeyError: 'SIGENERGY2MQTT_HASS_ENABLED' error while using the sigenergy2mqtt add-on can be frustrating. This comprehensive guide breaks down the error, explains the root causes, and provides detailed solutions to get your system back on track. We'll explore common scenarios, examine the logs, and offer step-by-step instructions to resolve this issue.

Understanding the Error

The error message KeyError: 'SIGENERGY2MQTT_HASS_ENABLED' indicates that the sigenergy2mqtt application is trying to access an environment variable named SIGENERGY2MQTT_HASS_ENABLED, but it cannot find it. This variable is crucial for determining whether the integration with Home Assistant is enabled. When the application starts, it checks for this environment variable to configure its behavior. If the variable is missing, the application throws a KeyError and fails to start.

Key Components Involved

  • sigenergy2mqtt: This is the core application responsible for collecting data from your Sigenergy system and publishing it to an MQTT broker. It acts as a bridge between your energy devices and your Home Automation system.
  • Environment Variables: These are dynamic named values that can affect the way running processes will behave on a computer. They are part of the environment in which a process runs. In this case, SIGENERGY2MQTT_HASS_ENABLED is used to tell the application whether to enable Home Assistant integration.
  • Home Assistant (HASS): A popular open-source home automation platform that integrates with various devices and services. The sigenergy2mqtt add-on can send data to Home Assistant for monitoring and control.
  • MQTT Broker: A messaging server that uses the MQTT protocol to facilitate communication between devices and applications. sigenergy2mqtt uses an MQTT broker to publish data, which can then be consumed by Home Assistant or other systems.

Analyzing the Logs

The provided logs give valuable clues about the state of the system and the sequence of events leading to the error. Let's break down the relevant parts:

s6-rc: info: service s6rc-oneshot-runner: starting
s6-rc: info: service s6rc-oneshot-runner successfully started
s6-rc: info: service fix-attrs: starting
s6-rc: info: service fix-attrs successfully started
s6-rc: info: service legacy-cont-init: starting
s6-rc: info: service legacy-cont-init successfully started
s6-rc: info: service legacy-services: starting
services-up: info: copying legacy longrun sigenergy2mqtt (no readiness notification)
s6-rc: info: service legacy-services successfully started
[09:54:22] INFO: Configuring...
[09:54:22] INFO: No configuration file found, using default configuration
[09:54:23] INFO: MQTT service available, using host: core-mosquitto, port: 1883, username: addons
[09:54:23] INFO: Checking for MQTT override configuration...
[09:54:23] INFO: MQTT configured, using username: addons
[09:54:23] INFO: INFO: MQTT configured, using host: core-mosquitto, port: 1883, username: addons
[09:54:24] INFO: Read-only mode enabled, no write operations can be performed.
[09:54:25] INFO: Metrics disabled, not publishing metrics to Home Assistant.
[09:54:25] INFO: Starting sigenergy2mqtt service...
[09:54:25] INFO:  -> Parameter: --hass-enabled
[09:54:25] INFO:  -> Parameter: --mqtt-broker=core-mosquitto
[09:54:25] INFO:  -> Parameter: --mqtt-port=1883
[09:54:25] INFO:  -> Parameter: --mqtt-username=addons
[09:54:25] INFO:  -> Parameter: --mqtt-password=[REDACTED]
[09:54:25] INFO:  -> Parameter: --modbus-host=192.168.1.13
[09:54:25] INFO:  -> Parameter: --modbus-inverter-device-id
[09:54:25] INFO:  -> Parameter: 1
[09:54:25] INFO:  -> Parameter: --modbus-readonly
[09:54:25] INFO:  -> Parameter: --no-metrics
2025-08-02 09:54:25,732 INFO  __init__:24 Release 2025.8.2
Traceback (most recent call last):
  File "/root/.local/bin/sigenergy2mqtt", line 3, in <module>
    from sigenergy2mqtt.__main__ import main
  File "/root/.local/share/pipx/venvs/sigenergy2mqtt/lib/python3.12/site-packages/sigenergy2mqtt/__main__.py", line 1, in <module>
    from sigenergy2mqtt.config import Config
  File "/root/.local/share/pipx/venvs/sigenergy2mqtt/lib/python3.12/site-packages/sigenergy2mqtt/config/__init__.py", line 487, in <module>
    apply_cli_to_env(arg, override)
  File "/root/.local/share/pipx/venvs/sigenergy2mqtt/lib/python3.12/site-packages/sigenergy2mqtt/config/__init__.py", line 436, in apply_cli_to_env
    if value != os.environ[variable]:
                ~~~~~~~~~~^^^^^^^^^^
  File "<frozen os>", line 714, in __getitem__
KeyError: 'SIGENERGY2MQTT_HASS_ENABLED'
[07:54:25] WARNING: Halt add-on with exit code 1
  • The logs indicate that the add-on is starting up and using the default configuration because no configuration file was found. This is an important clue because it suggests that the environment variables are not being set through a configuration file.
  • The application successfully connects to the MQTT broker using the provided credentials.
  • The logs show that the --hass-enabled parameter is being passed to the sigenergy2mqtt service. This suggests that the intention is to enable Home Assistant integration. However, the application is failing because the corresponding environment variable is missing.
  • The traceback clearly points to the KeyError: 'SIGENERGY2MQTT_HASS_ENABLED' within the config/__init__.py file. This confirms that the application is trying to access this environment variable and failing.

Possible Causes

Several factors can contribute to this error. Let's explore the most common ones:

  1. Missing Environment Variable: The most likely cause is that the SIGENERGY2MQTT_HASS_ENABLED environment variable is not set in the environment where the sigenergy2mqtt application is running. This can happen if the variable was not defined in the add-on configuration or if there was an issue during the setup process.
  2. Incorrect Add-on Configuration: If you are using a configuration file, there might be an error in the way the environment variables are defined. A typo or incorrect syntax can prevent the variable from being set correctly.
  3. Add-on Update Issues: Sometimes, an update to the sigenergy2mqtt add-on can introduce changes that require specific environment variables to be set. If you recently updated the add-on, this could be the cause.
  4. Conflicting Configurations: In some cases, other add-ons or system configurations might interfere with the environment variables used by sigenergy2mqtt. This is less common but can occur in complex setups.

Troubleshooting Steps

Now that we understand the error and its potential causes, let's dive into the troubleshooting steps. Follow these instructions carefully to identify and resolve the issue.

Step 1: Check Add-on Configuration

Since the logs indicate that the add-on is using the default configuration, the first step is to ensure that the necessary environment variables are set correctly in the add-on configuration. Here’s how you can do it:

  1. Access Home Assistant: Open your Home Assistant interface in a web browser.

  2. Navigate to Add-ons: Go to the Supervisor or Add-ons section in the Home Assistant sidebar.

  3. Find sigenergy2mqtt: Locate the sigenergy2mqtt add-on in your list of installed add-ons.

  4. Open Configuration: Click on the add-on to open its details, and then go to the “Configuration” tab.

  5. Check Environment Variables: Look for a section where you can define environment variables. Ensure that SIGENERGY2MQTT_HASS_ENABLED is listed and set to true if you want to enable Home Assistant integration. If it’s missing, add it.

    env_vars:
      SIGENERGY2MQTT_HASS_ENABLED: "true"
    
  6. Save Changes: Save the configuration changes.

  7. Restart Add-on: Restart the sigenergy2mqtt add-on for the changes to take effect.

Step 2: Verify Environment Variable in the Container

Sometimes, the environment variable might be set in the add-on configuration but not correctly passed to the container running the sigenergy2mqtt application. To verify this, you can try to access the container and check the environment variables directly. However, this requires some familiarity with Docker and command-line interfaces.

  1. Install the “SSH & Web Terminal” Add-on: If you don’t have it already, install the “SSH & Web Terminal” add-on from the Home Assistant add-on store. This will allow you to access the Home Assistant command line.

  2. Start the Add-on: Start the “SSH & Web Terminal” add-on.

  3. Connect to the Terminal: Open the web terminal from the add-on’s interface.

  4. Identify the Container ID: Use the docker ps command to list the running containers and find the container ID for sigenergy2mqtt. The name might be something like hassio_addon_xxxxxxxx_sigenergy2mqtt.

    docker ps
    
  5. Access the Container: Use the docker exec command to access the container’s shell. Replace <container_id> with the actual container ID you found in the previous step.

    docker exec -it <container_id> bash
    
  6. Check Environment Variables: Inside the container, use the printenv command to list all environment variables. Look for SIGENERGY2MQTT_HASS_ENABLED in the list.

    printenv | grep SIGENERGY2MQTT_HASS_ENABLED
    
    • If the variable is not listed, it means it’s not being passed to the container. You’ll need to revisit Step 1 and ensure that the add-on configuration is correctly setting the environment variable.
  7. Exit the Container: Type exit to leave the container’s shell.

Step 3: Review Add-on Logs for Clues

Sometimes, additional information in the add-on logs can provide more context about the issue. Review the logs for any error messages or warnings that might indicate why the environment variable is not being set correctly.

  1. Access Add-on Logs: Go back to the sigenergy2mqtt add-on details in Home Assistant.
  2. Open the Logs Tab: Click on the “Logs” tab.
  3. Examine Logs: Look for any error messages or warnings related to environment variables or configuration issues. Pay close attention to any messages that appear during the add-on startup process.

Step 4: Check for Conflicting Configurations

In rare cases, other add-ons or system configurations might interfere with the environment variables used by sigenergy2mqtt. If you have recently installed or updated other add-ons, try temporarily disabling them to see if they are causing the issue.

  1. Disable Other Add-ons: Go to the Supervisor or Add-ons section in Home Assistant.
  2. Disable Add-ons: For each add-on you suspect might be causing a conflict, click on the add-on details and then click the “Disable” button.
  3. Restart sigenergy2mqtt: After disabling the add-ons, restart the sigenergy2mqtt add-on.
  4. Check if the Error Persists: See if the KeyError is resolved. If it is, then one of the disabled add-ons is likely the cause. You can then re-enable the add-ons one by one to identify the specific conflict.

Step 5: Reinstall the Add-on

If none of the above steps resolve the issue, a more drastic measure is to reinstall the sigenergy2mqtt add-on. This can help ensure that all files are correctly installed and that there are no corrupted configurations.

  1. Uninstall the Add-on: Go to the sigenergy2mqtt add-on details in Home Assistant.
  2. Click Uninstall: Click the “Uninstall” button.
  3. Reinstall the Add-on: Go to the add-on store and reinstall the sigenergy2mqtt add-on.
  4. Configure Add-on: After reinstalling, configure the add-on with the necessary settings, including the SIGENERGY2MQTT_HASS_ENABLED environment variable.
  5. Restart Add-on: Restart the add-on for the changes to take effect.

Step 6: Seek Community Support

If you’ve tried all the troubleshooting steps and are still facing the issue, it’s time to seek help from the community. The Home Assistant community forums and the sigenergy2mqtt project’s issue tracker are excellent places to ask for assistance.

  1. Home Assistant Forums: Post a detailed description of your issue, including the error message, the steps you’ve taken, and any relevant logs, on the Home Assistant forums.
  2. sigenergy2mqtt Issue Tracker: Check the issue tracker for the sigenergy2mqtt project on GitHub or GitLab. Someone else might have encountered the same problem, and there might be a solution or workaround available. If not, you can open a new issue with your details.

Conclusion

The KeyError: 'SIGENERGY2MQTT_HASS_ENABLED' error can be a stumbling block when setting up sigenergy2mqtt, but by systematically following these troubleshooting steps, you can identify and resolve the issue. Remember to check your add-on configuration, verify environment variables, review logs, and consider potential conflicts. If all else fails, the community is there to help. By addressing this error, you’ll be one step closer to fully integrating your Sigenergy system with Home Assistant and taking control of your home energy management. Keep your configurations tidy, and your troubleshooting skills sharp, and you'll be back on track in no time! If you guys have any additional questions or run into snags, don't hesitate to reach out to the community or consult the official documentation. Happy automating!