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