Virtual Switch is a custom Home Assistant helper that creates virtual switch entities backed by existing Home Assistant entities.
It is useful when you want to combine the state, control, and attributes of existing Home Assistant entities into a single logical switch.
For example, a virtual switch can use one entity for its on/off state, another for commands, and expose power, voltage, battery level, runtime, or other entity data as attributes.
- Creates standard Home Assistant switch entities
- Uses another entity as the switch state
- Sends on/off commands to a configurable entity
- Supports separate state and command entities
- Supports different on/off icons
- Exposes states from other entities as attributes
- Copies specific attributes from other entities
- Supports Home Assistant templates for transforming attribute values
- Supports explicit units of measurement
- Can automatically copy units of measurement from source entities
- Updates when source entities or template dependencies change
- Does not poll
- Links the virtual switch to the source entity's existing Home Assistant device
- Supports multiple virtual switches
-
Open HACS.
-
Go to Integrations.
-
Add a custom repository.
-
Use this repository:
https://github.com/bloodtick/home-assistant-virtual-switch -
Select Integration as the repository type.
-
Install Virtual Switch.
-
Restart Home Assistant.
-
Go to Settings > Devices & services.
-
Add Virtual Switch.
Virtual switch definitions are configured in YAML.
virtual_switch:
switches:
master_bedroom_switch_fan:
name: "Master Bedroom Switch Fan"
unique_id: master_bedroom_switch_fan
state_entity: switch.master_bedroom_fan
command_entity: switch.master_bedroom_fan
on_icon: mdi:fan
off_icon: mdi:fan-off
attributes:
power:
entity: sensor.master_bedroom_fan_power
copy_unit: true
voltage:
entity: sensor.master_bedroom_fan_voltage
copy_unit: trueThis creates a virtual switch whose state follows:
switch.master_bedroom_fan
Turning the virtual switch on or off sends the command to:
switch.master_bedroom_fan
The resulting switch can expose attributes such as:
power: 13
power_unit: W
voltage: 121
voltage_unit: V
Friendly name for the virtual switch.
name: "Master Bedroom Switch Fan"Unique Home Assistant identifier for the virtual switch.
unique_id: master_bedroom_switch_fanEntity used to determine whether the virtual switch is on or off.
state_entity: switch.master_bedroom_fanThe virtual switch is also associated with the Home Assistant device belonging to this entity when a device is available.
Entity that receives the on and off commands.
command_entity: switch.master_bedroom_fanIf omitted, state_entity is used.
Different icons can be displayed for the on and off states.
on_icon: mdi:fan
off_icon: mdi:fan-offVirtual Switch can expose data from other Home Assistant entities as attributes of the switch.
attributes:
power:
entity: sensor.master_bedroom_fan_powerThis creates a power attribute using the state of the source entity.
attributes:
power:
entity: sensor.master_bedroom_fan_power
copy_unit: trueIf the source entity reports watts, the resulting attributes could be:
power: 13
power_unit: W
Instead of using the source entity's state, you can copy one of its attributes.
attributes:
current_mode:
entity: climate.living_room
attribute: hvac_actionAttribute values can be transformed using Home Assistant templates.
The source value is available inside the template as:
value
The source entity ID is available as:
entity
The complete Home Assistant State object is available as:
state
attributes:
battery_runtime_min:
entity: sensor.ups_battery_runtime
value_template: >
{{ (value | float(0) / 60) | round(1) }}
unit: "min"If the source sensor reports:
3600
the virtual switch will expose:
battery_runtime_min: 60.0
battery_runtime_min_unit: min
attributes:
power_kw:
entity: sensor.house_power
value_template: >
{{ (value | float(0) / 1000) | round(2) }}
unit: "kW"attributes:
temperature_f:
entity: sensor.room_temperature
value_template: >
{{ ((value | float(0)) * 9 / 5 + 32) | round(1) }}
unit: "°F"attributes:
load_status:
entity: sensor.device_power
value_template: >
{% if value | float(0) > 100 %}
High
{% elif value | float(0) > 10 %}
Normal
{% else %}
Idle
{% endif %}Templates can also reference other Home Assistant entities using normal Home Assistant template functions.
attributes:
system_status:
entity: sensor.device_power
value_template: >
{% if is_state('binary_sensor.system_online', 'on') %}
Online - {{ value }} W
{% else %}
Offline
{% endif %}Virtual Switch tracks entities referenced by templates so the virtual switch updates when those dependencies change.
There are two ways to add units to an attribute.
copy_unit: trueunit: "min"If both are specified, the explicit unit value takes precedence.
For example:
attributes:
battery_runtime_min:
entity: sensor.ups_battery_runtime
copy_unit: true
value_template: >
{{ (value | float(0) / 60) | round(1) }}
unit: "min"The resulting unit will be min, regardless of the source sensor's unit.
A virtual UPS power switch can combine switch control with power and battery telemetry:
virtual_switch:
switches:
master_bedroom_ups_power_switch:
name: "UPS Power"
unique_id: master_bedroom_ups_power_switch
state_entity: switch.hubitat_ups_mb_switch
command_entity: switch.hubitat_ups_mb_switch
on_icon: mdi:power
off_icon: mdi:power
attributes:
power:
entity: sensor.hubitat_ups_mb_switch_power
copy_unit: true
voltage:
entity: sensor.hubitat_ups_mb_switch_voltage
copy_unit: true
battery:
entity: sensor.ups_battery_charge
copy_unit: true
battery_runtime:
entity: sensor.ups_battery_runtime
copy_unit: true
battery_runtime_min:
entity: sensor.ups_battery_runtime
value_template: >
{{ (value | float(0) / 60) | round(1) }}
unit: "min"
status:
entity: sensor.ups_status
status_data:
entity: sensor.ups_status_dataThis allows one logical switch entity to provide:
State
Power
Voltage
Battery charge
Battery runtime
Battery runtime in minutes
UPS status
UPS status data
while still controlling the original UPS switch.
Multiple switches can be defined under the same integration.
virtual_switch:
switches:
bedroom_fan:
name: "Bedroom Fan"
unique_id: bedroom_fan
state_entity: switch.bedroom_fan
office_fan:
name: "Office Fan"
unique_id: office_fan
state_entity: switch.office_fan| Option | Description |
|---|---|
entity |
Source Home Assistant entity |
attribute |
Optional attribute to read instead of the entity state |
copy_unit |
Copy the source entity's unit of measurement |
unit |
Explicit unit to expose for the virtual attribute |
value_template |
Home Assistant template used to transform the source value |
unit_attribute |
Optional source attribute containing the unit |
When updating Virtual Switch through HACS:
- Install or redownload the latest release.
- Restart Home Assistant.
Python changes to custom integrations require a Home Assistant restart before they take effect.
MIT