Difficulties to get serial sensor configured correctly

Hi,

I’m trying to configure the serial sensor, which sends data from 40 sensor values separated with space every 2 seconds as a string. I haven’t managed to do so where the serial sensor is only returning the unknown state. Via terminal, I can easily see data coming in with the command: minicom -D /dev/ttyAMA0, but for some unknown reason, the serial sensor and template created sensor state stays at unknown status. Can anyone suggest what I’m missing/forgetting on this setup?

Here is my yaml-config for serial sensor:

- platform: serial
    serial_port: "/dev/ttyAMA0"
    baudrate: 38400
    name: RPICT
    value_template: "{{ states('sensor.serial_sensor') }}"
template:
  - sensor:
      - name: "Old Fuse Box - L1"
        unique_id: "rpict_ct1"
        unit_of_measurement: W
        device_class: power
        state_class: measurement
        availability: true
        icon: mdi:current-ac
        state: "{{ states('sensor.serial_sensor').split(' ')[1] | float }}"

Assume the incorrect indentation of the first line is a copy/ paste thing?

How long is the serial response? Sensor states are limited to 255 characters.

I think your problem is the value_template line in the serial sensor, which is referencing a non-existent sensor. Try removing it.

Your serial sensor will then be sensor.rpict. The template sensors will need updating to reflect that.

Yes, the first one is copy-paste thing. :slight_smile:

Currently, only half of the sensors are enabled so the length is not the issue. When everything is enabled serial response is designed to be between 200 to 250 characters.

Yep, that value template was my last try to get this working. Without it when the sensor is the sensor.rpict state status is still unknown

When I play with Developer Tools - Template area and try to put statements there

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

I get the result that the state is unknown. And if I put the following statement there:

{{ states('sensor.rpict').split(' ')[1] }}

I will get an error message of list object has no element 1

So now I’m wondering that do I need to somehow enable serial data reading inside of Home Assistant to get it working (SSH to server and using terminal serial reading works).

Until you get a valid reading in sensor.rpict you are wasting your time fiddling with split and the template sensors.

I have no experience with serial sensors, but check your logs and maybe use:

value_template: "{{ value[:250] }}"

to start with, to eliminate an over-long response as a problem.

Yep, you were right on your hunch that this was caused over-long serial response. with that value_template limitation, I got main sensor values coming visible at HA.

Thanks for helping in this serial case.

Now I just need to figure out how to get around this limitation.

I have been struggling with getting a functional serial sensor as well. I have the same symptom, in that I can use minicom and see the serial data being received. Short “this is a test” data is sent by a separate computer using putty. I can even send data to that other computer using shell_command.

I have enabled the following:

logger:
  default: critical
  logs:
    homeassistant.components.sensor: debug
    homeassistant.components.serial: debug
    homeassistant.components.sensor.serial: debug

No valuable information that I could find.

sensor is configured as follows:

sensor:
  - platform: serial
    name: "ttyS0"
    serial_port: /dev/ttyS0
    baudrate: 9600
    bytesize: 8
    parity: N
    stopbits: 1
    xonxoff: true
    rtscts: false
    dsrdtr: false
    value_template: "{{ value[:250] }}"

Unfortunately, message size does not appear to be my issue. My applogies in advance for any formatting issues or other faux pas. Posting here because “The solution doesn’t work for” me.

An interesting update:
I formatted several test messages within docklight. Some messages are successful, others are not. It would appear that the ONLY successful messages include a \n (line feed) at the end. If line feed is included mid-message, the sensor will report the character sent preceding the line feed.
Indeed, if a message was sent without line feed, that message stays in buffer until line feed:
e.g.
sends: this is a test
sensor = “unknown”
sends: this is a second test \n
sensor = “this is a testthis is a second test”

“{{ value[:250] }}” was not a necessary condition for these short test messages

Is this the expected behavior??