Hi, I am attempting to configure multiple DS18B20 sensors that are read serially (json formatted) from a connected Arduino. The serial data from the arduino looks like this:
My configuration.yaml entry looks like this:
Example configuration.yaml entry
sensor:
- platform: serial
serial_port: /dev/ttyUSB0
baudrate: 9600
Use template sensors to parse the data.
template:
- sensor:
- name: “28AA35F0541401C6”
unit_of_measurement: “°F”
device_class: temperature
state: >
{% if {{ state_attr(‘sensor.serial_sensor’, ‘sensor’) }} == ‘28AA35F0541401C6’ %}
# update the sensor value with the new value
{{ state_attr(‘sensor.serial_sensor’, ‘value’) }}
{% else %}
#no match so keep the current data
{% endif %}
- name: “28AA6819551401CE”
unit_of_measurement: “°F”
device_class: temperature
state: >
{% if {{ state_attr(‘sensor.serial_sensor’, ‘sensor’) }} == ‘28AA6819551401CE’ %}
# update the sensor value with the new value
{{ state_attr(‘sensor.serial_sensor’, ‘value’) }}
{% else %}
#no match so keep the current data
{% endif %}
This logic appears to completely fail and then neither of the values get updated at all. Any help would be appreciated. thanks in advance!
Please format your configuration properly.

You are nesting templates, which isn’t allowed.
{% if state_attr('sensor.serial_sensor', 'sensor') == '28AA6819551401CE' %}
Then you will need to use the self-referencing variable this in the else or you will end up without a state most of the time.
Thank you… can you propose how better to handle this?
I already did… Look at the quoted portion from your original post and what I posted… you need to remove the expression delimiters {{ }} that are inside your statement delimiters {% %}.
apologies… i see that now… and thanks for the tip on posting
ok i have the new configuration as this:
sensor:
- platform: serial
serial_port: /dev/ttyUSB0
baudrate: 9600
# Use template sensors to parse the data.
template:
- sensor:
- name: "28AA35F0541401C6"
unit_of_measurement: "°F"
device_class: temperature
state: >
{% if state_attr(‘sensor.serial_sensor’, ‘sensor’) == ‘28AA6819551401C6’ %}
# update the sensor value with the new value
{{ state_attr('sensor.serial_sensor', 'value') }}
{% else %}
{{ this.state }}
{% endif %}
- name: "28AA6819551401CE"
unit_of_measurement: "°F"
device_class: temperature
state: >
{% if state_attr(‘sensor.serial_sensor’, ‘sensor’) == ‘28AA6819551401CE’ %}
# update the sensor value with the new value
{{ state_attr('sensor.serial_sensor', 'value') }}
{% else %}
{{ this.state }}
{% endif %}
This still is not updating the values at all
You haven’t provided tests and defaults for the first run where this is not defined…
I would recommend switching to trigger-based template sensors instead of state-based. Since your Serial sensor provides an attribute for the sensor ID, you could uses distinct State triggers for each one instead of needing if/then and self-referencing.
template:
- triggers:
- trigger: state
entity_id: sensor.serial_sensor
attribute: sensor
to: 28AA35F0541401C6
sensor:
- name: '28AA35F0541401C6'
unit_of_measurement: '°F'
device_class: temperature
state: "{{ trigger.to_state.attributes.value }}"
- triggers:
- trigger: state
entity_id: sensor.serial_sensor
attribute: sensor
to: 28AA6819551401CE
sensor:
- name: '28AA6819551401CE'
unit_of_measurement: '°F'
device_class: temperature
state: "{{ trigger.to_state.attributes.value }}"
1 Like
Ok switching to trigger-based template sensors seems to do exactly what I want… I will keep playing with it… Thanks so much!