Mastering Virtual Devices in Home Assistant: Advanced Logic with Input Booleans and Template Entities

NGC 224
DIY Smart Home Creator
In the vast landscape of Home Assistant, we often focus on integrating physical devices – lights, sensors, thermostats. However, the true power of a smart home lies not just in controlling hardware, but in orchestrating complex logic, abstracting states, and creating intuitive user interfaces that transcend the physical realm. This is where virtual devices come into play. By mastering virtual entities like input_boolean
, input_number
, input_text
, and advanced template
sensors and switches, you can elevate your Home Assistant automations from simple reactions to intelligent, context-aware systems.
What Are Virtual Devices?
Virtual devices in Home Assistant are entities that don't correspond to a physical piece of hardware. Instead, they represent a logical state, a numerical value, a text string, or a calculated condition based on other entities. They act as powerful intermediaries for your automations, enabling you to:
- Simplify complex logic: Aggregate states from multiple devices into a single, easy-to-manage virtual entity.
- Create "modes" or "scenes": Design custom operating modes for your home (e.g., "Guest Mode," "Movie Night") with a single toggle.
- Improve UI/UX: Provide intuitive controls and feedback in Lovelace without exposing raw, granular device states.
- Abstract functionality: Decouple automations from specific hardware, making your configuration more robust and adaptable.
Key Virtual Entity Types:
input_boolean
: A simple on/off toggle. Perfect for binary states like "Away Mode" or "Alarm Armed."input_number
: A slider or text input for numerical values. Useful for setting thermostat setpoints, dimming levels, or delays.input_text
: A text field for custom strings. Great for temporary notes, dynamic announcements, or custom commands.template
entities (Binary Sensor, Sensor, Switch): These derive their state or behavior from other entities using Jinja2 templates, offering immense flexibility for complex, conditional logic.
Step-by-Step Setup: Input Helpers (Basic Virtual Devices)
Input helpers are the simplest form of virtual devices, easily configured via the Home Assistant UI or configuration.yaml
. We'll focus on input_boolean
as a prime example.
1. Creating an Input Boolean for "Guest Mode"
Via the UI:
- Navigate to Settings > Devices & Services > Helpers.
- Click + CREATE HELPER.
- Select Toggle (this is an
input_boolean
). - Give it a descriptive name, e.g., "Guest Mode Toggle", and an optional icon.
- Click CREATE.
You now have an entity named input_boolean.guest_mode_toggle
(or similar) that you can use in automations and display in Lovelace.
Via configuration.yaml
:
For those who prefer YAML or need more structured configuration, add the following to your configuration.yaml
:
# configuration.yaml
input_boolean:
guest_mode_toggle:
name: Guest Mode
initial: off
icon: mdi:account-group
away_mode:
name: Away Mode
initial: off
icon: mdi:bus
Reload your Home Assistant YAML configuration (Developer Tools > YAML > RELOAD ALL YAML CONFIGURATIONS or restart HA). These new entities will appear.
2. Integrating into Automations:
Now, let's make "Guest Mode" actually *do* something. When input_boolean.guest_mode_toggle
is on
, we might unlock a guest room door, turn on specific lights, or disable certain security automations.
# automations.yaml
- alias: 'Guest Mode Activated'
trigger:
- platform: state
entity_id: input_boolean.guest_mode_toggle
to: 'on'
action:
- service: lock.unlock
target:
entity_id: lock.guest_room_door
- service: light.turn_on
target:
entity_id: light.guest_room_main
data:
brightness_pct: 70
- service: script.disable_security_alerts_guest_mode # Example script
mode: single
- alias: 'Guest Mode Deactivated'
trigger:
- platform: state
entity_id: input_boolean.guest_mode_toggle
to: 'off'
action:
- service: lock.lock
target:
entity_id: lock.guest_room_door
- service: light.turn_off
target:
entity_id: light.guest_room_main
- service: script.enable_security_alerts_guest_mode # Example script
mode: single
Advanced Virtual Devices: Template Entities
Template entities allow you to define a virtual sensor or switch whose state is dynamically calculated using the powerful Jinja2 templating engine. This unlocks highly sophisticated logic.
1. Creating a Template Binary Sensor: "House Occupied"
Imagine you have multiple presence detectors (e.g., mobile apps, router presence, motion sensors). Instead of checking each one in every automation, create a single "House Occupied" sensor.
Add this to your configuration.yaml
or a dedicated templates.yaml
file (if you use !include templates.yaml
):
# configuration.yaml or templates.yaml
template:
- binary_sensor:
- name: "House Occupied"
unique_id: house_occupied_template_sensor
device_class: occupancy
state: >
{{ is_state('person.john_doe', 'home') or
is_state('person.jane_doe', 'home') or
is_state('binary_sensor.front_door_motion', 'on') or
is_state('binary_sensor.living_room_motion', 'on') }}
This sensor will be on
if John, Jane, or either motion sensor detects presence. Restart Home Assistant to load new template entities.
2. Creating a Template Switch: "Night Mode" with Conditions
A template switch can not only report a state but also execute actions when turned on or off. Let's create a "Night Mode" switch that only activates between certain hours and turns off specific devices.
# configuration.yaml or templates.yaml
template:
- switch:
- name: "House Night Mode"
unique_id: house_night_mode_template_switch
value_template: >
{{ is_state('input_boolean.night_mode_active', 'on') and
now().hour >= 22 or now().hour < 6 }}
turn_on_action:
- service: input_boolean.turn_on
target:
entity_id: input_boolean.night_mode_active
- service: light.turn_off
target:
entity_id:
- light.living_room_main
- light.kitchen_counter
turn_off_action:
- service: input_boolean.turn_off
target:
entity_id: input_boolean.night_mode_active
- service: light.turn_on # Example: turn on a hallway light slightly
target:
entity_id: light.hallway_light
data:
brightness_pct: 20
icon_template: >
{% if is_state('input_boolean.night_mode_active', 'on') %}
mdi:moon-waning-gibbous
{% else %}
mdi:white-balance-sunny
{% endif %}
Note: For template switches, it's often best practice to link its value_template
to an underlying input_boolean
(like input_boolean.night_mode_active
here) which actually holds the state. The template switch then acts as a sophisticated wrapper around this boolean, adding conditional logic to its state and actions.
Real-World Use Cases and Best Practices
Use Cases:
Complex Scene/Mode Management:
- Movie Night Mode: An
input_boolean
that, when toggled, dims lights, closes blinds, turns on the projector, and sets amplifier input. - Vacuum Charging State: A
template_binary_sensor
that combines a robot vacuum's charging status with its dock presence for a more reliable "ready to clean" status.
- Movie Night Mode: An
Cross-Protocol Aggregation:
- "All Windows Closed" Sensor: A
template_binary_sensor
that'son
only if all your Zigbee, Z-Wave, and Wi-Fi window sensors reportclosed
. Ideal for HVAC automations.
- "All Windows Closed" Sensor: A
UI Abstraction and Simplification:
- Present a single "HVAC Override"
input_boolean
in Lovelace that triggers a script adjusting multiple climate entities, rather than showing each individual thermostat.
- Present a single "HVAC Override"
Automation Guard Rails:
- A "Water Leak Detected - Alarm Active"
input_boolean
that, whenon
, prevents irrigation systems from running and sends critical notifications. - An
input_number
to dynamically set a delay for "lights off" automations after motion ceases.
- A "Water Leak Detected - Alarm Active"
Best Practices:
- Meaningful Naming: Use clear, descriptive names and
unique_id
for easy identification. - Modular Configuration: For larger setups, organize your YAML by type (e.g.,
input_booleans.yaml
,template_sensors.yaml
) and use!include
directives inconfiguration.yaml
. - Iconography: Assign relevant icons (
mdi:
) to make them visually distinct in the UI. - Documentation: Add comments to your YAML to explain complex templates or the purpose of specific virtual devices.
- Avoid Circular Dependencies: Ensure template entities don't inadvertently depend on each other in a loop, which can lead to unpredictable behavior or errors.
- Test Templates: Use the "Template" tab in Developer Tools to test your Jinja2 expressions before applying them to avoid configuration errors.
Troubleshooting Virtual Devices
- Configuration Errors: Always check the Home Assistant logs (Settings > System > Logs) after reloading YAML or restarting. Syntax errors in templates are common.
- State Not Updating: For template entities, ensure all underlying entities in your
value_template
are correctly referenced and their states are actually changing. Use the "States" tab in Developer Tools to monitor entity states. - Actions Not Firing: For template switches, verify the service calls in
turn_on_action
andturn_off_action
are correct and the target entities exist. - Slow Performance: While virtual devices are generally lightweight, overly complex or frequently updating templates can consume resources. Optimize your templates by avoiding unnecessary computations.
Scalability, Reliability, and Security
Scalability:
Virtual devices themselves are very lightweight. The main scaling consideration is keeping your configuration organized. Using !include
for different types of virtual entities will make your configuration.yaml
manageable as your smart home grows.
Reliability:
By abstracting logic into virtual devices, you can improve reliability. For instance, if one physical motion sensor fails, your "House Occupied" template sensor can still function using other inputs, making your automations more resilient.
Security:
Virtual devices typically do not introduce new security vulnerabilities directly. However, if they control critical systems (e.g., door locks, alarms) and are exposed externally via Home Assistant Cloud or Nabu Casa, ensure your Home Assistant instance itself is secured with strong authentication, HTTPS, and up-to-date software.
Conclusion
Virtual devices are an indispensable tool in the Home Assistant ecosystem, offering a powerful way to move beyond simple device control to creating truly intelligent, responsive, and user-friendly smart home automations. By mastering input_boolean
, input_number
, input_text
, and the advanced capabilities of template
sensors and switches, you can build a more robust, scalable, and intuitive smart home experience. Start experimenting today and unlock a new level of control and customization!

NGC 224
Author bio: DIY Smart Home Creator