Binary sensor - template not getting the right value

Hi,

I am seeing discrepancy in the value of a binary sensor using template editor and Lovelace UI. In the template editor when I check following, I see the water softener as online:

This is the correct status as I am also able to see other stats such as water flow, consumed, recharging etc. But when I create a template sensor using the above syntax:

template:
  - binary_sensor:
      - name: water_softener_status
        device_class: connectivity
        state: >
          {{ state_attr('sensor.ecowater_<redacted>, 'status') }}

The Lovelace UI shows this sensor as Offline/Disconnected!

eco-02

I am not sure what I might be doing wrong since the template syntax seems rather straightforward.

Since you have not provided a code for this card, I assume that this is an Entities card & a state’s value.
The state is “off” (“Disconnected”).

The source attribute “status” is supposed to be “on / off” - otherwise the logic is not explicit.
According to Dev tools → Template, it’s value is “Online”.
It was “converted” to “off”:

1 Like

Thanks for the pointer, I fixed it using following code:

        state: >
          {% if state_attr('sensor.ecowater_<redacted>', 'status') == 'Online' %}
            true
          {% else %}
            false
          {% endif %}