Test JSON sensor data without generating errors on HASS log

Hi! I’ve finally trying to get rid of an annoying error that appears on HASS log at startup.

I have a serial sensor:

sensor:
  - platform: serial
    name: bedroom
    serial_port: /dev/ttyS0

That presents data like this:

    {"lights":[0,1,-9],"environmental":[88,-9,-9]}

The environmental JSON tag is temperature (first value), humidity (second value), pressure (third value).

To extract the humidity value of bedroom sensor I do:

- platform: template
  humid_bedroom:
    device_class: humidity
    friendly_name: "Bedroom humidity"
    unit_of_measurement: '%'
    value_template: >-
        {% if states.sensor.bedroom.attributes.environmental[1]|float > 0 %}
          {{states.sensor.bedroom.attributes.environmental[1]|float|round(1)}}
        {% endif %}

The conditional on value_template exists because, sometimes, the sensor generate negative values for humidity, and this is not good.

The problem is that, at startup, bedroom is not populated (because the serial sensors takes sometime to load), and the conditional produces an error on HASS log. This behavior is documented on the “Warning” that exists on https://www.home-assistant.io/docs/configuration/templating/#states.

I’ve tried using all statements suggested on the Warning indicated before, without success. I can’t figure how to test an specific element of a JSON tag array with those functions.

It is possible to do this test without errors on HASS log?

Thanks in advance.

Cheers!

I took the liberty of adding two devices states: negative when the value is less than zero and unavailable when sensor.bedroom isn’t ready.

Try this:

      humid_bedroom:
        device_class: humidity
        friendly_name: "Bedroom humidity"
        unit_of_measurement: '%'
        value_template: >-
          {% if states('sensor.bedroom') != 'unknown' %}
            {% set t = state_attr('sensor.bedroom', 'environmental')[1] | float | round(1) %}
            {{ t if t > 0 else 'negative' }}
          {% else %}
            unavailable
          {% endif %}

If you plan to graph this template sensor’s values then you should remove my two additions to avoid mixing numeric and non-numeric values in the sensor’s history.

Sorry for the long delay, but only this weekend I’ve had time to get back on this issue.

Checking with states('sensor.bedroom') works, but an error appears on HASS log because element [1] doesn’t exist on None, generated by the set t = ... below it.

I’ve changed the first test to state_attr like this:

      humid_bedroom:
        device_class: humidity
        friendly_name: "Bedroom humidity"
        unit_of_measurement: '%'
        value_template: >-
          {% if state_attr('sensor.bedroom', 'environmental') != None %}
            {% set t = state_attr('sensor.bedroom', 'environmental')[1] | float | round(1) %}
            {{ t if t > 0 else 'negative' }}
          {% else %}
            unavailable
          {% endif %}

It checks if sensor.bedroom is defined and, at the same time, if environmental tag exists on it.

I’ve maintained the negative and unavailable states, that is a great addition!

Thanks a lot!

Cheers!