Test of is_state('Template Sensor', 'test string') always returns false

Hi Everyone,
I’ve probably just been up too late and missing something obvious. TL;DR I can’t get this condition to execute correctly in the Developer Tools - Template Editor:

NOTE: I have already tried all the combinations of single and double-quotes in the test condition as well as the resulting string, making the condition the same as the result, opposite, etc. I highly doubt it’s a quote-related issue at this point.

  - sensor:
    - name: "lock status"
      state: >
        {% if is_state('sensor.exterior_door_lock_status', 'locked') %}
          "locked"
        {% else %}
          "unlocked"
        {% endif %}

As you can see in this side-by-side screenshot, my template sensor’s state says "locked" in the Developer Tools - State, but my test in the Developer Tools - Template Editor evaluates to "unlocked".

Long story short, the reason why I’m creating a sensor to read the status of my locks is because I intentionally want the state to return "Unknown" with a different icon than lock or lock-open when one door is locked and the other is unlocked. The plan is for the lock-off icon to inform the user of this condition, and the lovelace button card to execute a script that locks both doors if the state is unlocked or unknown and unlocks both doors if, and only if, both of their states are locked. Here’s the script this test will be incorporated into for your reference:

alias: toggle_exterior_door_locks
sequence:
  - service: |
      {% if is_state('sensor.exterior_door_lock_status', 'locked') %}
        script.unlock_exterior_doors
      {% else %}
        script.lock_exterior_doors
      {% endif %}
mode: single
icon: mdi:lock

The following code is working correctly, but in case it’s relevant here’s the code for my template sensor that I have in my configuration.yaml:

template:
  - sensor:
    - name: "Exterior door lock status"
      state: >
        {% if is_state('lock.assure_touchscreen_deadbolt_front_door', 'locked') and is_state('lock.assure_key_free_lever_touchscreen_back_door', 'locked') %}
          "locked"
        {% elif is_state('lock.assure_touchscreen_deadbolt_front_door', 'unlocked') and is_state('lock.assure_key_free_lever_touchscreen_back_door', 'unlocked') %}
          "unlocked"
        {% else %}
          "unknown"
        {% endif %}
      icon: >
        {% if is_state('lock.assure_touchscreen_deadbolt_front_door', 'locked') and is_state('lock.assure_key_free_lever_touchscreen_back_door', 'locked') %}
          mdi:lock
        {% elif is_state('lock.assure_touchscreen_deadbolt_front_door', 'unlocked') and is_state('lock.assure_key_free_lever_touchscreen_back_door', 'unlocked') %}
          mdi:lock-open
        {% else %}
          mdi:lock-off
        {% endif %}

It’s intriguing that, in your screenshot, the state is shown with double-quotes, though. It shouldn’t

If you put {{ states.sensor.exterior_door_lock_status.state }} in the template editor, it should give you the actual value. If you see quotes there, then the state is actually with doubles-quotes

Yeah, agreed. @smizles — despite your doubtfulness, it is a quote-related issue. Try this:

{% if is_state('sensor.exterior_door_lock_status', '"locked"') %}
   @koying is right
{% else %}
   @koying is wrong
{% endif %}

Don’t use the quotes in your template outputs, so

          locked

not

          "locked"

Well I’ll be gosh darned, we’re both right!

@koying there must be quotes inside the state output and I have been up too late to catch the obvious quote issue in my template sensor. This whole time I was bashing my head against the wall of quotes in the script and template editors that I never thought to play with the quotes in the one part of my code that I thought was working correctly…

Many thanks @koying!!!

Now that you have that sorted out, why are you going to all the trouble of making a sensor and an icon template when a binary sensor with a lock device class will do all this for you?

template:
  - binary_sensor:
    - name: "Exterior door lock status"
      state: >
        {{ is_state('sensor.exterior_door_lock_status', '"locked"') }}
      device_class: lock

I was answering that when I saw this:

2 Likes