Trouble with a template sensor

I’m certain I’m doing something stupid, but am trying to get this template sensor to work.

‘sensor.bluetooth_jim_downstairs’ is a bluetooth MQTT sensor that I’ve confirmed evaluates to the correct confidence, and the elseif is an owntracks sensor that also works on its own. I can’t get ‘jim’ to show home or away.

Thanks.

  - platform: template
    sensors:
      jim:
        friendly_name: "Jim"
        value_template: >-
          {% if sensor.bluetooth_jim_downstairs > 10 %}
            Home
          {% elif is_state('device_tracker.jimphone_phone', 'workj') %}  
            Work  
          {% else %}
            Away
          {% endif %}

you need to provide an entity_id or manually update the sensor. This is a new “feature” since 0.81
You also need to update your first if statement. An entity state is a string, so you need to convert to an int (thanks @pnbruckner)
Finally you need to use either states.sensor.bluetooth_jim_downstairs.state or states('sensor.bluetooth_jim_downstairs') to get an entity’s state

  - platform: template
    sensors:
      jim:
        friendly_name: "Jim"
        entity_id: sensor.bluetooth_jim_downstairs
        value_template: >-
          {% if (states.sensor.bluetooth_jim_downstairs.state | int) > 10 %}
            Home
          {% elif is_state('device_tracker.jimphone_phone', 'workj') %}  
            Work  
          {% else %}
            Away
          {% endif %}
1 Like

Um, not exactly. What’s new in 0.81 is, if it can’t figure out what entities the template uses, it no longer evaluates the template for every state change in the system (which is what it used to do in this case. Instead it never evaluates the template automatically, which would mean you either have to specify the entity_id’s manually, so it knows what entities to watch, or you have to force it to update via a service call.)

But if you use standard ways of referencing entities – such as states('domain.object_id'), state_attr('domain.object_id', 'attribute') or states.domain.object_id..., it will be able to find those entities, so you don’t need to specify them manually via the entity_id parameter.

The issue with the template sensor in the OP is, of course, not using sensor.bluetooth_jim_downstairs correctly, which you’ve pointed out. Once that is fixed it should work, even without using the entity_id parameter.

(BTW, if entity_id is used, you need to make sure to specify all entities used by the template. In this case that would have been both sensor.bluetooth_jim_downstairs and device_tracker.jimphone_phone. :slight_smile:)

EDIT: Also, I always recommend using the states or state_attr function when possible. So, altogether, I would recommend:

  - platform: template
    sensors:
      jim:
        friendly_name: Jim
        value_template: >
          {% if states('sensor.bluetooth_jim_downstairs') | int > 10 %}
            Home
          {% elif is_state('device_tracker.jimphone_phone', 'workj') %}  
            Work  
          {% else %}
            Away
          {% endif %}

EDIT2: @Buckeyes1995, are you sure you want this logic? If sensor.bluetooth_jim_downstairs is 10 or less, it will evaluate to Away, even if device_tracker.jimphone_phone is home.

Thanks!

Yeah, good catch. I’ll put in a check for the device tracker being home as well.