Value_template always returns true (pls help fix my template)

Hi everybody,

I am trying to create a few automations that only run when two people are not at home. However, I cannot seem to create the right template for this. This is my test package

I added comments to the code rather then describing what I need beforehand… problem is, both device_trackers work fine, but the binary_sensor will always be true.

# This sensor is supposed to be true/on when both people are home and false/off when either of them are away
 
binary_sensor:
  - platform: template
    sensors:
      beide_home:
        friendly_name: "Beide Home"
        value_template: >
# this SHOULD return true when  both are home and false otherwise, but will always return true
          {% if states('device_tracker.ben_galaxys9') + states('device_tracker.jonna_honor10') %}
            true
          {% else %}
            false
          {% endif %}
# perhaps it'd be better to do this the other way round? Return true when both are NOT at home and false if EITHER is home?


# This doesn't do anything productive yet; just sends a notification if both parties have been back home for 10 minutes or have been gone for 30 minutes (so that I will get feedback to test). I have  not implemented the actual things that are supposed to happen yet because I need the basics to work first.

automation:
  - id: "beide_home_false"
    alias: "[Anw] Beide != Home"
    trigger:
      - platform: state
        entity_id: binary_sensor.beide_home
        from: "off"
        to: "on"
        for: 00:10:00
    action:
      - service: notify.ben
        data:
          message: "🧪 Ihr seid beide seit 10 Minuten wieder zu Hause 🧪"
  - id: "beide_home_true"
    alias: "[Anw] Beide = Home"
    trigger:
      - platform: state
        entity_id: binary_sensor.beide_home
        from: "on"
        to: "off"
        for: 00:30:00
    action:
      - service: notify.ben
        data:
          message: "🧪 Ihr seid beide seit 30 Minuten nicht mehr zu Hause 🧪"

I thought creating a binary_sensor would be best so that I don’t have to include BOTH devices to be not_home whenever I want an automation to trigger, but rather just “ask” the binary_sensor whether or not they are both gone or if one of them is home.

The planned automations do things like regulating the temperature, so they must not be triggered when one of the two is at home.

Thank you for your ideas :slight_smile:

 {% if is_state('device_tracker.ben_galaxys9', 'home') and is_state('device_tracker.jonna_honor10', 'home') %}

And as the template actually returns true/false it can be reduced to this:

binary_sensor:
  - platform: template
    sensors:
      beide_home:
        friendly_name: "Beide Home"
        value_template: "{{ is_state('device_tracker.ben_galaxys9', 'home') and is_state('device_tracker.jonna_honor10', 'home') }}"

Where did you read that the plus operator (+) is equivalent to the logical AND operator?

Because it it not equivalent.

To prove it to yourself, put this into the Template Editor:

{{ states('device_tracker.ben_galaxys9') + states('device_tracker.jonna_honor10') }}

The result will be something like homehome or homenot_home or some other combination. In this situation, the plus operator is concatenating the two strings.

tom_I has shown the correct solution (i.e. use logical and operator with the is_state function).

Thank you @tom_l
I have yet to get used to the syntax…

@123 you are right. Weirdly enough, I have tested this in templates before posting here… now that I tried again, I got homenot_home, while before I always got True (whether or not the value should actually have been true, though).
I tried the plus sign because I had already tried and and got an error message; I assume this was caused by something else, but at the time I had thought that the “and” caused that error…

The plus operator is for adding numeric values. However, if the values aren’t numeric but are strings then it will concatenate the strings. This dual behavior can create problems (errors) if the values are a mix of numeric and strings. That’s why it is recommended to use the tilde operator (~) to concatenate strings (it only does that and won’t add numeric values). The tilde operator will also treat numeric values as strings so this will work:

{{ 'hello ' ~ 123 }}

and produce:

hello 123

whereas this won’t work and will produce an error:

{{ 'hello ' + 123 }}

Most importantly, neither the plus nor the tilde operators will perform a logical AND operation. For that you simple use the and operator.


If you wish to experiment, paste this into the Template Editor:

{{ 'hello ' + 'world' }}
{{ 'hello ' ~ 'world' }}

{{ 1 + 2 }}
{{ '1' + '2' }}

{{ 'hello ' + '123' }}

{{ 'hello ' ~ 123 }}

{#
This template, containing a string and integer will produce an error
{{ 'hello' + 123 }}

#}