Location and 'Not' (or !=)

I’m still coming to grips with automation, but one thing I had hoped to use HA for eludes me so far …
it revolves around ‘NOT’ which according to other posts, doesn’t seem to be an operand in HA

what I am trying to get to … (Pseudo code here)

IF Time 9:15am AND Workday AND Sons_Location != ‘School’ Notify via Slack

I enabled the workday binary sensor, but when it comes to location, the seeming inability to say ‘Not’ is hampering me.

It’s actually pretty easy to solve using this:

  condition:
    conditions:
      - condition: template
        value_template: "{{ not is_state('device_tracker.my_phone','home') }}"
2 Likes
trigger:
  platform: time
  at: '09:15:00'
condition:  # AND by default
  - condition: state
    entity_id: binary_sensor.workday_sensor
    state: 'on'
  - condition: template
    value_template: "{{ not is_state('device_tracker.sons_location', 'School') }}"
actions:
...
3 Likes

Thank you both, so a value template is needed to compare two values and get a Boolean response?

Not always. For example, this checks if your son is at school rather than not at school:

trigger:
  platform: time
  at: '09:15:00'
condition:  
  - condition: state
    entity_id: binary_sensor.workday_sensor
    state: 'on'
  - condition: zone
    entity_id: device_tracker.sons_location
    zone: zone.school
actions:
...

Hi Guys, just to be difficult … it doesn’t seem to work :\ and I am not sure what I’m doing wrong …

- id: '1571571390739'
  alias: School Check - Ben
  description: ''
  trigger:
  - at: 09:30:00
    platform: time
  condition:
  - condition: state
    entity_id: binary_sensor.workday_sensor
    state: 'on'
  - condition: template
    value_template: '{{ not is_state(''device_tracker.life360_ben'', ''SchoolSNP'')
      }}'
  action:
  - data:
      message: Please Check Bens Location, it does not show him at school.
      target:
      - '@mark'
    service: notify.slack_team

When I check in Developer tools for the Tracker state … it shows the correct value.
but the notification fires anyway.

I think the problem is that you put two single quotes before and after device_tracker.life360_ben and SchoolSNP. Try with only one single quote.

  - condition: template
    value_template: "{{ not is_state('device_tracker.life360_ben', 'SchoolSNP') }}"

Thanks but I’m afraid not, the code checks clean … but the notification still fires
I’ve tried changing the tracker to person.ben (with the tracker attached to his record) but same result.

Actually hold up, there is something wrong with either (A) my installation or (B) the evaluation syntax … I changed the code to :

value_template: "{{ is_state('device_tracker.life360_ben', 'Sc8ho4olS3P') 
}}"

(eg: to a rubbish name) and the notification fired …

I realize you can / should be able to use value_templates to evaluate items like this - and not sure why I set mine up like this, but in order to simplify I created a bunch of sensors based on the underlying device_tracker to set my own home / not_home for different family members. I can then just use default AND conditions more simply:

    google_maps_mark_ishome:
      friendly_name: "Mark Google Maps Home Sensor"
      value_template: >-
       {% if is_state('device_tracker.google_maps_###', 'home') %}
         home
       {% else %}
         not_home
       {% endif %}

I’ve built these for each tracker / zone combination. (_iswork = work/not_work, etc.) A little messier sensors.yaml, but then don’t have to mess w/ value templates in automations :slight_smile:

Also worth using /developer-tools/template url in your HA to test your template results to ensure they’re returning what you want w/out having to load/reload HA / automations / etc.

The automation currently triggers at 9:30 and confirms it’s a workday and Ben’s location is not SchoolSNP. If both conditions are satisfied, it sends the notification.

So what were the conditions when you said “but the notification fires anyway”.

Also, did you wait until 9:30 for the automation to trigger or did you trigger the automation manually (via the Services page using the automation.trigger service)? If you triggered it manually, it ignores the trigger and condition(s) and simply executes the action.

1 Like

Thanks Mark, I did check the developer tools but that syntax will certainly work as well.
Taras, I think, has hit the nail on the head due to my ignorance of the trigger method, I assumed it was a ‘test’ button and I didn’t expect it would ignore all the conditions. Trap for young players :wink:

Confirmed, my mistake.
It all works perfectly, merely my confusions on manually activating it.

… and in case someone was wondering if this behavior is documented, it is: Testing your automation.

Please note that if you click on Trigger of an automation in the frontend, only the action part will be executed by Home Assistant. That means you can’t test your trigger or condition part that way. It also means that if your automation uses some data from triggers, it won’t work properly as well just because trigger is not defined in this scenario.

1 Like