Sensor Template with IF, AND and ELSE

Hi all!

I’m a newbie to all this and I’m struggling with a sensor and it’s state being set dependent on the state of two other sensors.

When I run this the sensor just doesn’t show up. I can see the state for the other 2 sensors being correct - i.e. it shows the AP name and the SS.

The sensor code is below:

    ollie_location:
      value_template: >
        {% if is_state('sensor.ollie_ap','Outdoor_AP')
        and is_state('sensor.ollie_ap_strength') | float < -50 %}
          Outside
        {% elif is_state('sensor.ollie_ap','LivingRoom_AP') %}
          Living Room
        {% elif is_state('sensor.ollie_ap', 'Office_AP')
        and is_state('sensor.ollie_ap_strength') | float > -50 %}
          Office
        {% elif is_state('sensor.ollie_ap', 'Outdoor_AP')
        and is_state('sensor.ollie_ap_strength') | float > -50 %}
          Office
        {% elif is_state('sensor.ollie_ap', 'Playroom_AP')
        and is_state('sensor.ollie_ap_strength') | float < -50 %}
          Kitchen
        {% elif is_state('sensor.ollie_ap', 'Playroom_AP')
        and is_state('sensor.ollie_ap_strength') | float > -50 %}
          Playroom
        {% else %}
          Unknown
        {% endif %}

I hope you can help out :). I’m using the Cisco Mobility Express integration to pull the AP and the SS of the device to the AP.

Thanks!

Nothing wrong there. What does the rest of the sensor config look like?

Platform for example.

Have you defined the entities that update your template sensor?
The template sensor will never be set unless you define which entities it depends on.

- platform: template
  sensors:
     ollie_location:
         entity_id:
            - sensor.ollie_ap
            - sensor.ollie_ap_strength
           value_template: >
           ...

Here is the full code now with a screenshot of the sensors in the glance:

    ollie_ap:
      value_template: "{{state_attr('device_tracker.iphone', 'AP')}}"
    ollie_ap_strength:
      value_template: "{{state_attr('device_tracker.iphone', 'SS')}}"
    ollie_location:
      entity_id:
        - sensor.ollie_ap
        - sensor.ollie_ap_strength
      value_template: >
        {% if states('sensor.ollie_ap','Outdoor_AP')
        and is_state('sensor.ollie_ap_strength') | float < -50 %}
          Outside
        {% elif states('sensor.ollie_ap','LivingRoom_AP') %}
          Living Room
        {% elif is_state('sensor.ollie_ap', 'Office_AP')
        and states('sensor.ollie_ap_strength') | float > -50 %}
          Office
        {% elif is_state('sensor.ollie_ap', 'Outdoor_AP')
        and is_state('sensor.ollie_ap_strength') | float > -50 %}
          Office
        {% elif is_state('sensor.ollie_ap', 'Playroom_AP')
        and states('sensor.ollie_ap_strength') | float < -50 %}
          Kitchen
        {% elif is_state('sensor.ollie_ap', 'Playroom_AP')
        and states('sensor.ollie_ap_strength') | float > -50 %}
          Playroom
        {% else %}
          Unknown
        {% endif %}

Screenshot 2020-04-25 at 18.50.54

I feel like it could be to do with how I’ve pulled the attribute for the strength? Like if it’s not a number but a string of a number?

Looking at the full code:

  • ollie_ap and ollie_ap_strength need entity_id’s linking them to the source entity.

  • tests like states(‘sensor.ollie_ap’,‘Outdoor_AP’) should be like is_state(‘sensor.ollie_ap’, ‘Outdoor_AP’)
    It looks like you have some of these correct and some not

  • tests like is_state('sensor.ollie_ap_strength) | float should be states(‘sensor.ollie_ap_strength’) | float

I find the key with my template sensors is to use the Template tab in Developer Tools to test what I want to do before implementing - it helps me to build some of the more complex value_templates.

Points 2 and 3 are correct, but you don’t need to define the entities in the template sensor config. All of my template sensors update without explicitly defining the source entities in the config. The docs say “This can be used if the automatic analysis fails to find all relevant entities.”

Thanks all, it seems to be working now.

Thanks to @TazUk, I just changed everything like you said. Here’s the final code block:

ollie_location:
  value_template: >
    {% if is_state('sensor.ollie_ap','Outdoor_AP')
    and states('sensor.ollie_ap_strength') | float < -50 %}
      Outside
    {% elif is_state('sensor.ollie_ap','LivingRoom_AP') %}
      Living Room
    {% elif is_state('sensor.ollie_ap', 'Office_AP')
    and states('sensor.ollie_ap_strength') | float > -50 %}
      Office
    {% elif is_state('sensor.ollie_ap', 'Outdoor_AP')
    and states('sensor.ollie_ap_strength') | float > -50 %}
      Office
    {% elif is_state('sensor.ollie_ap', 'Playroom_AP')
    and states('sensor.ollie_ap_strength') | float < -50 %}
      Kitchen
    {% elif is_state('sensor.ollie_ap', 'Playroom_AP')
    and states('sensor.ollie_ap_strength') | float > -50 %}
      Playroom
    {% else %}
      Unknown
    {% endif %}

And thanks for the templates tip! Had no clue, saves me restarting everytime to test it.

Also, rookie error, misspelling of the sensor in the glance card…

Good to know, thanks.

I started doing it after reading to resolve some updating issues with some of my more bespoke summary templates, and it’s simply become part of my personal coding standards since then (most of which I forget why I do…)

1 Like

Glad you got it working.
The template tool is an absolute lifesaver for me.

1 Like

I believe you can reduce the template’s logic to this:

ollie_location:
  value_template: >
    {% set l = states('sensor.ollie_ap') %}
    {% set s = states('sensor.ollie_ap_strength') | float %}

    {% if l == 'Office_AP' %}
      Office
    {% elif l == 'LivingRoom_AP' %}
      Living Room
    {% elif l == 'Outdoor_AP') %}
      {{ 'Outside' if s < -50 else 'Office' }}
    {% elif l == 'Playroom_AP' %}
      {{ 'Kitchen' if s < -50 else 'Playroom' }}
    {% else %}
      Unknown
    {% endif %}
1 Like

That’s amazing! Thanks. Still trying to work out the Syntax but that makes a lot of sense.