A Foundational Home Assistant Sensor
Welcome to the next post in my series on the foundational configurations that form the bedrock of my Thinking Home. We’ve explored automations that react to simple events, but what about detecting more complex, nuanced states? How does our smart home know when it’s “bedtime,” when we’re “away,” or when it’s “movie time”? The truth is, it’s rarely just one thing.
Today, we’re moving beyond simple triggers to build a sensor that uses a weighted scoring system to infer a complex state. This “Weighted Confidence Sensor” is far more reliable than any single trigger because it understands that our daily routines are a collection of activities, not a single event.
The Problem: The Unreliable “Bedtime” Trigger
One of the biggest challenges in home automation is reliably detecting complex human states. A simple trigger, like turning off the bedside lamp, is brittle. What if you’re reading on a tablet? What if one person goes to bed before the other? Relying on a single condition often leads to automations that fire at the wrong time or, worse, not at all.
To truly create a “smart” home, the system needs to be able to look at the bigger picture. It needs to weigh multiple pieces of evidence to arrive at a confident conclusion. To illustrate this technique, let’s build a sensor that reliably detects “bedtime” in my home.
The Solution: A Weighted Confidence Sensor
Instead of a simple “if this, then that” approach, this sensor calculates a “confidence score.” It looks at a list of conditions, each with a different “weight” or level of importance. For example, my phone being on the charger is a strong indicator of bedtime (high weight), while the living room TV being off is a weaker indicator (low weight).
The sensor’s state only turns on
when the combined weight of all the true conditions passes a certain threshold (e.g., 80% of the total possible weight). This makes it incredibly resilient. If one or two conditions aren’t met, the sensor can still confidently determine that it’s bedtime.
How It Works
This sensor is built using the Template integration. The logic is contained entirely within the YAML configuration.
1. The Conditions List: At the heart of the sensor is a list of conditions. Each item in the list is a tuple containing two values: the condition to check (which evaluates to true
or false
), and its numerical weight. A crucial part of this is checking that a sensor is not unavailable
or unknown
before using its state; this prevents errors during Home Assistant restarts or network issues.
2. Calculating the Score: The template then calculates two values:
total_weight
: The sum of the weights of all conditions whose entities are currently available and providing a valid state.true_weight
: The sum of the weights of only the conditions that are currentlytrue
.
3. The Threshold: The sensor’s final state becomes on
only if the true_weight
is greater than or equal to a set percentage (in my case, 80%) of the total_weight
.
4. The Override: Life has exceptions. No matter what the score is, if our master bedroom door is open, the sensor is immediately forced to false
. This simple override takes the home out of a heightened security mode that is activated when we are asleep (bedtime is TRUE).
5. Debugging Attributes: To make tuning easy, the sensor includes two helpful attributes:
confidence
: Shows the current score as a percentage.conditions_met
: Provides a human-readable list of which specific conditions are currently passing.
How to Implement This in Your Home Assistant
This is a Template Binary Sensor. You can add the code directly to your configuration.yaml
file under the template:
section. However, for better organization, I recommend placing it in a separate file. To do this, create a file named template_sensors.yaml
in your /config/
directory and add the following line to your configuration.yaml
:
template: !include template_sensors.yaml
Then, you can place the code for this sensor, and any other template sensors, into that new file.
You can get the complete, up-to-date code from my GitHub repository:
View the Bedtime Detected Sensor on GitHub
To get it running, follow these steps:
1. Copy the Code: Start by copying the complete YAML configuration for the sensor into your chosen file (configuration.yaml
or template_sensors.yaml
).
2. Customize Your Conditions: This is where you adapt the sensor to your own life and devices.
- Go through the
conditions
list in thestate
andattributes
sections. - Replace my entities with your own (e.g., your phone’s charging sensor, your TV’s media player, your door sensors).
- Adjust the weights for each condition based on how important it is as an indicator of “bedtime” in your household.
- Add or remove conditions to create a complete picture of your evening routine.
3. Set Your Threshold: In the state template, you can adjust the confidence threshold by changing the 0.80 value to whatever percentage feels right for you.
This weighted confidence sensor is a powerful pattern that can be adapted to detect any number of complex states in your home—”leaving for work,” “movie time,” or “house is empty.” It’s a foundational piece that moves beyond simple triggers and allows Home Assistant to make more intelligent, human-like decisions.
How do you detect complex states in your home? Share your own solutions in the comments below or on GitHub.