Unable to seperate data in serial message

Hey all,

Feeling a little confused reading through the serial documentation help:


and

Basically I am using an Arduino to handle all of my sensors, utilising the arduinoJson.h library to output my variables and values over to my Raspberry Pi 4. which is working fine and I get the following:

{
  "Temperature": 23.297,
  "pH": 0,
  "DisolvedOxygen": 6.35
}

Then I added to my configuration.yaml file

sensor:
  - platform: serial
    serial_port: /dev/ttyACM0
    baudrate: 9600

  - platform: template
    sensors:
      Temperature_sensor:
        friendly_name: Temperature
        unit_of_measurement: "°C"
        value_template: "{{ states('sensor.serial_sensor').split(',')[1] | float }}"

        pH_sensor:
        friendly_name: pH
        value_template: "{{ states('sensor.serial_sensor').split(',')[3] | float }}"

        DO_sensor:
        friendly_name: DO
        unit_of_measurement: "mg/L"
        value_template: "{{ states('sensor.serial_sensor').split(',')[5] | float }}"

From my understanding,i have set it so each sensor looks at the value in position 1, 3 and 5 of the string? still getting my head around HA

Okay so just to confirm when i look at the serial port i have set up i get the following:

image

So its getting the data… I’m just not getting how to break it down. i also tried the follwoing two different options

sensor:
  - platform: serial
    serial_port: /dev/ttyACM0
    baudrate: 9600
    name: Arduino

  - platform: template
    sensors:
      temperature_sensor:
        friendly_name: Temperature
        unit_of_measurement: "°C"
        value_template: ‘{{ float(sensor.value_json.Temperature) }}’
        
  - platform: template
    sensors:
      weatherstation_temperature:
        friendly_name: 'Outside temperature'
        unit_of_measurement: '°C'
        value_template: "{{ float(states.sensor.Arduino.state.split(':')[3]) }}"

Looks like they’re different attributes, which you’d be able to do with something like this:

- platform: template
  sensors:
    temperature_sensor:
      friendly_name: "Temperature"
      unit_of_measurement: "°C"
      value_template: "{{ state_attr('sensor.serial_sensor', 'Temperature') }}"

You can check if this is the right formatting in the “Developer Tools > Template” section of HA. If it is, you’ll get ‘21’ as the value.

You can also have a maximum of one instance of the following:

- platform: template
  sensors:

Every sensor you template will go underneath that one configuration so:

- platform: template
  sensors:
    sensor1:
      *details*
    sensor2:
      *details*

Just remember to check all the value templates you make for sensors in your Developer section to make sure you get the right data.

1 Like

This is not going to work without telling it what sensor to evaluate.

I don’t believe those are attributes. I’m pretty sure that is the state.

So to pull data out of that state you would need to use the following templates:

{{ states.sensor.serial_sensor.state['pH'] }}
{{ states.sensor.serial_sensor.state['Temperature'] }}
{{ states.sensor.serial_sensor.state['DisolvedOxygen'] }}

it’s hard to test it since I don’t have that exact sensor but I tested it in the template editor by creating a variable with your provided info and it worked:

@icaman004 and @finity thank you so much for your explanation/time. I can now stop banging my head up against the wall! Also, I couldn’t wrap my head around the dev template tool until now, so big thanks again, guys!!

so for anyone else having issues in the future, I know I struggled to find a solved answer for this in the threads, it was super particular about the formatting, this is what I ended up using to get it to work:

sensor:
  - platform: serial
    serial_port: /dev/ttyACM0
    baudrate: 9600

  - platform: template
    sensors:
      
      temperature_sensor:
        friendly_name: "Temperature"
        unit_of_measurement: "°C"
        value_template: "{{ state_attr('sensor.serial_sensor', 'Temperature') }}"
      ph_sensor:
        friendly_name: "pH"
        unit_of_measurement: "pH"
        value_template: "{{ state_attr('sensor.serial_sensor', 'pH') }}"
      do_sensor:
        friendly_name: "DO"
        unit_of_measurement: "mg/L"
        value_template: "{{ state_attr('sensor.serial_sensor', 'DisolvedOxygen') }}"