Error parsing value (Low priority)

I have an ESP8266-01 with two DS18b20 temperature sensors. The ESP publishes the temperatures every few minutes with the topic: “temperature01/temperature”, and a payload of ‘{“ds1”:{“Temperature”:77.00}}’ or ‘{“ds0”:{“Temperature”:63.00}}’.

My sensors.yaml file is below:

  - platform: mqtt
    name: "Temp In"
    state_topic: "temperature01/temperature"
    value_template: "{{ value_json['ds0'].Temperature }}"
    force_update: true
    unit_of_measurement: "°F"
  - platform: mqtt
    name: "Temp Out"
    state_topic: "temperature01/temperature"
    value_template: "{{ value_json['ds1'].Temperature }}"
    force_update: true
    unit_of_measurement: "°F"

This works fine and I see the inside and outside temeratures on Home Assistant.
But, I am getting errors logged:

Error parsing value: 'dict object' has no attribute 'ds0' (value: {"ds1":{"Temperature":80.00}}, template: {{ value_json['ds0'].Temperature }}) 
‎
Error parsing value: 'dict object' has no attribute 'ds1' (value: {"ds0":{"Temperature":63.00}}, template: {{ value_json['ds1'].Temperature }}) 
‎

I suspect the errors are because the template for ds0 doesn’t know what to do with the data from ds1. (For the record, this is my first template).

While the errors don’t interfere with Home Assistant, I would like to fix this for two reasons. 1) To learn how to better handle data from a sensor with two values, and 2) to reduce the write cycles on my SD card in the Raspberry Pi.

So, I am looking for tips on fixing either my templates or the data format sent by the ESP (or both).

Thanks

I don’t use MQTT, so not that familiar with it, but maybe those two values should be separated into two topics, one for each??

Anyway, you could do something like this I suppose:

value_template: >
  {% if 'ds0' in value_json %}
    {{ value_json.ds0.Temperature }}
  {% else %}
    {{ states('sensor.temp_in') }}
  {% endif %}

And the same for the second, except use ds1 and the entity_id of the “Temp Out” sensor.

The idea is, if the payload contains the value it’s looking for, then use it. Otherwise use the previous value. (Not sure what the actual names of the sensors are, so I guessed. Adjust accordingly.)

Thanks for the reply.
As I said, this is my first template- I didn’t know I could do if-then in a template, so
I can easily implement something like your tip shows. I did think of making the sensor ESP send two topics, but I am trying to use more JSON format data.