Skip to content

Template

The template platform creates entities driven entirely by automations instead of hardware: their actions run in response to a command or press. It is built into UbiHome, so no top-level section is required to enable it.

Turning the switch on or off runs the configured actions.

switch:
- platform: template
name: 'Living Room'
id: living_room
# optimistic (default true): immediately report the new state after a command.
optimistic: true
turn_on_action:
then:
- switch.turn_on: relay
turn_off_action:
then:
- switch.turn_off: relay
PropertyDescriptionExample
optimisticPublish the new state right after a command, without state feedback.true
assumed_stateWhether the state must be assumed. Defaults to optimistic.true
lambdaSource the reported state from a global (see below).see below
turn_on_actionList of actions run when turned on.see above
turn_off_actionList of actions run when turned off.see above

The lambda is written in YAML and currently supports globals.get, which reports the switch state from a bool global. The state tracks the global live — whenever the global changes (e.g. from a turn_on_action that sets it, or from elsewhere) the switch updates. With a lambda the switch is no longer optimistic; its state always reflects the global.

globals:
- id: relay_state
type: bool
initial_value: false
switch:
- platform: template
name: 'Living Room'
id: living_room
lambda:
globals.get: relay_state
turn_on_action:
then:
- globals.set:
id: relay_state
value: true
turn_off_action:
then:
- globals.set:
id: relay_state
value: false

Similar to ESPHome: Template Switch

Pressing the button (from the API, MQTT, or a button.press action) runs on_press.

button:
- platform: template
name: 'Restart Service'
id: restart_service
on_press:
then:
- button.press: real_restart_button
PropertyDescriptionExample
on_pressList of actions run when pressed.see above

Similar to ESPHome: Template Button

Setting the number (from the API, MQTT, or Home Assistant) runs set_action. min_value, max_value, step, unit_of_measurement and device_class are the shared Number attributes.

number:
- platform: template
name: 'Fan Speed'
id: fan_speed
min_value: 0
max_value: 100
step: 1
optimistic: true
set_action:
then:
- button.press: apply_fan_speed

set_action has no access to the commanded value (there is no x variable, unlike ESPHome’s C++ lambdas); use optimistic or a lambda so the entity’s own reported state reflects it instead.

PropertyDescriptionExample
optimisticPublish the commanded value right after a command, without state feedback.true
initial_valueValue to report on startup when not driven by a lambda. Defaults to min_value.0
lambdaSource the reported value from a float global (see below).see below
set_actionList of actions run when a value is set.see above

The same globals.get mechanism as the template switch above, but reading a float global instead of a bool one. The number reports whatever the global currently holds, live, and updates the global automatically whenever it is set to a new value:

globals:
- id: fan_speed_value
type: float
initial_value: 0
number:
- platform: template
name: 'Fan Speed'
id: fan_speed
min_value: 0
max_value: 100
step: 1
lambda:
globals.get: fan_speed_value
# A `lambda`-driven number automatically writes the commanded value back
# to its backing global, so `set_action` is only needed for extra side
# effects (e.g. applying the value elsewhere) and can be omitted.
set_action:
then:
- button.press: apply_fan_speed

Similar to ESPHome: Template Number

Sensors are read-only, so - unlike the switch/number above - there is no command a client can send. The reported value always comes from a lambda reading a float global, and updates live whenever that global changes.

globals:
- id: room_temperature_value
type: float
initial_value: 21.5
sensor:
- platform: template
name: 'Room Temperature'
id: room_temperature
unit_of_measurement: '°C'
lambda:
globals.get: room_temperature_value

unit_of_measurement, device_class, state_class, accuracy_decimals and filters are the shared Sensor attributes.

Similar to ESPHome: Template Sensor