From 8dfbec52c867a6387420791b197ed4ba30017674 Mon Sep 17 00:00:00 2001 From: Pavel Slama Date: Mon, 15 Jun 2020 08:58:46 +0200 Subject: [PATCH] value_template --- README.md | 5 +++-- custom_components/attributes/sensor.py | 17 ++++++++++------- info.md | 5 +++-- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 90d304e..7869a88 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/custom_components/attributes/sensor.py b/custom_components/attributes/sensor.py index d3f1edd..1ae7976 100644 --- a/custom_components/attributes/sensor.py +++ b/custom_components/attributes/sensor.py @@ -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 @@ -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 }) @@ -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: @@ -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) diff --git a/info.md b/info.md index 13a3634..e456758 100644 --- a/info.md +++ b/info.md @@ -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.