Add missing classes to Template Binary Sensor for Homekit support

Adding new device classes to Template Binary Sensor would help users be able to integrate incompatible sensors that reads in non-binary values (i.e: Nest Protect).

  • CO > Carbon Monoxide
  • CO2 > Carbon Dioxide

On top of that, add Homekit Component support for CO class binary sensors (currently only accepts CO2).

Thx!

CO class binary sensors can be added to HomeKit with:

device_class: gas

Adding new device classes just for template sensors doesn’t make much sense. They are there to categorize existing sensors. However you can use the device_class parameter of the binary_sensor if you want to, https://www.home-assistant.io/components/binary_sensor.template/#device_class

Had no idea “gas” was meant for Carbon Monoxide. HAP specs have also services to detect gas leaks, which could be misleading. (My case…)

Thanks, I’ll try that.

I’m not a big fan either, but it works for now.

Take a look at: https://github.com/home-assistant/home-assistant/blob/dev/homeassistant/components/homekit/type_sensors.py#L33-L45
This might help better identify which classes map to which services. Especially since you have already looked at the HAP docs :wink:

Wow, super cool stuff. I’ll dig into this to make more intelligent suggestions in the future. :smiley:

1 Like

Curious to if you found a solution for the Nest protect sensors in HomeKit? Currently my Nest only shows the binary sensor for occupancy and it’d be more helpful to show the status of the smoke and/or CO detector… I’ve done some digging but cannot find a solution anywhere

Hey Mike.
Yes I did. I had to create ghost sensors that read from the original ones.
Since Nest sensors won’t report standard values, the ghost sensors will be triggered if they read both “Warning” and “Emergency” on Nest.

Keep the “_status” in the end of the entity name when you customise.

Here is the cake recipe: :smiley:

binary_sensor:
  - platform: template
    sensors:
      smoke_kitchen:
        friendly_name: "Nest Smoke"
        device_class: smoke
        value_template: >-
          {{ is_state('sensor.kitchen_nest_protect_smoke_status', 'Warning')
             or is_state('sensor.kitchen_nest_protect_smoke_status', 'Emergency') }}
      co_kitchen:
        friendly_name: "Nest Carbon"
        device_class: gas
        value_template: >-
          {{ is_state('sensor.kitchen_nest_protect_co_status', 'Warning')
             or is_state('sensor.kitchen_nest_protect_co_status', 'Emergency') }}
1 Like

I believe I didn’t tag on you reply properly. Check the topic for the solution on the Nest Protect.

Excellent work.

Now I wonder if this could be adapted to other sensors to show them in homekit. I wonder if we can show the status of a NAS for example…

Thanks again for replying

Sure. You can make a binary sensor of anything.
You can set the class as “occupancy” so it’s nothing alarming on your Homekit and set the values to trigger the sensor. Maybe you will want to trigger it only when it’s offline…

binary_sensor:
  - platform: template
    sensors:
      nas_state:
        friendly_name: "NAS State"
        device_class: occupancy
        value_template: >-
          {{ is_state('sensor.nas_status', 'Offline') }}
2 Likes

Sorry to resurrect this older post, I was wondering if anybody know if it is possible to add a binary_sensor into home kit to look like a lock in the home app. I tried adding device_class: lock in the customize.yaml but shows up as a occupancy sensor.
Example below.

image

No. You can’t. But you could make a template component that gets triggered from the sensor.

Just get creative. :slight_smile:

1 Like

image

Thank you so much, it’s perfect! I didn’t know we could do that!

I tried but no luck, still show up as occupancy sensors, what am I missing?

binary_sensor:
  - platform: template
    sensors:
      humidifier_needs_water:
        delay_on:
          minutes: 5
        value_template: "{{ state_attr('switch.humidifier','current_power_w')|float < 8
          and is_state('switch.humidifier', 'on') }}"
        device_class: safety
      garage_left_open:
        device_class: smoke
        delay_on:
          minutes: 10
        value_template: "{{ is_state('binary_sensor.garage_door','on')
          or is_state('binary_sensor.carport','on') }}"