Epever uphi modbus

I’m facing an issue with my Home Assistant setup involving a Modbus integration with the Epever UP HI inverter. Specifically, the entity representing the battery state on my dashboard displays ‘0’ instead of ‘Normal’. Here’s the relevant sensor template configuration I’m using:

sensor:

  • platform: template
    sensors:
    estado_da_bateria_text:
    friendly_name: “Battery State”
    value_template: >
    {% set state = states(‘sensor.modbus_hub_Estado_da_Bateria_10_0x3553’) | int %}
    {% set d0 = state & 1 %}
    {% set d1 = (state & 2) >> 1 %}
    {% set d2 = (state & 4) >> 2 %}
    {% set d3 = (state & 8) >> 3 %}
    {% set d4 = (state & 16) >> 4 %}

      {% if d0 == 0 and d1 == 0 and d2 == 0 and d3 == 0 and d4 == 0 %}
        Normal
      {% elif d0 != 0 %}
        Overvoltage
      {% elif d1 != 0 %}
        Undervoltage
      {% elif d2 != 0 %}
        Over discharge
      {% elif d4 != 0 %}
        Fault
      {% else %}
        Unknown
      {% endif %}
    

Despite this configuration, the entity on the dashboard displays ‘0’ instead of ‘Normal’. The error reported by Home Assistant is:

Invalid config for ‘template’ from integration ‘sensor’ at configuration.yaml, line 194: invalid template (TemplateSyntaxError: unexpected char ‘&’ at 98)

How can I troubleshoot and resolve this issue?