'if not' in a value template

I am trying to insert a effective ‘not’ into a value template ‘if and’ test.
I want to test that a state is not a specific value within the below code


  trigger:
      platform: template
      value_template: > 
 "{% if 
 (is_state('device_tracker.cory','not_home')) and 
 (is_state('device_tracker.corypixel','not_home')) and 
 (is_state('device_tracker.pixel','not_home')) 
 %}true{% endif %}"

I want to add to this a test that the state of a sensor is not a specific value. Sort or like below.


  trigger:
      platform: template
      value_template: > 
 "{% if 
 (is_state('device_tracker.me','not_home')) and 
 (is_state('device_tracker.mypixel','not_home')) and 
 (is_state('device_tracker.mybtpixel','not_home')) and
 (is_not_state('device_tracker.mymqttpixel','home'))
 %}true{% endif %}"

The mqtt (owntracks) tracker will have extra states other than ‘not_home’ according to actual location, like ‘work’. So I want to just trigger when it is in a state that is not ‘home’.
Is there a way to do this withing the existing value_template test?

have you tried putting all those device trackers into a group?

You might get a state for the group that is what you want, which I think is not home for all of the devices?

then you could just test that single state.

I know this doesn’t answer the question exactly, but if you put all those devices in a group, the group will only show home / not_home, where !home == all devices elsewhere than home. then your trigger becomes:

trigger:
  platform: state
  device: group.my_tracker_group
  to: 'not_home'

If those are the only devices you’re tracking, then HA already has a group for you called group.all_devices .

1 Like
(not is_state('device_tracker.mymqttpixel','home'))
5 Likes

I new it had to be this simple, thanks.

I may look into groups later on but a solution that I can easily add to my existing code works best at this point.

This worked perfectly. Thank you so much!