Serial text string problem

Hello
I’m trying to receive data string from my powermeter in my hassio with serial sensor

the power meter sends this string

490.74,235.27,3.05,0.68, 726.72,233.05,3.67,0.85, 208.93,235.24,1.14,0.78, 17864.96,84042.15
every second


  - platform: serial
    serial_port: /dev/ttyUSB0
    baudrate: 115200

  - platform: template
    sensors:

      my_powermeter_sensor_l1w:
        friendly_name: L1_W
        unit_of_measurement: "W"
        value_template: "{{ states('sensor.serial_sensor').split(',')[1] | float }}"
      my_powermeter_sensor_l1v:
        friendly_name: L1_V
        unit_of_measurement: "V"
        value_template: "{{ states('sensor.serial_sensor').split(',')[2] | float }}"
      my_powermeter_sensor_l1a:
        friendly_name: L1_A
        unit_of_measurement: "A"
        value_template: "{{ states('sensor.serial_sensor').split(',')[3] | float }}"
      my_powermeter_sensor_l1cosphi:
        friendly_name: L1_cosphi
        unit_of_measurement: "Q"
        value_template: "{{ states('sensor.serial_sensor').split(',')[4] | float }}"   
      my_powermeter_sensor_l2w:
        friendly_name: L2_W
        unit_of_measurement: "W"
        value_template: "{{ states('sensor.serial_sensor').split(',')[5] | float }}"
      my_powermeter_sensor_l2v:
        friendly_name: L2_V
        unit_of_measurement: "V"
        value_template: "{{ states('sensor.serial_sensor').split(',')[6] | float }}"
      my_powermeter_sensor_l2a:
        friendly_name: L2_A
        unit_of_measurement: "A"
        value_template: "{{ states('sensor.serial_sensor').split(',')[7] | float }}"
      my_powermeter_sensor_l2cosphi:
        friendly_name: L2_cosphi
        unit_of_measurement: "Q"
        value_template: "{{ states('sensor.serial_sensor').split(',')[8] | float }}"        
      my_powermeter_sensor_l3w:
        friendly_name: L3_W
        unit_of_measurement: "W"
        value_template: "{{ states('sensor.serial_sensor').split(',')[9] | float }}"
      my_powermeter_sensor_l3v:
        friendly_name: L3_V
        unit_of_measurement: "V"
        value_template: "{{ states('sensor.serial_sensor').split(',')[10] | float }}"
      my_powermeter_sensor_l3a:
        friendly_name: L3_A
        unit_of_measurement: "A"
        value_template: "{{ states('sensor.serial_sensor').split(',')[11] | float }}"
      my_powermeter_sensor_l3cosphi:
        friendly_name: L3_cosphi
        unit_of_measurement: "Q"
        value_template: "{{ states('sensor.serial_sensor').split(',')[12] | float }}" 
      my_powermeter_sensor_whout:
        friendly_name: WH_out
        unit_of_measurement: "Wh"
        value_template: "{{ states('sensor.serial_sensor').split(',')[13] | float }}"
      my_powermeter_sensor_whin:
        friendly_name: Wh_in
        unit_of_measurement: "Wh"
        value_template: "{{ states('sensor.serial_sensor').split(',')[14] | float }}"  

but in the states all I get is an unavailable
log:
2020-03-26 15:58:10 ERROR (MainThread) [homeassistant.components.template.sensor] Could not render template L2_A: UndefinedError: list object has no element 7
2020-03-26 15:58:10 ERROR (MainThread) [homeassistant.components.template.sensor] Could not render template Wh_in: UndefinedError: list object has no element 14
2020-03-26 15:58:10 ERROR (MainThread) [homeassistant.components.template.sensor] Could not render template L2_cosphi: UndefinedError: list object has no element 8
2020-03-26 15:58:10 ERROR (MainThread) [homeassistant.components.template.sensor] Could not render template L3_W: UndefinedError: list object has no element 9
2020-03-26 15:58:10 ERROR (MainThread) [homeassistant.components.template.sensor] Could not render template L1_A: UndefinedError: list object has no element 3
2020-03-26 15:58:10 ERROR (MainThread) [homeassistant.components.template.sensor] Could not render template L1_W: UndefinedError: list object has no element 1
2020-03-26 15:58:10 ERROR (MainThread) [homeassistant.components.template.sensor] Could not render template L3_V: UndefinedError: list object has no element 10
2020-03-26 15:58:10 ERROR (MainThread) [homeassistant.components.template.sensor] Could not render template L3_A: UndefinedError: list object has no element 11
2020-03-26 15:58:10 ERROR (MainThread) [homeassistant.components.template.sensor] Could not render template L1_cosphi: UndefinedError: list object has no element 4
2020-03-26 15:58:10 ERROR (MainThread) [homeassistant.components.template.sensor] Could not render template L2_W: UndefinedError: list object has no element 5
2020-03-26 15:58:10 ERROR (MainThread) [homeassistant.components.template.sensor] Could not render template L3_cosphi: UndefinedError: list object has no element 12
2020-03-26 15:58:10 ERROR (MainThread) [homeassistant.components.template.sensor] Could not render template WH_out: UndefinedError: list object has no element 13
2020-03-26 15:58:10 ERROR (MainThread) [homeassistant.components.template.sensor] Could not render template L2_V: UndefinedError: list object has no element 6
2020-03-26 15:58:10 ERROR (MainThread) [homeassistant.components.template.sensor] Could not render template L1_V: UndefinedError: list object has no element 2

