Template sensor based on attributes and if-elif-endif structure

Hi, I’m trying to create a template sensor based on attributes of a sensor.

The sensor is:

device_tracker.02b8b6be_e768_4cf3_9006_2e9c0463815c_100_1

and the attribute is:

source

Because its a room presence sensor, and if i’m not in the room, it will be unavailable, hence it should default to Not Home.

  1. Do I need to add some other information for this sensor like device_class, etc?

  2. For some odd reason, the template editor is not liking the syntax (after removing the header fluff at top), not sure what the syntax error is.

thanks for the help!

  sensors:
       room_presence:
         friendly_name: "Room Presence"
         value_template: >-
           {% if state_attr('device_tracker.02b8b6be_e768_4cf3_9006_2e9c0463815c_100_1', 'source', esp32-bluetooth-proxy-5e9b38) %}
             Basement Family Room
           {% elif state_attr('device_tracker.02b8b6be_e768_4cf3_9006_2e9c0463815c_100_1', 'source', esp32-bluetooth-proxy-299fcc) %}
             Family Room
           {% elif state_attr('device_tracker.02b8b6be_e768_4cf3_9006_2e9c0463815c_100_1', 'source', esp32-bluetooth-proxy-5a63a8) %}
             Bonus Room
           {% elif state_attr('device_tracker.02b8b6be_e768_4cf3_9006_2e9c0463815c_100_1', 'source', esp32-bluetooth-proxy-5f189c) %}
             Master Bedroom
           {% else %}
             Not Home
           {% endif %}

You have a few issues in your if and elif statements. You seem to have combined two different methods… and you are missing quote marks around the names of your sources.

Method 1: Use is_state_attr(). This is the preferred method

{% if is_state_attr('device_tracker.02b8b6be_e768_4cf3_9006_2e9c0463815c_100_1', 'source', 'esp32-bluetooth-proxy-5e9b38') %}

Method 2: Use a == comparison

{% if state_attr('device_tracker.02b8b6be_e768_4cf3_9006_2e9c0463815c_100_1', 'source') == 'esp32-bluetooth-proxy-5e9b38' %}

Either will work, but you can’t mix them up and both need quotes around the source.

There are also other methods that can produce the same results:

Using a Dictionary method
# Updated to current format 

template:
  - sensor:
      name: "Room Presence"
      state: >-
        {% set source = state_attr('device_tracker.02b8b6be_e768_4cf3_9006_2e9c0463815c_100_1', 'source') %}
        {% set mapper = { 
        'esp32-bluetooth-proxy-5e9b38': 'Basement Family Room',
        'esp32-bluetooth-proxy-299fcc': 'Family Room',
        'esp32-bluetooth-proxy-5a63a8': 'Bonus Room',
        'esp32-bluetooth-proxy-5f189c': 'Master Bedroom' } %}
        {{ iif( source in mapper.keys(), mapper.get(source), 'Not Home', 'unknown' ) }}
1 Like

thanks @Didgeridrew!

Do I add this to my sensor.yaml or my templates.yaml?

I’m having an issue saving it to either file.

in my templates.yaml, I already have the sensor: tab

but it does not seem to like if I add it as, this below, what am I missing here?

  room_presence:
    friendly_name: "Room Presence"
    value_template: >-
      {% set source = state_attr('device_tracker.02b8b6be_e768_4cf3_9006_2e9c0463815c_100_1', 'source') %}
      {% set mapper = { 
      'esp32-bluetooth-proxy-5e9b38': 'Basement Family Room',
      'esp32-bluetooth-proxy-299fcc': 'Family Room',
      'esp32-bluetooth-proxy-5a63a8': 'Bonus Room',
      'esp32-bluetooth-proxy-5f189c': 'Master Bedroom' } %}
      {{ iif( source in mapper.keys(), mapper.get(source), 'Not Home', 'unknown' ) }}

You’re trying to add a Template Sensor defined in legacy format into a file (templates.yaml) containing Template Sensors defined in modern format. You can’t combine the two formats in the same file (new style is in the template: domain whereas old style is in the sensor: domain).

Reference: Template Sensor

Thanks for the explanation.

Was this a recent change? I don’t know what I’m having a brain freeze for such a trivial thing, I have two files (three if you include the config.yaml, but I don’t put sensors in there):

templates.yaml- first line in this file is - sensor:
sensors.yaml - each sensor begins with - platform:

when I do a check configuration, it fails all the time when I try to put this new sensor in there. The syntax is fine.

No; easily over a year ago.

It fails for the reason I explained above.

ok thanks, problem was it didn’t like value_template, had to switch that to state as per the template sensor link provided.

Thanks!

It’s more than that. The new format doesn’t accept friendly_name. It uses name which is used for both the friendly name and the entity_id. Plus the YAML structure is different.

thanks for the information. Here’s the sensor as is, and seems to be working well with no errors or issues.

Friendly name is an attribute here.

    ### Room Presence ##
    - name: "Room Presence"
      unique_id: 9c484940-2df3-4t43-bffd-f18424a5d709
      attributes:
        friendly_name: "Room Presence"
      icon: >-
        {% set state = "device_tracker.02b8b6be_e768_4cf3_9006_2e9c0463815c_100_1" %}
        {% if is_state( state, "home") %}
          mdi:home
        {% elif is_state( state, "not_home") %}
          mdi:home-outline
        {% else %}
          mdi:crosshairs-question
        {% endif %}
      state: >
        {% set source = state_attr('device_tracker.02b8b6be_e768_4cf3_9006_2e9c0463815c_100_1', 'source') %}
        {% set mapper = { 
        'esp32-bluetooth-proxy-5e9b38': 'Basement Family Room',
        'esp32-bluetooth-proxy-299fcc': 'Family Room',
        'esp32-bluetooth-proxy-5a63a8': 'Bonus Room',
        'esp32-bluetooth-proxy-5f189c': 'Master Bedroom' } %}
        {{ iif( source in mapper.keys(), mapper.get(source), 'Not Home', 'unknown' ) }}
1 Like

Which means it won’t be the entity’s actual friendly_name.

Here’s a simple example:

The following configuration will produce sensor.example_sensor. However, the entity’s friendly_name will not be “This is an example sensor”. The entity’s friendly_name is the value specified in name.

  - sensor:
      - name: Example Sensor
        state: '{{ true }}'
        attributes:
          friendly_name: 'This is an example sensor'

Summary

If you define an attribute named friendly_name, it won’t be used to create the entity’s actual friendly_name.