TasmotaManager/docs/summaries/template_activation_fix_summary.md
2025-10-28 00:21:08 +00:00

3.8 KiB

Template Activation Fix Summary

Issue Description

The issue was that templates were not being properly activated after being set. In the Tasmota web UI, there's an "Activate" checkbox that needs to be checked when applying a template. Without checking this box, the template is set but not activated.

In our code, we were setting the template using the Template command, but we weren't activating it, which is equivalent to not checking the "Activate" box in the web UI.

Changes Made

1. Understanding the Template Activation Process

In Tasmota, to fully activate a template, three steps are required:

  1. Set the template using the Template command
  2. Set the module to 0 (Template module) using the Module 0 command
  3. Restart the device using the Restart 1 command

2. Modifications to check_and_update_template Method

We modified the check_and_update_template method in TasmotaManager.py to include the template activation steps. Changes were made in two places:

When a template is updated:

# URL encode the template value
import urllib.parse
encoded_value = urllib.parse.quote(template_value)
url = f"http://{ip}/cm?cmnd=Template%20{encoded_value}"

response = requests.get(url, timeout=5)
if response.status_code == 200:
    self.logger.info(f"{name}: Template updated successfully")
    
    # Activate the template by setting module to 0 (Template module)
    self.logger.info(f"{name}: Activating template by setting module to 0")
    module_url = f"http://{ip}/cm?cmnd=Module%200"
    module_response = requests.get(module_url, timeout=5)
    
    if module_response.status_code == 200:
        self.logger.info(f"{name}: Module set to 0 successfully")
        
        # Restart the device to apply the template
        self.logger.info(f"{name}: Restarting device to apply template")
        restart_url = f"http://{ip}/cm?cmnd=Restart%201"
        restart_response = requests.get(restart_url, timeout=5)
        
        if restart_response.status_code == 200:
            self.logger.info(f"{name}: Device restart initiated successfully")
            template_updated = True
        else:
            self.logger.error(f"{name}: Failed to restart device")
    else:
        self.logger.error(f"{name}: Failed to set module to 0")
else:
    self.logger.error(f"{name}: Failed to update template")

When a device name is updated:

Similar changes were made when a device name is updated to match a template. After successfully updating the device name, we added code to set the module to 0 and restart the device.

3. Test Script

A test script test_template_activation.py was created to verify that templates are properly activated. The script:

  1. Gets a test device from current.json
  2. Gets a template from network_configuration.json
  3. Sets the template on the device and activates it
  4. Verifies that the template was properly activated by checking:
    • The module is set to 0 (Template module)
    • The template matches the expected value

How to Test

To test the template activation fix:

  1. Run the test script:

    python3 test_template_activation.py
    
  2. The script will output information about the template activation process and verify that the template was properly activated.

  3. You can also manually test by:

    • Running TasmotaManager with a device that has a template defined in network_configuration.json
    • Checking the device's module and template after TasmotaManager has processed it

Expected Results

After the fix, when a template is set or a device name is updated to match a template:

  1. The template should be properly set on the device
  2. The module should be set to 0 (Template module)
  3. The device should restart to apply the template

This ensures that templates are fully activated, equivalent to checking the "Activate" box in the Tasmota web UI.