Test if entity exists -- is this odd?

Looking back at the post How to test whether an entity exists?, I’m not sure if the test for {{ states(...) is defined }} ever worked, but now states() returns unknown if the entity is, well, unknown.

Which made me wonder, why you can do something like this:

  - condition: template
    value_template: >-
      {{ states('media_player.apple_tv_family_room') in
      ['unknown','standby','off','unavailable'] }}

but can’t then do this:

  - condition: state
    entity_id: media_player.apple_tv_family_room
    state:
      - unknown
      - standby
      - "off"
      - unavailable

A little confusing if you were expecting to test for a state that states(...) returns, right?

It did not. VDRainer commented that it “only works with the states.entity_id.state notation.”

There’s a subtle but important difference between the two types of conditions.

  • The Template Condition is employing the Jinja2 processor to evaluate its template. The states() function is responsible for determining the supplied entity’s state. Whoever created the function, chose to have it report unknown if it cannot locate a valid entity_id.

  • The State Condition is employing python to evaluate the YAML code. Whoever wrote it chose to report the lack of a valid entity_id as an error.

I would expect a Condition (or Trigger, or Action) that is fed erroneous data to report it as an error.

Of course, that’s how I got here…

I saw states(…) now returning unknown and simply added it to my condition then noticed it didn’t behave the same way.

I’m using Spook to disable an integration (Apple TV) when not using it. I use it maybe once a week. When the integration is first disabled the entity becomes unavailable, and that’s what I had used first. At some point it started to return unknown.

I’m not quite sure when or how that happens. I disabled the integration and restarted core and it still reported unavailable.