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.
Switch
Section titled “Switch”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: relayAttributes
Section titled “Attributes”| Property | Description | Example |
|---|---|---|
optimistic | Publish the new state right after a command, without state feedback. | true |
assumed_state | Whether the state must be assumed. Defaults to optimistic. | true |
lambda | Source the reported state from a global (see below). | see below |
turn_on_action | List of actions run when turned on. | see above |
turn_off_action | List of actions run when turned off. | see above |
State from a global (lambda)
Section titled “State from a global (lambda)”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: falseSimilar to ESPHome: Template Switch
Button
Section titled “Button”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_buttonAttributes
Section titled “Attributes”| Property | Description | Example |
|---|---|---|
on_press | List of actions run when pressed. | see above |
Similar to ESPHome: Template Button
Number
Section titled “Number”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_speedset_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.
Attributes
Section titled “Attributes”| Property | Description | Example |
|---|---|---|
optimistic | Publish the commanded value right after a command, without state feedback. | true |
initial_value | Value to report on startup when not driven by a lambda. Defaults to min_value. | 0 |
lambda | Source the reported value from a float global (see below). | see below |
set_action | List of actions run when a value is set. | see above |
State from a global (lambda)
Section titled “State from a global (lambda)”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_speedSimilar to ESPHome: Template Number
Sensor
Section titled “Sensor”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_valueunit_of_measurement, device_class, state_class, accuracy_decimals and
filters are the shared Sensor attributes.
Similar to ESPHome: Template Sensor