thank you

Try this in the template tool at developer tools > template and share the output:

{{ states('sensor.serial_sensor') }}

Looks like it’s being read as a list instead of a string (you’d know if you see output like ['490.74', '235.27', ... ]. If that’s true, this should work:

{{ states('sensor.serial_sensor')[0] }}

That should return the first value in the list. Also, converting to float in your templates won’t do anything since it’ll just get converted back to a string anyway.

Your sample data contains 14 comma-delimited numbers. When converted to a list, the items in that list are referenced from 0 to 13. In other words, you reference the 14th item with [13] and not [14].

However, that’s not the reason for all those error messages you’re getting. It seems like sometimes the serial sensor’s value, after conversion to a list, contains nothing.

Some of the list items have a leading space (like ' 726.72'). I think the float filter is being used to remove the leading space.

Just add an availability template to the sensor and it’ll get rid of the errors. But you’ll start seeing ‘unavailable’ when the sensor doesn’t have a list.

{{ states('sensor.serial_sensor').split(',') | length > 1 }} 

or

{{ ',' in states('sensor.serial_sensor') }} 

I delete this bit of code from configuration.yaml sensor: witch did not work anyway and the data apear.

  - platform: modbus
    registers:
      - name: porttcp
        hub: mc760
        
      unit_of_measurement: °C
        slave: 1
        register: 40207

corrected code looks like this

  - platform: serial
    serial_port: /dev/ttyUSB0
    baudrate: 115200

  - platform: template
    sensors:

      my_powermeter_sensor_l3w:
        friendly_name: L3_W
        unit_of_measurement: "W"
        value_template: "{{ states('sensor.serial_sensor').split(',')[0] | float }}"
      my_powermeter_sensor_l3v:
        friendly_name: L3_V
        unit_of_measurement: "V"
        value_template: "{{ states('sensor.serial_sensor').split(',')[1] | float }}"
      my_powermeter_sensor_l3a:
        friendly_name: L3_A
        unit_of_measurement: "A"
        value_template: "{{ states('sensor.serial_sensor').split(',')[2] | float }}"
      my_powermeter_sensor_l3cosphi:
        friendly_name: L3_cosphi
        unit_of_measurement: "Q"
        value_template: "{{ states('sensor.serial_sensor').split(',')[3] | float }}" 
        
      my_powermeter_sensor_l2w:
        friendly_name: L2_W
        unit_of_measurement: "W"
        value_template: "{{ states('sensor.serial_sensor').split(',')[4] | float }}"
      my_powermeter_sensor_l2v:
        friendly_name: L2_V
        unit_of_measurement: "V"
        value_template: "{{ states('sensor.serial_sensor').split(',')[5] | float }}"
      my_powermeter_sensor_l2a:
        friendly_name: L2_A
        unit_of_measurement: "A"
        value_template: "{{ states('sensor.serial_sensor').split(',')[6] | float }}"
      my_powermeter_sensor_l2cosphi:
        friendly_name: L2_cosphi
        unit_of_measurement: "Q"
        value_template: "{{ states('sensor.serial_sensor').split(',')[7] | float }}" 
        
      my_powermeter_sensor_l1w:
        friendly_name: L1_W
        unit_of_measurement: "W"
        value_template: "{{ states('sensor.serial_sensor').split(',')[8] | float }}"
      my_powermeter_sensor_l1v:
        friendly_name: L1_V
        unit_of_measurement: "V"
        value_template: "{{ states('sensor.serial_sensor').split(',')[9] | float }}"
      my_powermeter_sensor_l1a:
        friendly_name: L1_A
        unit_of_measurement: "A"
        value_template: "{{ states('sensor.serial_sensor').split(',')[10] | float }}"
      my_powermeter_sensor_l1cosphi:
        friendly_name: L1_cosphi
        unit_of_measurement: "Q"
        value_template: "{{ states('sensor.serial_sensor').split(',')[11] | float }}" 

      my_powermeter_sensor_whout:
        friendly_name: WH_out
        unit_of_measurement: "Wh"
        value_template: "{{ states('sensor.serial_sensor').split(',')[12] | float }}"
      my_powermeter_sensor_whin:
        friendly_name: Wh_in
        unit_of_measurement: "Wh"
        value_template: "{{ states('sensor.serial_sensor').split(',')[13] | float }}"         
        
 #       // 490.74,235.27,3.05,0.68,  726.72,233.05,3.67,0.85,  208.93,235.24,1.14,0.78,  17864.96,84042.15

thanks everyone

That didn’t work because your spacing is way off. This must have cascaded into your other setup.

This would be the correct spacing.

  - platform: modbus
    registers:
      - name: porttcp
        hub: mc760
        unit_of_measurement: °C
        slave: 1
        register: 40207