Sensor template not working for proximity

Hi

I need to modify the name of a proximity atribute and wantend to create a template sensor in my sensors.yaml
But for some reason I get nothing out of it and the template sensor keerps saying “onbekend”

What am I doiing wrong here

- platform: template
  sensors:
    template_proximity_vincent:
      friendly_name: “proximity_vincent_tp"
      value_template: >
        {% if states ('states.proximity.vincent.attributes.dir_of_travel') == 'arrived' %} Aangekomen
        {% else %}
        {% endif %}
        {% if states ('states.proximity.vincent.attributes.dir_of_travel') == 'towards' %} Richting huis
        {% else %}
        {% endif %}
        {% if states ('states.proximity.vincent.attributes.dir_of_travel') == 'away_from' %} Gaat weg
        {% else %}
        {% endif %}
        {% if states ('states.proximity.vincent.attributes.dir_of_travel') == 'unknown' %} Onbekend
        {% else %}
        {% endif %}
        {% if states ('states.proximity.vincent.attributes.dir_of_travel') == 'stationary' %} Geen beweging
        {% else %}
        {% endif %}
        {% if states ('states.proximity.vincent.attributes.dir_of_travel') == 'not set' %} Geen vermelding
        {% else %}
        {% endif %}

You have two issues that, together, are causing the wrong value to be returned.

  1. Your templates are mixing the states object method and the state functions. The states()functions expect an entity ID string as its input, not a state object. By having a state object string instead, the rendered value of the function is unknown, which is leading to the template returning "Onbekend”.
  2. When you want the value of an attribute you need to use state_attr() not states().

Instead of using a long chain of if/thens, you can use a dictionary and the get() method to retrieve the desired value.

- platform: template
  sensors:
    template_proximity_vincent:
      friendly_name: “proximity_vincent_tp"
      value_template: >
        {% set mapper = {
        'arrived': 'Aangekomen',
        'towards': 'Richting huis',
        'away_from': 'Gaat weg',
        'unknown': 'Onbekend',
        'stationary': 'Geen beweging',
        'not set': 'Geen vermelding' } %}
        {{ mapper.get( state_attr('proximity.vincent', 'dir_of_travel') ) }}
3 Likes

That was the solution indeed

Thank you very much!

1 Like