Template works in Template Editor but not in configuration.yaml

I’m stumped here, maybe someone can help me.

When I paste the following into the Template Editor, it returns a correct on / off response. But when I add it to my config as a value_template, I get errors:

"{{ 'on' if "FD:81:5F:77:95:FF (Gregory's Pixel Buds Pro 2)" in state_attr('sensor.pixel_9_pro_xl_bluetooth_connection', 'connected_paired_devices') else 'off' }}"

When I put this into my config like this:

  - platform: template
    sensors:
      ear_buds:
        value_template: "{{ 'on' if "FC:91:5D:65:95:FF (Gregory's Pixel Buds Pro 2)" in state_attr('sensor.pixel_9_pro_xl_bluetooth_connection', 'connected_paired_devices') else 'off' }}"
        friendly_name: "Ear Buds"

I get the following error when I try to validate:

Error loading /config/configuration.yaml: while parsing a block mapping
  in "/config/configuration.yaml", line 149, column 9
expected <block end>, but found '<scalar>'
  in "/config/configuration.yaml", line 149, column 38

Any suggestions would be appreciated.

I’d have to test, but it looks like you have quotation issues with
Gregory's and the use of "" with FC:91:5D:65:95:FF (Gregory's Pixel Buds Pro 2)

Does this work with changing the entity ID from Gregory's to gregorys

{{'on' if 'FC:91:5D:65:95:FF (Gregorys Pixel Buds Pro 2)' in state_attr('sensor.pixel_9_pro_xl_bluetooth_connection', 'connected_paired_devices') | default([], true) else 'off'}}

2 Likes

I have a few suggestions.

  1. This should be a binary sensor as it has two states.

  2. You should use the new template integration for new template entities.

  3. Escape the single quote with another single quote. Or just match on the MAC address, that should be unique.

e.g.

configuration.yaml (not sensors.yaml)

template:
  - binary_sensor:
      - name: "Ear Buds"
        state: "{{ "FC:91:5D:65:95:FF" in state_attr('sensor.pixel_9_pro_xl_bluetooth_connection', 'connected_paired_devices') }}"

No need to use if/else. The template returns true or false which will be translated to on/off (or whatever you want with a device_class) .

As for why your sensor did not work, did you put - platform: template under sensor: in your configuration.yam file?

Or in the sensors.yaml file if you use includes.

2 Likes

Really nice, I slightly over complicated this by making a binary sensor with a different binary output :rofl:

1 Like

Thanks for the suggestions.

I kind of figured that the apostrophe in Gregory’s might be the issue, so I changed the name of the Bluetooth device in my Android settings. So now it shows up as a connected device as FC:91:5D:65:95:FF (Pixel Buds Pro 2) in Home Assistant.

I then used the helper template integration to add it as a binary sensor as suggested. Under “Template Optons” I put this:

“{{ ‘FC:91:5D:65:95:FF (Pixel Buds Pro 2)’ in state_attr(‘sensor.pixel_9_pro_xl_bluetooth_connection’, ‘connected_paired_devices’) }}”

The new sensor loads with no issues now, but shows the state as “off” all of the time. If I paste this same thing into the Template Editor it returns as “True” when I have the ear buds on, and “False” when I take them out. Working this way as it should.

If I only include the MAC address part of the attribute in the template statement, it doesn’t work at all in either method. Seems to need the whole thing, maybe because the attributes are returned as a LIST, and not a STRING.

I’m stumped here.

Using search() will allow you to use just the MAC and will work whether the list is empty, contains just the desired device, or contains multiple items.

template:
  - binary_sensor:
      - name: "Ear Buds"
        state: |
          {{ state_attr('sensor.pixel_9_pro_xl_bluetooth_connection', 'connected_paired_devices') 
          is search("FC:91:5D:65:95:FF") }}

If you need to specify multiple search term/MACs you can do so by using a | between the terms:

{{ state_attr('sensor.pixel_9_pro_xl_bluetooth_connection', 'connected_paired_devices') 
is search("FC:91:5D:65:95:FF|34:88:5D:9C:25:E2") }}
1 Like

This is the winner!
Thank you very much to all who helped.