To create a template sensor in Home Assistant that combines two Boolean inputs and outputs true only when both inputs are true, you can use the and logical operator in a template. Here's how you can set it up:
YAML Configuration
Add the following to your configuration.yaml or through the UI in the "Helpers" section under "Template Sensors":
sensor:
- platform: template
sensors:
combined_boolean_sensor:
friendly_name: "Combined Boolean Sensor"
value_template: >-
{{ is_state('boolean.input1', 'on') and is_state('boolean.input2', 'on') }}
Explanation:
platform: template: Specifies that this is a template sensor.
sensors: Defines the list of template sensors.
combined_boolean_sensor: The unique ID for your sensor.
friendly_name: The display name for your sensor in the UI.
value_template: The template that defines the logic for the sensor's value.
is_state('boolean.input1', 'on'): Checks if the first Boolean input (boolean.input1) is on (i.e., true).
is_state('boolean.input2', 'on'): Checks if the second Boolean input (boolean.input2) is on (i.e., true).
and: Ensures both conditions must be true for the sensor to be true.
Example with Binary Sensors
If your inputs are binary sensors instead of Boolean inputs, the configuration remains the same. For example, if you have binary sensors binary_sensor.sensor1 and binary_sensor.sensor2:
sensor:
- platform: template
sensors:
combined_binary_sensor:
friendly_name: "Combined Binary Sensor"
value_template: >-
{{ is_state('binary_sensor.sensor1', 'on') and is_state('binary_sensor.sensor2', 'on') }}
Testing and Verification
- Ensure both input Booleans or binary sensors are correctly configured and functioning.
- Restart Home Assistant after adding the template sensor.
- Check the state of the new sensor (
combined_boolean_sensor or combined_binary_sensor) in the UI or via the Developer Tools > States.
The sensor should be on (or true) only when both input conditions are true. Otherwise, it will be off (or false).