From 88c8cc2ff102f0ddc7823bde8a34016fd633786d Mon Sep 17 00:00:00 2001 From: Mikael Svensson Date: Wed, 28 Mar 2018 09:04:48 +0200 Subject: [PATCH] Template function state_attr to get attribute from a state (#4981) * Adds docs for https://github.com/home-assistant/home-assistant/pull/13378 * :ambulance: Fixes build error --- source/_docs/configuration/templating.markdown | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/source/_docs/configuration/templating.markdown b/source/_docs/configuration/templating.markdown index d00a879b7bd..5bd5d45789c 100644 --- a/source/_docs/configuration/templating.markdown +++ b/source/_docs/configuration/templating.markdown @@ -68,6 +68,7 @@ Home Assistant adds extensions to allow templates to access all of the current s - `states.sensor.temperature` returns the state object for `sensor.temperature`. - `states('device_tracker.paulus')` will return the state string (not the object) of the given entity or `unknown` if it doesn't exist. - `is_state('device_tracker.paulus', 'home')` will test if the given entity is specified state. +- `state_attr('device_tracker.paulus', 'battery')` will return the value of the attribute or None if it doesn't exist. - `is_state_attr('device_tracker.paulus', 'battery', 40)` will test if the given entity is specified state. - `now()` will be rendered as current time in your time zone. - For specific values: `now().second`, `now().minute`, `now().hour`, `now().day`, `now().month`, `now().year`, `now().weekday()` and `now().isoweekday()` @@ -114,7 +115,7 @@ The next two statements result in same value if state exists. The second one wil ### {% linkable_title Attributes %} -Print an attribute if state is defined +Print an attribute if state is defined. Both will return the same thing but the last one you can specify entity_id from a variable. ```text {% raw %}{% if states.device_tracker.paulus %} @@ -124,6 +125,18 @@ Print an attribute if state is defined {% endif %}{% endraw %} ``` +With strings + +```text +{% raw %}{% set tracker_name = "paulus"%} + +{% if states("device_tracker." + tracker_name) != "unknown" %} + {{ state_attr("device_tracker." + tracker_name, "battery")}} +{% else %} + ?? +{% endif %}{% endraw %} +``` + ### {% linkable_title Sensor states %} Print out a list of all the sensor states.