Trying to work with data coming in on a serial port - Home brew hardware

Hi everyone. This is my first post, so please go easy with me.
I use opensourceautomation.com software for Windows, but its kind of clunky and now the website has gone offline, its time to move on.

I created some home brew hardware a few years ago with PIC microcontrollers.
Low powered PIRs and temp sensors that transmit their data over to a receiver on 434/315 Mhz.

The receiver then converts the 4 bytes of data into serial, 9600 8N1 and sends it into the comm port.
With a plugin I wrote for OSA, everything worked well.

Over the last few days, I’ve been trying to integrate this into HA.
I’ve tested the incoming bytes using some terminal software on the Linux machine. It is working fine.

I’ve got this far, and I can see data coming into Home Assistant, but I can’t for the life of me figure out how to convert each of the 4 bytes into its decimal value.

This is what I have in my configuration.yaml so far:

sensor:
  - platform: serial
    serial_port: /dev/ttyUSB0
    baudrate: 9600
    bytesize: 8
    parity: N
    stopbits: 1

  - platform: template
    sensors:
      temperature_sensor:
        friendly_name: Temp
        unit_of_measurement: ""
        value_template: "{{ states('sensor.serial_sensor')  [0:1]  }}"

This does show the first byte of 4 as a character, but I’ve tried all sorts to get its ASCII numeric value.
As an int, float etc. Nothing I’ve tried has worked.

Hope this is enough information for someone to help?
If you need more info, please let me know.

Kind regards,
Ian.

Welcome! There might be a better way, but if speed is not an issue, see my responses here for ideas:

Hi Troon.
Thanks for the suggestion, but I think I’ve figured it out (typical, straight after posting for help!).

There is something I’ve used in the past in Visual Basic and Pascal called ‘ord’.
It returns the decimal value of an ASCII character.

I tried it in HA and its worked !

value_template: "{{ states('sensor.serial_sensor')  [0:1] | ord }}"

I’ve just seen it return 117 for the letter u so I’m confident its working.

My next problem is why it takes about 30 seconds for data at the serial port to appear in HA, but I might save that for another post. I’ve not actually searched for a solution yet.

Again though, thanks for your suggestion.

Kind regards,
Ian.

1 Like

Seriously. I thought I’d tried that as a solution to the other problem with no luck, but it does work.