Skip to content

Commit

Permalink
value_template
Browse files Browse the repository at this point in the history
  • Loading branch information
pilotak committed Jun 15, 2020
1 parent 45aee7d commit 8dfbec5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ Configuration variables:

- **entities** (*Required*): A list of entity IDs that you want to read attributes from.
- **attribute** (*Required*): Which attribute to extract from defined entity IDs.
- **friendly_name** (*Optional*): Name to use in the Frontend *(will be the same for all entities specified at the moment)*.
- **friendly_name** (*Optional*): Name to use in the Frontend *(will be the same for all entities specified)*.
- **icon** (*Optional*): Icon to use in the Frontend.
- **unit_of_measurement** (*Optional*): Defines the units of measurement of the sensor, if any.
- **round_to** (*Optional*): round numbers to 'x' decimals, if zero it will become whole number. Skip this field if you extracting a string or you want to leave the value as it is.
- **round_to** (*Optional*): Round numbers to 'x' decimals, if zero it will become whole number. Skip this field if you extracting a string or you want to leave the value as it is.
- **value_template** (*Optional*): In case you need to do a math with the value ie. offset, bit gain, etc. *(will be the same for all entities specified)*.
- **time_format** (*Optional*): **`strftime`** type string to beautify time attribute output. Applicable only when attribute `last_changed` or `last_triggered` is selected. Cheatsheet for strftime formatting [here](http://strftime.ninja/).

## Install via [HACS](https://github.com/custom-components/hacs)
Expand Down
17 changes: 10 additions & 7 deletions custom_components/attributes/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
from homeassistant.core import callback
from homeassistant.components.sensor import ENTITY_ID_FORMAT, PLATFORM_SCHEMA
from homeassistant.const import (
ATTR_FRIENDLY_NAME, ATTR_UNIT_OF_MEASUREMENT,
ATTR_ICON, CONF_ENTITIES, EVENT_HOMEASSISTANT_START, STATE_UNKNOWN)
ATTR_FRIENDLY_NAME, ATTR_UNIT_OF_MEASUREMENT, ATTR_ICON, CONF_ENTITIES,
EVENT_HOMEASSISTANT_START, STATE_UNKNOWN, CONF_VALUE_TEMPLATE)
from homeassistant.exceptions import TemplateError
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import Entity, async_generate_entity_id
Expand All @@ -32,6 +32,7 @@
vol.Optional(ATTR_UNIT_OF_MEASUREMENT): cv.string,
vol.Optional(CONF_TIME_FORMAT): cv.string,
vol.Optional(CONF_ROUND_TO): cv.positive_int,
vol.Optional(CONF_VALUE_TEMPLATE): cv.string,
vol.Required(CONF_ATTRIBUTE): cv.string,
vol.Required(CONF_ENTITIES): cv.entity_ids
})
Expand All @@ -46,7 +47,6 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
for device in config[CONF_ENTITIES]:
attr = config.get(CONF_ATTRIBUTE)
time_format = str(config.get(CONF_TIME_FORMAT))
round_to = config.get(CONF_ROUND_TO, None)

if (attr == "last_triggered" or
attr == "last_changed") and time_format:
Expand All @@ -58,18 +58,21 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
{{% else %}} {3} {{% endif %}}").format(
device, attr, time_format, STATE_UNKNOWN)
else:
round_to = config.get(CONF_ROUND_TO, None)
additional_template = config.get(CONF_VALUE_TEMPLATE, "")

state_template = "{{% if states('{0}') != '{2}' %}}"

if round_to is None:
state_template += "{{{{ states.{0}.attributes['{1}'] }}}}"
state_template += "{{{{ states.{0}.attributes['{1}'] {4} }}}}"
elif round_to > 0:
state_template += "{{{{ (states.{0}.attributes['{1}'] | float) | round({3}) }}}}"
state_template += "{{{{ (states.{0}.attributes['{1}'] | float) | round({3}) {4} }}}}"
else:
state_template += "{{{{ states.{0}.attributes['{1}'] | int }}}}"
state_template += "{{{{ states.{0}.attributes['{1}'] | int {4} }}}}"

state_template += "{{% else %}} {2} {{% endif %}}"
state_template = state_template.format(
device, attr, STATE_UNKNOWN, round_to)
device, attr, STATE_UNKNOWN, round_to, additional_template)

_LOGGER.info("Adding attribute: %s of entity: %s", attr, device)
_LOGGER.debug("Applying template: %s", state_template)
Expand Down
5 changes: 3 additions & 2 deletions info.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ Configuration variables:

- **entities** (*Required*): A list of entity IDs that you want to read attributes from.
- **attribute** (*Required*): Which attribute to extract from defined entity IDs.
- **friendly_name** (*Optional*): Name to use in the Frontend *(will be the same for all entities specified at the moment)*.
- **friendly_name** (*Optional*): Name to use in the Frontend *(will be the same for all entities specified)*.
- **icon** (*Optional*): Icon to use in the Frontend.
- **unit_of_measurement** (*Optional*): Defines the units of measurement of the sensor, if any.
- **round_to** (*Optional*): round numbers to 'x' decimals, if zero it will become whole number. Skip this field if you extracting a string or you want to leave the value as it is.
- **round_to** (*Optional*): Round numbers to 'x' decimals, if zero it will become whole number. Skip this field if you extracting a string or you want to leave the value as it is.
- **value_template** (*Optional*): In case you need to do a math with the value ie. offset, bit gain, etc. *(will be the same for all entities specified)*.
- **time_format** (*Optional*): **`strftime`** type string to beautify time attribute output. Applicable only when attribute `last_changed` or `last_triggered` is selected. Cheatsheet for strftime formatting [here](http://strftime.ninja/).

This example shows how to extract the `battery_level` attribute.
Expand Down

0 comments on commit 8dfbec5

Please sign in to comment.