I recently encountered an issue while configuring an automation in my smart home setup, and I wanted to share my experience in case it helps anyone else facing a similar problem. My goal was to create an automation that would charge my sonnen battery using excess power from my PV installation. While the basic version worked perfectly, I ran into trouble when trying to implement a more complex setup with conditional logic.
The Challenge
The problematic version of my automation involved a choose block to handle different scenarios based on the available excess power. The automation was supposed to send a charging command if there was excess power and notify me if there wasn’t. However, the automation became unavailable under the developer tools, and I couldn’t figure out why.
Here’s the problematic YAML snippet:
yaml
choose:
- conditions:
- condition: template
value: “{{ sensor.battery_charge_pv_watt | float > 0 }}”
sequence: - service: shell_command.shellcom_battery_charge_pv
data:
url: “http://myip/api/v2/setpoint/charge/{{ sensor.battery_charge_pv_watt | int }}”
token: “My hidden token”
- condition: template
- conditions:
- condition: template
value: “{{ sensor.battery_charge_pv_watt | float <= 0 }}”
sequence: - service: persistent_notification.create
data:
title: Error
message: “Unable to send command to charge battery: not enough excess PV power”
- condition: template
The Solution
After spending hours debugging, I realized that the issue was with how the choose block was structured. I had mistakenly placed the conditions directly under the choose block without wrapping them in a proper list format. Additionally, I wasn’t correctly referencing the sensor values in the template conditions.
Here’s the corrected YAML configuration:
yaml
choose:
- conditions:
- condition: template
value_template: “{{ sensor.battery_charge_pv_watt.state | float > 0 }}”
sequence: - service: shell_command.shellcom_battery_charge_pv
data_template:
url: “http://myip/api/v2/setpoint/charge/{{ sensor.battery_charge_pv_watt.state | int }}”
token: “My hidden token”
- condition: template
- conditions:
- condition: template
value_template: “{{ sensor.battery_charge_pv_watt.state | float <= 0 }}”
sequence: - service: persistent_notification.create
data:
title: Error
message: “Unable to send command to charge battery: not enough excess PV power”
- condition: template
Key Takeaways
- Proper YAML Structure: Ensure that your
chooseblock is correctly structured with a list of conditions and sequences. Misindentation or incorrect nesting can cause the automation to fail silently. - Template Syntax: Use
value_templateinstead ofvaluewhen working with templates in conditions. This ensures that the template is properly evaluated. - Sensor References: Make sure you’re correctly referencing sensor states using
.statewhen working with template conditions. - Debugging Tools: Utilize the developer tools in your smart home platform to check for errors and ensure that your automations are valid.
By carefully reviewing my configuration and testing each part of the automation, I was able to resolve the issue and get my PV-powered battery charging system up and running smoothly. It was a valuable learning experience, and I hope sharing my journey can help others avoid similar pitfalls!
Happy automating! ![]()