Skip to content

Rules Engine

window.lcards.core.rulesManager — Evaluates conditions and hot-patches card styles at runtime.


Overview

RulesEngine extends BaseService. It maintains a compiled index of rules, tracks which entities each rule depends on, and re-evaluates dirty rules on every HASS state push. Matched rules produce patches — plain style objects that are merged onto target overlays without those overlays needing to know the rule exists.


Key Classes

ClassFileRole
RulesEnginecore/rules/RulesEngine.jsEvaluation loop, dependency index, patch distribution
compileConditionscore/rules/compileConditions.jsCompiles rule when DSL to optimised predicate functions
RuleTraceBuffercore/rules/RuleTraceBuffer.jsRing buffer of evaluation events for the Debug Panel

Evaluation Flow

HASS state push
    → mark affected rules dirty  (dependency index O(1) lookup)
    → evaluate dirty rules only  (compiled predicates)
    → for each matched rule: compute patches
    → distribute patches to registered overlay listeners
    → overlays call requestUpdate()

Rule Schema

yaml
rules:
  - id: high_temp
    priority: 10
    when:
      entity: sensor.cpu_temp
      above: 75
    apply:
      style:
        color: var(--lcards-red)
      animations:
        - tag: temp_widget
          preset: alert_pulse
          loop: true

  - id: motion_active
    when:
      entity: binary_sensor.front_door
      state: "on"
    apply:
      style:
        background: var(--lcards-orange)

Condition Operators

OperatorValue typeMeaning
statestringExact state match
abovenumbernumeric_state > value
belownumbernumeric_state < value
attribute{ key, value }Attribute equals value
templateJS string[[[code]]] — truthy return
allarray of conditionsAND
anyarray of conditionsOR

Targeting

Rules target overlays via:

FieldMatches
id:Exact overlay ID
tag:Any overlay with that tag
type:All overlays of that card type
*All registered overlays

Card Integration

javascript
// Register overlay (in _handleFirstUpdate)
this._registerOverlayForRules({ id: `btn-${this._cardGuid}`, type: 'button' });

// Receive patches
_onRulePatchesChanged(patches) {
  this._resolveStyle();   // merge config.style + patches, then requestUpdate()
}

Priority & Stop Processing

Higher priority (lower number) wins when multiple rules target the same overlay property. A rule can include stop_processing: true to prevent lower-priority rules from being evaluated for that overlay.


See Also