Template and binary_sensor always OFF, again

Hi

I’m not the first one asking about it, I know, I read few posts on this subject and am still stuck.

I have in my configuration.yaml file:

template: !include template.yaml

template.yaml reads:

- binary_sensor:
    - name: garage_door_state
      unique_id: garage_door_state
      device_class: window
      state: >
        {% if states('binary_sensor.door_sensor_1') == 'on' %}
          'open'
        {% else %}
          'closed'
        {% endif %}
        icon:
        {% if states('binary_sensor.door_sensor_1') == 'on' %}
          mdi:garage-open
        {% else %}
          mdi:garage
        {% endif %}

binary_sensor.door_sensor_1 works OK, tested in template editor.
garage_door_state is always ‘off’ and shows on dashboard as ‘Closed’ due to ‘device_class: window’

Doesn’t matter what I put in the comparison, result is always ‘off’.
Also the ‘icon:’ statement does nothing.
What am I missing? After hours of reading forum where there is a mix of old and new way of doing this and reading documentation, i am stuck.

Appreciate any help.
Regards,
Chris

To start I see an indent issue with icon: Should be aligned with state:

I’d use a cover template

This much shorter version will do everything you tried to:

- binary_sensor:
    - name: Garage Door State
      unique_id: garage_door_state
      device_class: garage_door
      state: "{{ is_state('binary_sensor.door_sensor_1','on') }}"
      availability: "{{ has_value('binary_sensor.door_sensor_1') }}"

Apart from the indentation issue you were also missing the multi-line character “>” after icon: Like you had for state:.

You can give the binary sensor a pretty name with spaces and capital letters, this will be converted to binary_sensor.garage_door_state for you.

Also templates for binary sensors should return true or false. The device class will translate that to Open or Closed.

There’s also no need for an icon template, the device class will supply open and closed garage icons. You just need to pick the correct device class.

See: https://www.home-assistant.io/integrations/binary_sensor/#device-class

The availability template will make the state unavailable instead of off if the door sensor is neither on nor off.

Finally you probably don’t even need a template sensor. You can do this from the frontend by clicking on binary_sensor.door_sensor_1 then clicking on the cog icon at the top of the pop-up card:

You can change the name and entity id to whatever you want too. But be careful if you have already used the entity id elsewhere in automations or scripts. You will need to change them too.

Appreciate the cleaner method! I need to share that in another recent thread.

I just edited my post. They probably don’t even need a template sensor.

1 Like

I missed that UI feature update.

It does rely on binary_sensor.door_sensor_1 having a unique id. If it does not have one it is not possible to edit its appearance from the frontend.

1 Like

Hi
Thank you very much for corrections and suggestions.

Indentations, have to do some more homework on it. Thanks for spotting this.
Will consider using cover template you suggested, LiQuid_cOOled.

With
state: “{{ is_state(‘binary_sensor.door_sensor_1’,‘on’) }}”

and correct “icon:>” indentation
it was OK.

I learned one more thing.
When I created a dashboard card, I specified an icon, as suggested by “Entity Card Configuration”.
This prevented displaying of open/close door icons specified in my template or those provided by the system according to device-class.
I did the same mistake when I first configured the sensor.

I ended up using the shortest version provided by Tom.

And you are right, Tom, I don’t really need a template sensor, it was just my first template-exercise :slight_smile:

Thank you all very much. I really appreciate your very kind and helpful attitude.

Regards,
Chris

1 Like

Yep that will override the device class icons. I’ve asked for two icons (one for on and one for off, I don’t know if it will ever happen though. See: WTH can't we define two icons?

2 Likes