Legacy binary_sensor template deprecation

Today, after updating HA I was presented with 5 repairs all with the same issue regarding the deprecation of the legacy platform: template stating I need to migrate to the modern template syntax:

I took a backup of my existing config and replaced it with the (cleaned up) provided config shown in the screenshot above, but it doesn’t work.

The config I have is:

template:
  - binary_sensor:
      - name: Teams Meeting
        default_entity_id: binary_sensor.teams_meeting
        icon:
          { if is_state("binary_sensor.meeting_state_is_in_meeting", "on") }
			    mdi:phone
          { else }
            mdi:phone-off
          { endif }
        state:
          { if is_state("binary_sensor.meeting_state_is_in_meeting", "on") }
            on
          { else }
            off
          { endif }

And when checking the configuration in Developer tools, I get this error:

Configuration errors
Error loading /config/configuration.yaml: while scanning for the next token
found character '\t' that cannot start any token
  in "/config/binary_sensor.yaml", line 7, column 1

Although I have been using HA for a few years, I am not really experienced in the yaml config side of things…

See this thread - especially the first post.

The \t was bugging me, so I pasted the code into a linux box and it showed some incorrect formatting so I corrected that and now get:

template:
  - binary_sensor:
      - name: Teams Meeting
        default_entity_id: binary_sensor.teams_meeting
        icon:
          { if is_state("binary_sensor.meeting_state_is_in_meeting", "on") }
            mdi:phone
          { else }
            mdi:phone-off
          { endif }
        state:
          { if is_state("binary_sensor.meeting_state_is_in_meeting", "on") }
            on
          { else }
            off
          { endif }

You are mixing tabs and spaces inside yaml, that’s a no-no. You have to use tabs or spaces, not both.

That’s what the \t was referring to.

1 Like

Agree with petro, YAML is hung up on spacing. You may have to “de-pretty” your expression.

All of your delimiters are missing their %

{ if is_state("binary_sensor.meeting_state_is_in_meeting", "on") }

should be:

{% if is_state("binary_sensor.meeting_state_is_in_meeting", "on") %}

Could it be you were using an editor that decided it replace spaces with tabs?

edit: oh, I see I’m a bit late to the party :grinning:

1 Like

Thanks all, I have ensured I am only using spaces in the config now. Suspected it was something like that, but posted before checking (my bad).

I have changed it to this as I think you are saying:

template:
  - binary_sensor:
      - name: Teams Meeting
        default_entity_id: binary_sensor.teams_meeting
        state:
          {% if is_state("binary_sensor.meeting_state_is_in_meeting", "on") %}
            on
          {% else %}
            off
          {% endif %}

But checking the configuration gives me this error now:

Error loading /config/configuration.yaml: while scanning for the next token
found character '%' that cannot start any token
  in "/config/binary_sensor.yaml", line 6, column 12

put a | behind that line to make it a multiline

state: |
1 Like

You’re missing the multi-line quote indicator:

template:
  - binary_sensor:
      - name: Teams Meeting
        default_entity_id: binary_sensor.teams_meeting
        state: |
          {% if is_state("binary_sensor.meeting_state_is_in_meeting", "on") %}
            on
          {% else %}
            off
          {% endif %}

Beat you to it :grinning:

That now gives this warning:

Invalid config for 'binary_sensor' at configuration.yaml, line 18: required key 'platform' not provided

Can the warning be ignored?

template doesn’t go in binary_sensor.yaml

There are multiple examples in the thread @PecosKidd linked above.

I promise I did look there before @PecosKidd mentioned it and tried some, but couldn’t get it to work. I probably missed the | for multilines.

Thank you all!

1 Like

How to do this in split yaml files? Because I get this error :

Invalid config for 'template' at entities/templates/binary_sensors/smoke.yaml, line 2: 'binary_sensor' is an invalid option for 'template', check: binary_sensor->0->binary_sensor Invalid config for 'template' at entities/templates/binary_sensors/smoke.yaml, line 2: required key 'state' not provided
---
binary_sensor:
  - default_entity_id: binary_sensor.rooksensor_overloop
    icon: '{{ ''mdi:smoke-detector-variant-alert'' if this.state|int(default=0) >
      0 else ''mdi:smoke-detector-variant-off'' }}'
    name: Rooksensor Overloop
    state: '{{ states.binary_sensor.smoke_sensor_overloop_smoke_detected.state }}'

  - default_entity_id: binary_sensor.rooksensor_overloop
    icon: '{{ ''mdi:smoke-detector-variant-alert'' if this.state|int(default=0) >
      0 else ''mdi:smoke-detector-variant-off'' }}'
    name: Rooksensor Keuken
    state: '{{ states.binary_sensor.smoke_sensor_keuken_smoke_detected.state }}'

  - default_entity_id: binary_sensor.rooksensor_zolder
    icon: '{{ ''mdi:smoke-detector-variant-alert'' if this.state|int(default=0) >
      0 else ''mdi:smoke-detector-variant-off'' }}'
    name: Rooksensor Zolder
    state: '{{ states.binary_sensor.smoke_sensor_zolder_smoke_detected.state }}'

you have to show how you included it.

Sure

My configuration.yaml:

# Configure a default setup of Home Assistant (frontend, api, etc)
default_config:

homeassistant:
  customize: !include customize.yaml

  # Load packages
  packages: !include_dir_named packages

automation: !include automations.yaml
#script: !include scripts.yaml
scene: !include scenes.yaml

And then in packages folder I have a template.yaml:

---
template: !include_dir_list ../entities/templates

And in entities/templates dir I have a binary_sensors folder with a file smoke.yaml:

---
binary_sensor:
  - default_entity_id: binary_sensor.rooksensor_overloop
    icon: '{{ ''mdi:smoke-detector-variant-alert'' if this.state|int(default=0) >
      0 else ''mdi:smoke-detector-variant-off'' }}'
    name: Rooksensor Overloop
    state: '{{ states.binary_sensor.smoke_sensor_overloop_smoke_detected.state }}'

  - default_entity_id: binary_sensor.rooksensor_overloop
    icon: '{{ ''mdi:smoke-detector-variant-alert'' if this.state|int(default=0) >
      0 else ''mdi:smoke-detector-variant-off'' }}'
    name: Rooksensor Keuken
    state: '{{ states.binary_sensor.smoke_sensor_keuken_smoke_detected.state }}'

  - default_entity_id: binary_sensor.rooksensor_zolder
    icon: '{{ ''mdi:smoke-detector-variant-alert'' if this.state|int(default=0) >
      0 else ''mdi:smoke-detector-variant-off'' }}'
    name: Rooksensor Zolder
    state: '{{ states.binary_sensor.smoke_sensor_zolder_smoke_detected.state }}'

use

template: !include_dir_merge_list ../entities/templates

Then name the files whatever you want and keep the content’s what you already have but add a - in front of binary_sensor:.

- binary_sensor:
  - default_entity_id: ....
    ...

You won’t be able to use sub folders, so just name it smoke.yaml and put all template entities related to smoke inside it. e.g.

- binary_sensor:
  - default_entity_id: ....
    ...
- switch:
  - default_entity_id: switch.foo
     ....
- light:
  - default_entity_id: light.foo
     ....

Thanks! That works :smiley: