Skip to content

Rule Conditions ​

The when block of a rule evaluates to true or false. When it matches, the rule's apply block fires. When it stops matching, the patch is removed.


All Conditions at a Glance ​

Condition typeKeyWhat it checks
Entity stateentityEntity state against a comparison
Entity attributeentity_attrNamed entity attribute against a comparison
Map rangemap_range_condEntity state linearly mapped to a new scale, then compared
Time rangetime_betweenCurrent time falls within a "HH:MM-HH:MM" window
Day of weekweekday_inCurrent day is in a list
Sun elevationsun_elevationSun is above/below a degree threshold
Random chancerandom_chanceEvaluates true with a given probability (0.0–1.0)
JavaScriptcondition / javascript / jsArbitrary JS expression: returns truthy
Jinja2condition / jinja2Arbitrary HA Jinja2 template: returns truthy
All of (AND)allEvery sub-condition must be true
Any of (OR)anyAt least one sub-condition must be true
Not (NOT)notInverts a single sub-condition

Logical Operators ​

Combine any conditions with all, any, and not. They nest arbitrarily.

all: AND ​

yaml
when:
  all:
    - entity: binary_sensor.motion
      state: "on"
    - entity: input_boolean.night_mode
      state: "on"

Shorthand: a bare list at the top level is treated as all:

yaml
when:
  - entity: binary_sensor.motion
    state: "on"
  - entity: input_boolean.night_mode
    state: "on"

any: OR ​

yaml
when:
  any:
    - entity: binary_sensor.door_front
      state: "on"
    - entity: binary_sensor.door_back
      state: "on"

not: NOT ​

yaml
when:
  not:
    entity: input_boolean.away_mode
    state: "on"

Nested logic ​

yaml
when:
  all:
    - entity: binary_sensor.motion
      state: "on"
    - any:
        - time_between: "08:00-12:00"
        - time_between: "14:00-20:00"
    - not:
        entity: input_boolean.guest_mode
        state: "on"

Entity Conditions ​

State check ​

yaml
when:
  entity: light.living_room
  state: "on"           # alias for 'equals'

Attribute check ​

yaml
when:
  entity_attr: light.living_room
  attribute: brightness
  above: 128

Comparison operators ​

All operators work on both entity state and entity_attr values.

OperatorTypeExampleDescription
statestringstate: "on"Exact match (alias for equals)
equalsstring / numberequals: "on"Exact equality (==)
not_equalsstring / numbernot_equals: "off"Not equal (!=)
abovenumberabove: 25Numeric greater-than (>)
at_leastnumberat_least: 25Numeric greater-than-or-equal (>=)
belownumberbelow: 90Numeric less-than (<)
at_mostnumberat_most: 90Numeric less-than-or-equal (<=)
inlistin: ["on", "idle"]State is in list
not_inlistnot_in: ["off", "unavailable"]State is not in list
regexstringregex: "^heat"State matches regular expression

The 4 numeric operators AND-combine when more than one is present on the same condition, so a range is expressed by combining two of them:

yaml
# Exclusive range: 18 < value < 26
when:
  entity: sensor.temperature
  above: 18
  below: 26

# Inclusive range: 18 <= value <= 26
when:
  entity: sensor.temperature
  at_least: 18
  at_most: 26

The condition-group editor's operator dropdown offers these as ready-made "between" / "between (exclusive)" options; pick one and it fills in both bound fields for you.

Examples ​

yaml
# Exact state match
when:
  entity: input_select.mode
  equals: "night"

# Not off or unavailable
when:
  entity: light.desk
  not_in: ["off", "unavailable"]

# Attribute threshold
when:
  entity_attr: light.living_room
  attribute: brightness
  above: 200

# Regex - match any "heating*" state
when:
  entity: climate.lounge
  regex: "^heat"

Time Conditions ​

Time range ​

yaml
when:
  time_between: "22:00-06:00"   # wraps past midnight automatically

Reactivity

The Rules Engine is reactive: it only re-evaluates when a watched entity changes. Add entity: sensor.time to purely time-based rules so they re-evaluate every minute.

yaml
when:
  all:
    - entity: sensor.time      # triggers re-evaluation every minute
    - time_between: "22:00-06:00"

