Problems with template

Hello

I’m trying to track my phone using the android app and ibeacons. It all works but having problems with a template for the binary sensor. The value works in dev, but I keep getting this error

Configuration warnings
Invalid config for 'template' from integration 'binary_sensor' at binary_sensor.yaml, line 53: 'state' is an invalid option for 'binary_sensor.template', check: sensors->beacon_detected->state

I have this in binary_sensor.yaml

  - platform: template
    sensors:
      beacon_detected:
          friendly_name: Beacon Detected
          device_class: presence
          value_template: >-
          state: >
            {% if (state_attr('sensor.marks_s22_ultra_beacon_monitor', 'a0b13730-3a9a-11e3-aa6e-0800200c9a66_56780_63704') != None) 
            %}
              on
            {% else %}
              off
            {% endif %}

Any ideas please? It has been a long time since I played with my HA yaml

Since you are using the legacy format, you should not have the state: > in there… you can also get rid of the if:

- platform: template
  sensors:
    beacon_detected:
      friendly_name: Beacon Detected
      device_class: presence
      value_template: >-
        {{ state_attr('sensor.marks_s22_ultra_beacon_monitor', 'a0b13730-3a9a-11e3-aa6e-0800200c9a66_56780_63704') != None }}
1 Like

Thank you so much! I need to get all my automations re-done to the newer way of running them

You can always use the UI to create a template binary sensor helper, avoiding YAML and the legacy / modern complication.

Assuming your template works ( not sure about the != None bit…):

1 Like

Thanks for this, however I am now looking to change the sensor, so it works with any value and not just the 1 iBeacon, since I have 3 or 4 of them.
The sensor doing the monitoring will only have the values of the iBeacons it has in from the companion app that I have allowed it to find.

Any ideas please? Tried all morning and I keep getting no where. Thanks

Because of the way the sensor turns sensed beacons into attributes, I think the only option is to combine all the value queries with logical operators:

value_template: >-
  {% set ent = "sensor.marks_s22_ultra_beacon_monitor" %}
  {{ (state_attr(ent, 'a0b13730-3a9a-11e3-aa6e-0800200c9a66_56780_63704') != None) or 
  (state_attr(ent, 'some-unique-id-from-another-beacon') != None) or
  (state_attr(ent, 'another-unique-id-from-a-different-beacon') != None) }}

It might also work if you start from the attributes property and reject all the standard attributes, but you will need to test to see if it’s reliable for your use:

{{ states.sensor.marks_s22_ultra_beacon_monitor.attributes
| reject('in', ['options', 'device_class', 'icon', 'friendly_name'])
| list | count > 0 }}
1 Like

Thank you, will give it a test. My HA Docker setup in from 4 or 5 years ago, I just did the updates and hardly touched it since. Hence I have no real idea about the settings etc in the interface.

I struggled to know which { or } or ( or ) was wrong. So it kept flagging up errors. I tried to see the format of % set ent and was getting no where at all. Well it loaded but said unknown.

        value_template: >-
          {% set (ent = "sensor.marks_s22_ultra_beacon_monitor")}
          {{ (state_attr(ent, '0000ffe1-0000-1000-xxxx-00805f9b34fb_101_102') != None) or 
          (state_attr(ent, '0000ffe1-0000-1000-xxxx-00805f9b34fb_201_202') != None) or
          (state_attr(ent, '0000ffe1-0000-1000-xxxx-00805f9b34fb_301_302') != None) }}  

Close, only changed the first line and some spacing:

value_template: >-
          {% set ent = "sensor.marks_s22_ultra_beacon_monitor" %}
          {{ (state_attr(ent, '0000ffe1-0000-1000-xxxx-00805f9b34fb_101_102') != None) or 
             (state_attr(ent, '0000ffe1-0000-1000-xxxx-00805f9b34fb_201_202') != None) or
             (state_attr(ent, '0000ffe1-0000-1000-xxxx-00805f9b34fb_301_302') != None) }}

You can always try it in Developer Tools / Template, which will flag syntax errors like that — not always particularly helpful messages though:

1 Like

Thank you, you wouldn’t think I was a software engineer 25+ years ago :no_mouth:

1 Like