Security News
Input Validation Vulnerabilities Dominate MITRE's 2024 CWE Top 25 List
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
home-assistant-javascript-templates
Advanced tools
A JavaScript utility to render Home Assistant JavaScript templates
A JavaScript utility to render Home Assistant JavaScript templates.
npm install home-assistant-javascript-templates
yarn add home-assistant-javascript-templates
pnpm add home-assistant-javascript-templates
const HomeAssistantJavaScriptTemplates = require('home-assistant-javascript-templates');
const renderer = new HomeAssistantJavaScriptTemplates(
document.querySelector('home-assistant').hass
);
renderer.renderTemplate('... template string ...');
import HomeAssistantJavaScriptTemplates from 'home-assistant-javascript-templates';
const renderer = new HomeAssistantJavaScriptTemplates(
document.querySelector('home-assistant').hass
);
renderer.renderTemplate('... template string ...');
The package exposes a class that needs to be instantiated and is this isntance the one that you need to use in your code.
Main class of the library, it is the default
export in the package.
new HomeAssistantJavaScriptTemplates(hass, throwErrors = false);
Parameter | Optional | Description |
---|---|---|
hass | no | A valid hass object |
throwErrors | yes | Indicates if the library should throw if the template contains any error. If not it will log the errors as a warning in the console. |
This is the main method to render JavaScript
templates, it needs a string as a parameter. Inside this string you can use several objects and methods.
The same hass
object that was sent to the class
States could be used in two ways, as a function or as an object.
// Using states as a function
states('device_tracker.paulus')
// Using states as an object
states['device_tracker.paulus'].state
Method to check if the state of an entity is equal to a certain value. It returns a boolean
. If the entity id doesn‘t exist it returns false
.
is_state('device_tracker.paulus', 'not_home')
Method to return the value of the state attribute or None
if it doesn’t exist.
state_attr('device_tracker.paulus', 'battery')
Method to test if the given entity attribute is the specified state. It returns a boolean
, if the entity doesn‘t exist it returns false
.
is_state_attr('device_tracker.paulus', 'battery', 40)
Method to test if the given entity is not unknown or unavailable. It returns a boolean
, if the entity doesn‘t exist it returns false
.
has_value('sensor.my_sensor')
Method that returns the value of an attribute for the given device id or None
if it doesn’t exist.
device_attr('706ad0ebe27e105d7cd0b73386deefdd')
Method to test if the value of a device attribute matches a value. It returns a boolean
, if the device id doen‘t exist it returns false
.
is_device_attr('706ad0ebe27e105d7cd0b73386deefdd', 'manufacturer', 'Synology')
Method to return the device id for a given entity id or None
if the entity doesn‘t exist.
device_id('sensor.my_sensor')
Method to return an array with all the areas ids.
areas()
Method to return the area id for a given device id, entity id, or area name. It returns None
if the area doesn‘t exist.
area_id('b8c1c9dd23cb82bbfa09b5657f41d04f')
area_id('sensor.my_sensor')
area_id('Woonkamer')
Method to return the area name for a given device id, entity id, or area id. It returns None
if the area doesn‘t exist.
area_name('b8c1c9dd23cb82bbfa09b5657f41d04f')
area_name('sensor.my_sensor')
area_name('woonkamer')
Method to return an array of entity ids tied to a given area id or area name. If the area doesn‘t exist it returns an empty array.
area_entities('woonkamer')
area_entities('Woonkamer')
Method to return an array of device ids tied to a given area id or area name. If the area doesn‘t exist it returns an empty array.
area_devices('woonkamer')
area_devices('Woonkamer')
Property to return the name of the user logged in in Home Assistant. It returns a string
.
user_name
Property to return if the user logged in in Home Assistant is admin or not. It returns a boolean
.
user_is_admin
Property to return if the user logged in in Home Assistant is the owner. It returns a boolean
.
user_is_owner
import HomeAssistantJavaScriptTemplates from 'home-assistant-javascript-templates';
const renderer = new HomeAssistantJavaScriptTemplates(
document.querySelector('home-assistant').hass
);
/**
* Get the device id of an entity
* With the device id get an attribute of the device
* Return the value of the attribute prefixed with "sn: "
* It will return something like "sn: 123456"
*/
renderer.renderTemplate(`
const deviceId = device_id("binary_sensor.koffiezetapparaat_aan");
const serialNumber = device_attr(deviceId, "serial_number");
return "sn:" + serialNumber;
`);
import HomeAssistantJavaScriptTemplates from 'home-assistant-javascript-templates';
const renderer = new HomeAssistantJavaScriptTemplates(
document.querySelector('home-assistant').hass
);
renderer.renderTemplate(`
const udatesEntities = states['update'];
const updatesEntitiesOn = udatesEntities?.filter((entity) => entity.state === 'on');
return updatesEntitiesOn?.length || 0;
`);
[1.1.0] - 2024-01-28
user
to Hass
interfaceuser_name
, user_is_admin
, user_is_owner
)strict mode
to the compiled codeFAQs
A JavaScript utility to render Home Assistant JavaScript templates
The npm package home-assistant-javascript-templates receives a total of 32 weekly downloads. As such, home-assistant-javascript-templates popularity was classified as not popular.
We found that home-assistant-javascript-templates demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.