Custom Binary Sensors

Using the binary sensor component, we can define virtual sensors in our Home Assistant configuration that allow us to track states derived from related input data.

Add the following to your configuration to start using binary sensors.

binary_sensor:
  - platform: template
    sensors:
      sensor_1:
        ...
      sensor_2:
        ...

Laundry sensor

Let’s say we want to receive notifications when laundry cycles start and end. Since I don’t think its worth investing in smart appliances, I’ve figured out a way to use virtual sensors and Home Assistant instead.

The following sensor uses a power meter attached to my laundry appliances to determine the state of the cycle (whether a load is running or not).


load_on:
  friendly_name: "Load is running"
  value_template: '{{ states.sensor.laundry_power.state > 5}}'
  delay_off:
    minutes: 5

The idea is that if it is washing a load of laundry, the power consumption will likely be more than 5 watt. I’ve added a delay_off such that the sensor does not flip to off when the washer drum is stationary for a few seconds.

Using this virtual sensor in an automation, we can send ourselves a notification:


- alias:             Laundry Notification
  trigger:
    platform:        state
    entity_id:       binary_sensor.load_on
  action:
    - service:       notify.pushbullet
      data_template:
        message:     >
          Washing load{% if trigger.to_state.state == 'true' %} started{% else %} finished{% endif %} at {{ now().time() }}.

I usually set up the laundry but use the machines delay start feature to schedule the start time such that the load finishes when im expected to come home from work the next day. The notifications keep me up to date on the program cycle and serve as a reminder.

Simples.

I actually had a power meter hooked up to the laundry and TV already, so it was just a matter of defining the binary_sensor and automation in Home Assistant.

TV Sensor

Applying a similar configuration, we can create a sensor telling us whether we left the TV on or not.


tv_on:
  friendly_name: "TV is on"
  value_template: "{{ states.sensor.tv_power.state > 20 }}"

I am using this sensor in an automation that turns off all devices at night (in case I forget to turn off a light before going to bed.)

For determining whether someone is watching TV, I can use the media_player.cast component, which is more relable than basing a virtual sensor on TV power consumption. I don’t have cable TV hooked up to my TV as I watch Netflix and YouTube on Chromecast. If you have other inputs, then defining this tv_on sensor is a nifty way for you to check whether the TV is running. You might even be able to distinguish between different states using the power consumption for each state.

Say playing a movie consumes 40 to 40w, but just playing some music consumes 20watts. It’s not perfect, but you might be able to make it work.