SONOFF SWV-ZFE Water Valve: Invalid Jinja2 templates for 24/30/180 hours records sensors
What happened?
Device: SONOFF Zigbee smart water valve (SWV-ZFE) Model ID: SWV-ZFE Firmware: 1.0.7
Issue: When using MQTT discovery, the device publishes invalid Jinja2 templates for the following sensors:
- 24_hours_records
- 30_days_records
- 180_days_records
Error in Home Assistant: TemplateSyntaxError: expected token 'end of print statement', got '_hours_records'
Root cause: The value_template uses property names that start with numbers: {{ value_json.24_hours_records | ... }}
This is invalid Jinja2 syntax. It should be: {{ value_json['24_hours_records'] | ... }}
Expected behavior: Either fix the template syntax or disable these sensors in MQTT discovery.
What did you expect to happen?
No response
How to reproduce it (minimal and precise)
No response
Zigbee2MQTT version
2.10.0
Adapter firmware version
Firmware: core: v3.3.1 / zigbee: 20240710
Adapter
SLZB-06U
Setup
HA Green
Device database.db entry
No response
Debug log
No response
Notes
No response
cc @cubez2mdeveloper
Opened #31930
The root cause is fairly small: the ha discovery templates were being emitted as {{ value_json.<property> }} even when the property name was not a valid Jinja identifier, as @rvsb1250 said. For names like 24_hours_records, ha ends up with broken templates and rather sensibly refuses to make much of them.
The fix keeps the existing payload keys and entity identities intact, and only switches the template access syntax to bracket notation where needed: {{ value_json["24_hours_records"] }}. I also added regression coverage for the simple case plus climate, cover, and fan templates.
Opened #31930
The root cause is fairly small: the ha discovery templates were being emitted as
{{ value_json.<property> }}even when the property name was not a valid Jinja identifier, as @rvsb1250 said. For names like24_hours_records, ha ends up with broken templates and rather sensibly refuses to make much of them.The fix keeps the existing payload keys and entity identities intact, and only switches the template access syntax to bracket notation where needed:
{{ value_json["24_hours_records"] }}. I also added regression coverage for the simple case plus climate, cover, and fan templates.
Hi @yo3gnd,
Thanks for looking into this and for submitting the PR.
I have checked the issue and agree with your analysis. The problem is caused by expose property names that start with a number, such as 24_hours_records. When Zigbee2MQTT generates the Home Assistant discovery template using dot notation, it becomes invalid in Home Assistant/Jinja. Bracket notation should be used for these keys instead.
This is not a firmware issue, but a compatibility issue in the generated Home Assistant discovery template.
Thanks again for the fix. I will keep following the PR and help review/test from the SONOFF side if needed.
Sorry for the delayed response.
Thanks to everyone for the quick response.
This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 7 days