Help with Template.Sensor

Hey everybody, i’m trying to split out my sensor.yaml file. I have a door sensor on my garage and i’m trying to give a custom icon: mdi:door-open when opened and mdi:door-closed for when its closed.

I got the icon to work properly but the state does not change and is stuck on “Off”. Does anyone know what i’m doing wrong here? Below is my code:

type or paste code here- binary_sensor:
  - name: "Garage Door Status"
    state: >
          {% if is_state('binary_sensor.lumi_lumi_sensor_magnet_aq2_on_off','on') %}
            fff
          {% else %}
            book
          {% endif %}`

      
      
    icon: >
      {% if is_state("binary_sensor.lumi_lumi_sensor_magnet_aq2_on_off", "on") %}
        mdi:door-open
      {% else %}
        mdi:door-closed
      {% endif %}
    

If this is a binary_sensor the state can only be on or off (true/false etc.) You have defined the state as “fff” or “book”.

Your state template should look like this

state: "{{ is_state('binary_sensor.lumi_lumi_sensor_magnet_aq2_on_off','on') }}"
1 Like

This worked seaonmat, thanks for the assist. The on/off specific states for the binary_sensor was the key. What I was ultimately trying to do was to make this sensor have states of “Open/Closed” instead of “On/Off”. With your code, i just changed the device_class to “opening” and it worked great. Thanks again.

Here’s the final code that worked in case anyone needs it:

binary_sensor:
  - name: "Garage Door Status"
    state: "{{ is_state('binary_sensor.lumi_lumi_sensor_magnet_aq2_on_off','on') }}"
    device_class: opening
      
      
    icon: >
      {% if is_state("binary_sensor.lumi_lumi_sensor_magnet_aq2_on_off", "on") %}
        mdi:door-open
      {% else %}
        mdi:door-closed
      {% endif %}
    ```

For future reference, this is explained in the documentation: device_class

It applies to both Template Sensor and Template Binary Sensor, however the available device classes for each one aren’t identical (for obvious reasons).

Device classes for:

  1. Binary Sensor
  2. Sensor

Actually. I thought i was good, but apparently not. I added a couple of more sensors and now when i try to validate the config file, it hangs. Are you able to see what I’m doing wrong? My config file reads:


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

# Text to speech
tts:
  - platform: google_translate

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml
light:  !include light.yaml 
sensor: !include sensor.yaml
template: !include template.yaml


wake_on_lan:


and my template.yaml file is below:

binary_sensor:
  - name: "Garage Door Status"
    state: "{{ is_state('binary_sensor.lumi_lumi_sensor_magnet_aq2_on_off','on') }}"
    device_class: opening
    icon: >
      {% if is_state('binary_sensor.lumi_lumi_sensor_magnet_aq2_on_off', "on") %}
        mdi:door-open
      {% else %}
        mdi:door-closed
      {% endif %}
  - name: "J Door Status"
    state: "{{ is_state('binary_sensor.lumi_lumi_sensor_magnet_aq2_4ca7c507_on_off','on') }}"
    device_class: opening
    icon: >
      {% if is_state('binary_sensor.lumi_lumi_sensor_magnet_aq2_4ca7c507_on_off', "on") %}
        mdi:door-open
      {% else %}
        mdi:door-closed
      {% endif %}
  - name: "Z Door Status"
    state: "{{ is_state('binary_sensor.lumi_lumi_sensor_magnet_aq2_f282c507_on_off','on') }}"
    device_class: opening
    icon: >
      {% if is_state('binary_sensor.lumi_lumi_sensor_magnet_aq2_f282c507_on_off', "on") %}
        mdi:door-open
      {% else %}
        mdi:door-closed
      {% endif %}
    

The only thing I can see right now, is that the syntax highlighting changes when you use double quotes in the icon template.
I wouldn’t have thought, this would be a problem but who knows …

I saw that and it still wasn’t working. That said, after hanging during the validation all afternoon yesterday, i tried it this morning and it worked perfectly. Must have been something else hanging everything up. Thanks so much again for your help seasonmat! Have a good one!

it’s because you’re using device_class: opening. Use device_class garage door in combination with customization and you’re configuration drops to just:

customize.yaml

binary_sensor.lumi_lumi_sensor_magnet_aq2_on_off:
  device_class: garage
binary_sensor.lumi_lumi_sensor_magnet_aq2_4ca7c507_on_off:
  device_class: garage
binary_sensor.lumi_lumi_sensor_magnet_aq2_f282c507_on_off:
  device_class: garage