Day of week ​

yaml
when:
  weekday_in: [mon, tue, wed, thu, fri]

Valid values: mon, tue, wed, thu, fri, sat, sun.

Sun elevation ​

yaml
# Night - sun below horizon
when:
  sun_elevation:
    below: 0

# Golden hour - sun low but above horizon
when:
  sun_elevation:
    above: 0
    below: 8

Map Range Condition ​

Linearly maps an entity's numeric state from one scale to another, then applies a comparison against the mapped value. Useful when the raw entity value is in a different unit than the threshold you want to test.

yaml
when:
  map_range_cond:
    entity: sensor.brightness_raw   # numeric state to read
    input:  [0, 1000]               # raw value range
    output: [0, 100]                # mapped value range
    above: 70                       # compare mapped value - here: > 70%
PropertyRequiredDescription
entity✅Entity whose state is mapped
input✅[min, max]: expected range of the raw entity value
output✅[min, max]: range the value is mapped into
clamp❌Clamp input to range before mapping (default true)
above / below / equals …✅ (one)Comparison applied to the mapped value

Random Chance ​

Evaluates to true with the given probability each time the rule re-evaluates. Value is a float 0.0–1.0.

yaml
when:
  random_chance: 0.25    # true roughly 25% of the time

Useful for stochastic alert animations or randomised accent cycling. Because rules re-evaluate on entity changes, pair with a frequently-changing entity (e.g. sensor.time) to get periodic random evaluations.


Template Conditions ​

For logic that doesn't fit a built-in condition, write JavaScript or Jinja2 directly.

JavaScript ​

Use [[[ ]]] syntax. Return any truthy value to match. The variables entity, hass, and states are available. The rule re-evaluates whenever an entity read as states['domain.object'] changes; entities read any other way (a computed id, for example) don't trigger it.

yaml
when:
  condition: "[[[return states['sensor.temperature'].state > 25]]]"

Multi-line (YAML block scalar):

yaml
when:
  condition: |
    [[[
      const temp = parseFloat(states['sensor.temperature'].state);
      const humid = parseFloat(states['sensor.humidity'].state);
      return temp > 24 && humid > 60;
    ]]]

Explicit form (equivalent):

yaml
when:
  javascript: "return states['light.bedroom'].state === 'on'"

Available variables:

VariableDescription
entityThe card's bound entity object (if any)
hassFull Home Assistant object
stateshass.states: shorthand for all entity states

Jinja2 ​

Use {{ }} syntax. The template is evaluated by Home Assistant's engine. Return any truthy value.

yaml
when:
  condition: "{{ states('sensor.temperature') | float > 25 }}"

Explicit form:

yaml
when:
  jinja2: "{{ states('climate.lounge') == 'heat' }}"

Choosing between JS and Jinja2 ​

JavaScriptJinja2
Evaluated byBrowserHome Assistant server
LatencySynchronous, instantAsync round-trip
Access to HA functions❌ (use states directly)✅ (is_state, state_attr, filters…)
Best forFast logic, math, multi-entityHA-native functions, formatted values

Invalid and Reserved Conditions ​

  • Unknown key (a typo such as entitty:): the condition never matches.
  • Plain-string condition: (no [[[ ]]] or {{ }}, e.g. condition: light.desk is on): a warning is logged in the browser console and the condition never matches. Wrap it as JavaScript or Jinja2 instead.
  • flag and perf_metric are reserved for development tooling. Nothing provides their values today, so they never match (a flag with is: false always matches). Don't use them in dashboards.

Combining Conditions: Full Example ​

yaml
rules:
  - id: comfort-alert
    when:
      all:
        # Temperature out of range
        - entity: sensor.temperature
          above: 26
        # Not already in alert mode
        - not:
            entity: input_boolean.alert_active
            state: "on"
        # Only during occupied hours
        - any:
            - time_between: "07:00-23:00"
            - entity: binary_sensor.presence
              state: "on"
    apply:
      overlays:
        tag:status:
          style:
            color: "{theme:colors.alert.yellow}"

See Also ​

  • Rules Engine: rule structure, apply block, priority, examples
  • Templates: template syntax used outside of rules