Binary Sensor Template Format

I’m reading that the format of a binary sensor template has changed and am having trouble getting my template to not have errors in it.

I have a monoprice door sensor that reports a value of 23 as open and 22 as closed. I’ve made a template and put the following:

##test binary sensor
- platform: template
  sensors:
    sensor name:
      friendly_name: "Entry Garage Door Sensor"
      value_template: "{% if state_attr('sensor.garage_entry_sensor_access_control_door_state', 'value') == 23 %}
closed
{% else %}
open
{% endif %}"

The first line of “- platform: template” has an error of " property platform is not allowed"
The 2nd line of “sensors:” has an error of " property sensors is not allowed"

Any idea what I am doing wrong. i do have this file referenced in my configuration.yaml to include this yaml file as well.

This is the new format for Template Sensors and it’s placed under the template key (as shown below) not the sensor key.

template:
  - binary_sensor:
      - name: Entry Garage Door Sensor
        state: "{{ is_state_attr('sensor.garage_entry_sensor_access_control_door_state', 'value', 23) }}"

If you have this template: ! include templates.yaml in your configuration.yaml file then place the example above in templates.yaml without the first line.

I’m unfamiliar with the Monoprice device you are using but is the value 23 literally in an attribute called value?

Thanks for this, two follow ups:

in your example that is just printing the state of value 23, correct? can i do if statements like i had in the original post to get values of open or closed?

once I have this template, does this show up as a device? basically i’m just wondering how I’ll reference this template for automation after the template is made.

No. The template is using the is_state_attr function to determine if the attribute named value, belonging to the entity named sensor.garage_entry_sensor_access_control_door_state contains 23. If it does then the template reports true otherwise it reports false. Boolean true/false is valid to make the binary_sensor’s value on/off.

It’s a Template Sensor so it will appear as an entity, not a device (the two terms, as used in Home Assistant, represent different things). After you add the YAML shown above as instructed, you should execute Developer Tools > YAML > Check Configuration to confirm you have not introduced YAML syntax errors. If all is well, execute Developer Tools > YAML > Reload Template Entities to load the new Template Sensor into Home Assistant. If sensor.entry_garage_door_sensor appears in Developer Tools > States view then it means it was loaded successfully.

BTW, are you certain sensor.garage_entry_sensor_access_control_door_state has an attribute named value? Post a screenshot of this sensor as it appears in Developer Tools > States.

just updated my yaml files and see this after a restart…looking good! thanks for the help!

@123 my automation works as well! thanks for all the help!

1 Like

@123, if i want to add an additional door sensor that reads the same way, do i simply add an additional line in the configuration file like this?

- binary_sensor:
      - name: Entry Garage Door Sensor
        state: "{{ is_state_attr('sensor.garage_entry_sensor_access_control_door_state', 'value', 23) }}"
      - name: Front Door Sensor
        state: "{{ is_state_attr('sensor.front_door_entry_sensor_access_control_door_state', 'value', 23) }}"

Yes. That should create two binary sensors.

1 Like

Hi!
Could you help please with same problem?
I have many binary sensors for different types of devices, based on their state.
For example TV plug, if measured power is more than 10 Watts, it means that TV is on, and so on.

template:
  - binary_sensor:
      - name: "TV"
        state: >
          {{ states('sensor.plug_tv_power')|float(0) > 10}}
  - binary_sensor:
      - name: "TV box"
        state: >
          {{ states('sensor.plug_tvbox_power')|float(0) > 10}}

This template code and other sensors works good while it is in configuration.yaml.
But I want to create each file for each binary sensor (possibly one file for all template binary sensors).
So I edited configuration.yaml and added this line:

binary_sensor: !include_dir_merge_list includes/bin_sensor

I created a single file for each sensor that i have, and put it into includes/bin_sensor folder.
Each file looks like this:

template:
  - binary_sensor:
      - name: "TV"
        state: >
          {{ states('sensor.plug_tv_power')|float(0) > 10}}

Configuration check validates ok, but no any new sensors created in system after restart.
What I’m doing wrong?

The first line of each file should not include the template: key. Remove it.

already tried it. so checking yaml config is stucks and freezes

Then there are other problems in your configuration and I can’t guess what they might be.

I’ve put in configuration.yaml line for template file
template: !include includes/templates.yaml
not dir merge.

Also in templates.yaml file added all sensors like this:

- binary_sensor:
    - name: "TV"
      state: >
        {{ states('sensor.plug_tv_power')|float(0) > 10}}
- binary_sensor:
    - name: "TV box"
      state: >
        {{ states('sensor.plug_tvbox_power')|float(0) > 10}}

and it works now.

The question is - how can i do it via dir merge to create each sensor per file?

Sorry, i’ve added this in template.yaml

  - binary_sensor:
      - name: "Tv cameretta power"
        state: >
          {{ states('sensor.presa_tv_cameretta_power')|float(0) > 10}}

but i receve in studio code " Missing property State" and on log ha invalid template configuration

did you solve this?

There has to be the template: block appearing once, somewhere.