Serial sensor template

Hi,

I got a serial port sensor with the following data: {“WaterUse”:10.69}
I don’t get the template correct so that home assistant can understand it.
This is the template I have made:

  sensors:
    WaterUse:
      friendly_name: Water meter
      unit_of_measurement: "M3"
      value_template: "{{ states('sensor.serial_sensor') | float  | round(1) | float (default=0)}}"

Can someone please explain why the data is not interpreted correctly? because I don’t understand.

Thanks.

Your serial sensor does not contain a string of digits that can be converted into a number by the the |float filter.

Show your configuration for the serial sensor and we can get it to show just 10.69 using a value_template.

Is this the only sensor the serial stream delivers?

Thanks for the reply.
I do not understand. 10.69 is a value that needed to be converter to float. Why is home assistant not understanding json?

The serial configuration is this:

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

Yes this is the only data which is send by the serial sensor. It is a arduino sensor that use the ArduinoJson lib.

Is there something I can read to so I got a basic understanding how things are structured.

Thanks

Because you have not told it to look for json data and what key to look for in that json data. You do this with a value_template.

Like this:

sensor:
  - platform: serial
    name: Water Meter
    serial_port: /dev/ttyUSB1
    baudrate: 9600 
    value_template: "{{ json_value.WaterUse }}"

If you want, you can add an icon and unit_of_measurement using customise as these are not in included in the available options for this sensor (see “serial” link below). Note how the entity id has changed by including a name in the configuration.

customize:
  sensor.water_meter:
    icon: "mdi:water-pump"
    unit_of_measurement: "m3"

This goes under homeassistant: in your configuration.yaml file, see the example in the “customize” link below.

The documents are your best resource. They include the available options for each integration, what type of data each option expects and if they are optional. There are usually a number of examples too.

For a basic understanding of yaml (lists vs dictionaries) this is a good read:

http://thomasloven.com/blog/2018/08/YAML-For-Nonprogrammers/

There is also some good info here:

That blog is filled with good advice.

Thanks for all the info. But it still doesn’ t work only I don’t see any error message. So I don’t have a clue what the problem is. i only found state unknown.

Made some progress.
Only I think there is something wrong with the value template:

  value_template: "{{ json_value.WaterUse }}"

Because I got the complete string as a value:
Screenshot from 2022-01-30 13-09-35

Any suggestion how to solve this?
Thanks!

Looks like the serial platform may only report strings not json.

Try this:

sensor:
  - platform: serial
    name: Water Meter
    serial_port: /dev/ttyUSB1
    baudrate: 9600 
    value_template: "{{ value.split(':')[1][:-1] }}"

This template splits the string into a list at the : character, then selects all but the last character of the second element of the list.

{"WaterUse":10.69}
split(':')
[{"WaterUse",10.69}]
[1]
10.69}
[:-1]
10.69

Thanks!!!
This seems to work.
Only I don’t understand why home assistant doesn’t understand that this is json.
It looks like a valid json line. Only there is no space after the : but is that required in json?
any suggestions?

It’s still valid json without the space.

It’s down to whoever wrote the serial integration. They did not include json parsing. It just reports strings.

You could open a feature request for it to be supported.

Ok, thank for the help. I understand now.