Template Error state.split

I get the error below for the 3 voltage sensors I created with Node Red. The results are good, but I do not understand the error or how to fix it the code. I would appreciate any ideas.

Thanks

Error:

Logger: homeassistant.components.template.template_entity
Source: components/template/template_entity.py:72
Integration: Template (documentation, issues)
First occurred: 1:39:45 PM (3 occurrences)
Last logged: 1:39:45 PM

TemplateError('UndefinedError: list object has no element 1') while processing template 'Template("{{ states.sensor.voltage_em_1.state.split('.')[0] + '.' + states.sensor.voltage_em_1.state.split('.')[1][:1] }}")' for attribute '_attr_state' in entity 'sensor.electric_voltage_l1_round'
TemplateError('UndefinedError: list object has no element 1') while processing template 'Template("{{ states.sensor.voltage.state.split('.')[0] + '.' + states.sensor.voltage.state.split('.')[1][:1] }}")' for attribute '_attr_state' in entity 'sensor.electric_voltage_l2_round'
TemplateError('UndefinedError: list object has no element 1') while processing template 'Template("{{ states.sensor.voltage_2.state.split('.')[0] + '.' + states.sensor.voltage_2.state.split('.')[1][:1] }}")' for attribute '_attr_state' in entity 'sensor.electric_voltage_l3_round'

Code:

- platform: template
    sensors:
      electric_voltage_l1_round:
        value_template: "{{ states.sensor.voltage_em_1.state.split('.')[0] + '.' + states.sensor.voltage_em_1.state.split('.')[1][:2] }}"
        friendly_name: 'EM L1'
        unit_of_measurement: 'V'

      electric_voltage_l2_round:
        value_template: "{{ states.sensor.voltage.state.split('.')[0] + '.' + states.sensor.voltage.state.split('.')[1][:2] }}"
        friendly_name: 'EM L2'
        unit_of_measurement: 'V'

      electric_voltage_l3_round:
        value_template: "{{ states.sensor.voltage_2.state.split('.')[0] + '.' + states.sensor.voltage_2.state.split('.')[1][:2] }}"
        friendly_name: 'EM L3'
        unit_of_measurement: 'V'

Result:

- platform: template
    sensors:
      electric_voltage_l1_round:
        value_template: "211.65"
        friendly_name: 'EM L1'
        unit_of_measurement: 'V'

      electric_voltage_l2_round:
        value_template: "213.90"
        friendly_name: 'EM L2'
        unit_of_measurement: 'V'

      electric_voltage_l3_round:
        value_template: "212.69"
        friendly_name: 'EM L3'
        unit_of_measurement: 'V'

image

The error message is telling you the template failed to access a value in a list because it doesn’t exist.

Instead of helping you debug the template, I think it should be completely redesigned. If you are simply trying to round the value to one decimal place, use round(1) instead of the hacky thing the template is currently doing.

value_template: "{{ states('sensor.voltage_em_1') | round(1) }}"

If you want to eliminate the decimal portion use round.

value_template: "{{ states('sensor.voltage_em_1') | round }}"

If you want to simply discard the decimal portion, use int.

value_template: "{{ states('sensor.voltage_em_1') | int }}"

If it’s none of those, then let us know how you want to reformat the value and we’ll show you a simpler way than the technique your template currently uses.

2 Likes

Thank you! I used the one with 1 decimal point.

1 Like