[Request] add "not is_state" in official starting guide

HI, I’m not sure where to post such a request, but I certainly would like something to be in the official starting guide of homeassistant. Especialy since this is a function that is available, working and simply not documented - at least I haven’t found it and was searching google up and down to find it.

That function is “not is_state”. Perhaps others too, I have only tested this one yet.
What it is capable of is to be used as a condition in order to trigger an event only if said state is NOT true.
The automation in which I am using this one is a light switch which turns off all lights if noone is at home (triggered every time someone leaves “zone.home”). Before I simply used “if state not_home”, but since I have integrated location service Owntracks and Mosquitto onto my computer, I have more states (zones) which makes it impossible to purely rely on “not_home”. Instead I need a “condition: not is_state home”. The lines of code look like this:

trigger:
  - platform: zone
    entity_id: device_tracker.NAME
    zone: zone.home
    event: leave
  - platform: zone
    entity_id: device_tracker.OTHER_NAME
    zone: zone.home
    event: leave
 condition:
   - condition: template
     value_template: "{{ not is_state('device_tracker.NAME', 'home') }}"
   - condition: template
     value_template: "{{ not is_state('device_tracker.OTHER_NAME', 'home') }}"
 action:
   - service: scene.turn_on
     entity_id: scene.all_lights_off

As said, this only is true if everyone is away, which in my automation continues to trigger lights off.
I also successfully used it to send me a notification using template-text:

  action: 
    - service_template: notify.notify
      data_template:
        title: Test
        message: |
          {%  if not is_state("device_tracker.NAME", "home") %}Not Home{% else %}ELSE{% endif %}

As far as I can tell everything is fine with this kind of coding and it saves me a lot of trouble with other automations of this kind too, because it is way easier than to keep track of all other possible options that (in that case) a state could be, especialy since I would ALWAYS have to update ALL automations whenever I add another zone to my map.

I hope you guys find that one helpful and add it to the starting guide too.
Thanks!

1 Like

Home Assistant users are encouraged to help maintain the documentation. There’s a link on the top-right of every documentation page indicating “edit this page on Github”.

What you’ve described falls in the category of Templating. Here’s its documentation page;

On that page, you’ll find this:

We will not go over the basics of the syntax, as Jinja2 does a lot better job at this in their Jinja2 documentation.

not is a logical operator. It is found in many programming languages. Some use an exclamation point as shorthand for not. It’s considered to be one of the aforementioned “basics of the syntax” and so that’s why the documentation directs to the Jinja2 documentation. The relevant section is here:
http://jinja.pocoo.org/docs/dev/templates/#logic

However, if you feel strongly about it, you could add a simple example of its usage in Home Assistant’s Templating documentation.

Oh, that’s neat.
I had a deeper look into that Jinja2 documentation, and as someone who is not used to coding - i.e I copy paste code from other examples to get my automations running - I have a hard time to understand the examples that are listed. So while single commands like “not” seem obvious, the way to use them propperly is not. Also, it seems like that documentation is not 100% accurate with how HA works. i.e it sais that using “!=” in order to compare two states, which should do exactly what my OP sais, has never worked for me. Maybe it’s just a decent attack of “silly me” involved here, but then again, no documentation was available to me to show me my error. All I had was that one line in Templating, that mentrions it:

{% if states("device_tracker." + tracker_name) != "unknown" %}

Ultimately finding and using “not” was the easy path in this case.

So while I concider adding that information on GitHub, I don’t feel too confident making such a change based on my coding-skill-level. I don’t want to mess up your guides by posting wrong information.
However what I will do is to add my coding example to the condition guide within Template Condition:


That way a user can easily spot it and use it right away copy-paste style.

!= does work in Home Assistant. I put this into the Template Editor and it correctly identified our kitchen light’s state.

{% if states("light.kitchen") != "off" %}
   Light is on!
{% endif %}

Similarly, this also works in the Template Editor:

{% set my_test_json = {"temperature": 25, "unit": "°C"} %}
{% if my_test_json.temperature != 26 %}
   Correct, it's not 26.
{% endif %}

I agree the documentation for Jinja2 assumes its audience is familiar with programming. Seeing an example of a concept is helpful so I encourage you to add one to the documentation. However, I wish to draw your attention to the fact that code readability can sometimes be impaired by the use of not.

When you only have two valid options (up/down, on/off, hot/cold, etc) it’s preferable to test for the option you want as opposed to the option you don’t want.
if water == cold
as opposed to
if not water == hot

When you have more than two valid options, like red/green/blue, and will accept any option except one (blue), then not is well-suited to the task:
if not color == blue

Thank you for your clarifying post.
As said, I would not be surprised if my failiure to use “!=” is based on “silly me”. The thing I wanted to point out is the difficulty to actualy find the reason for it not working.
Where ever possible I use a test-automation which should send me a pre-defined message and it is quite useful to tinker with existing code and adapt it into my specific home. Usualy HA helps me by saying " Line XYZ, expected item X, found Y" or some sort of it. That particuar case however usualy ended in my message containing nothing or not even being sent (text = template) or a condition not being true or falsely always being true (condition = template) but without the code being wrong, according to HA.

Thankfully there is a wonderful community here open for question and a lot of questions already having been asked and answered! Keep it up, guys!

Ok, just to get things straight on my end, I have tried setting up another test-automation to check if I can get this comparison “!=” to run. After tinkering for a while, this is what I ended up with:

trigger:
  - platform: time
    minutes: '/1'
    seconds: 0

condition:
  condition: template
  value_template: "{{ states('device_tracker.NAME.state') != 'home' }}"

action:
  service_template: notify.notify
  data_template:
    title: Debug-Message
    message: |
      {{ states.device_tracker.NAME.state }}

This automation works. It triggers, it passes the condition, then sends me the notification.

Expectation:
Since I am home, the condition should prevent the action from happening.
Reality:
The condition passes and sends me a message containing the text “home”, proving that the first part of the template-condition is home.
So the value_template sais “home != home”, and the automation sais: Yep, that’s fine, go on.
When switching the condition from “!=” to “==” the action does not run.

So yes, “!=” does work, but … errr… huh?

When switching the condition from " ‘home’ " to " states(‘home’) ", it’s working fine.
So, I have tried another automation, looking like this:

trigger:
  platform: state
  entity_id: device_tracker.NAME

condition:
  condition: template
  value_template: "{{ states('trigger.from_state.state') != states('trigger.to_state.state') }}"

action:
  service_template: notify.NAME
  data_template:
    title: Debug-Message
    message: |
      NAME went from {{ trigger.from_state.state }} to {{ trigger.to_state.state }}.

This one is working fine. Actualy I only tested it with condition value_template “==”, it does not work with “!=” since I am home - as expected.

Is this a bug or did I miss something in the guide?