1.2.0 • Published 5 months ago

ha-nunjucks v1.2.0

Weekly downloads
-
License
ISC
Repository
github
Last release
5 months ago

ha-nunjucks

License Project Maintenance GitHub Activity Community Forum

Github

A simple wrapper for nunjucks for use with Home Assistant frontend custom components to render templates. This repository offers an easy way for developers to add templating support to Home Assistant custom cards.

What is nunjucks?

Nunjucks is a templating engine for JavaScript that is heavily inspired by jinja2. Home Assistant uses jinja2 to process templates in card configurations on the backend, so the syntax of jinja2 and Nunjucks is virtually the same. This makes it an excellent alternative for Home Assistant templating for custom cards.

While some Home Assistant native cards support templating for certain fields, implementing proper Home Assistant jinja2 template support in custom cards is difficult. The only custom module that manages to do so (AFAIK) is card-mod, and it's implementation is hard to port to other projects. This project offers a much easier plug and play solution to adding template support to custom cards.

Usage

Install using npm:

npm install ha-nunjucks

Then import renderTemplate from ha-nunjucks and provide it with the hass object and a template string you want to process.

import { renderTemplate } from 'ha-nunjucks';

const renderedString = renderTemplate(this.hass, templateString);

Rather than rendering templates on the backend, nunjucks renders templates on the frontend. This repository uses the Home Assistant object present in all custom cards to read entity state data.

You can also provide additional context to the renderTemplate function to pass to nunjucks if you want to make additional variables or project specific functions available to your users for use in templates.

import { renderTemplate } from 'ha-nunjucks';

const context = {
  foo: 'bar',
  doThing(thing: string) {
    return `doing ${thing}!`;
  },
};

const renderedString = renderTemplate(this.hass, templateString, context);

Available Extensions

The catch to this approach of rendering jinja2/nunjucks templates is that we have to reimplement all of the Home Assistant template extension functions and filters. If there are functions or filters that you use that are not currently supported, please make a feature request or try adding it to the project yourself and create a pull request.

So far a subset of the Home Assistant template extension functions have been implemented as documented below.

Variables

These variables just remap Python built-in constants to JavaScript ones.

PythonJavaScript
Truetrue
Falsefalse
Nonenull

Frontend Data Hass Object

The frontend data hass object has been exposed to users to call upon.

Because entity IDs contain periods in them, it's better to access it using bracket notation like so:

{{ hass["states"]["light.sunroom_ceiling"]["state"] }}

You can also use dot notation for everything but the entity ID.

{{ hass.states["light.sunroom_ceiling"].state }}

States

Functions used to determine an entity's state or an attribute.

NameArgumentsDescription
statesentity_idReturns the state string of the given entity.
is_stateentity_id, valueCompares an entity's state with a specified state or list of states and returns true or false.
state_attrentity_id, attributeReturns the value of the attribute or undefined if it doesn't exist.
is_state_attrentity_id, attribute, valueTests if the given entity attribute is the specified value.
has_valueentity_idTests if the given entity is not unknown or unavailable.

Immediate If

A shorthand for an if else statement.

NameArgumentsDescription
iifcondition, if_true, if_false, if_noneImmediate if. Returns the value of if_true if the condition is true, the value of if_false if it's false, and the value of if_none if it's undefined, null, or an empty string. All arguments except condition are optional. Cannot be used as a filter.

Other

Functions that are not from the Home Assistant templating documentation

NameArgumentsDescription
match_mediamediaqueryReturns the boolean result of the provided CSS media query.
1.2.0

5 months ago

1.2.0-alpha.1

5 months ago

1.1.0

5 months ago

1.1.0-alpha.1

5 months ago

1.1.0-dev.2

5 months ago

1.1.0-alpha.3

5 months ago

1.0.1

5 months ago

0.0.13

5 months ago

0.0.12

5 months ago

0.0.10

5 months ago

0.0.9

5 months ago

0.0.8

5 months ago

0.0.7

5 months ago

0.0.6

5 months ago

0.0.4

5 months ago

0.0.3

5 months ago

0.0.2

5 months ago

0.0.1

5 months ago

1.0.0

5 months ago