mirror of
https://github.com/home-assistant/home-assistant.io.git
synced 2025-07-09 10:26:52 +00:00
Add Template Sensor device_class support (#5220)
* Add Template Sensor device_class support * Update battery templates * Update track battery level example
This commit is contained in:
parent
77a14ad9d9
commit
dcda30e25b
@ -32,7 +32,7 @@ Open OwnTracks and go to Connection preferences:
|
|||||||
- Mode: Select **Private HTTP**
|
- Mode: Select **Private HTTP**
|
||||||
- Host: [Home Assistant URL]:[port]/api/owntracks/[your name]/[device name]
|
- Host: [Home Assistant URL]:[port]/api/owntracks/[your name]/[device name]
|
||||||
- Identification: Turn **Authentication** on, username `homeassistant` and password is your API password that you use to login to Home Assistant.
|
- Identification: Turn **Authentication** on, username `homeassistant` and password is your API password that you use to login to Home Assistant.
|
||||||
|
|
||||||
Host example: If I host my Home Assistant at `https://example.duckdns.org`, my name is Paulus and my phone is a Pixel I would set the host to be `https://example.duckdns.org/api/owntracks/paulus/pixel`. This will result in an entity with an ID of `device_tracker.paulus_pixel`. You can pick any name for the user and the device.
|
Host example: If I host my Home Assistant at `https://example.duckdns.org`, my name is Paulus and my phone is a Pixel I would set the host to be `https://example.duckdns.org/api/owntracks/paulus/pixel`. This will result in an entity with an ID of `device_tracker.paulus_pixel`. You can pick any name for the user and the device.
|
||||||
|
|
||||||
Since the battery data is available as an attribute of the device tracker entity, it can be tracked with a [`template` sensor](/components/sensor.template/).
|
Since the battery data is available as an attribute of the device tracker entity, it can be tracked with a [`template` sensor](/components/sensor.template/).
|
||||||
@ -47,17 +47,6 @@ sensor:
|
|||||||
friendly_name: Pixel of Paulus
|
friendly_name: Pixel of Paulus
|
||||||
unit_of_measurement: "%"
|
unit_of_measurement: "%"
|
||||||
value_template: '{{ states.device_tracker.paulus_pixel.attributes.battery|int }}'
|
value_template: '{{ states.device_tracker.paulus_pixel.attributes.battery|int }}'
|
||||||
icon_template: >-
|
device_class: battery
|
||||||
{% set battery_level = states.device_tracker.paulus_pixel.attributes.battery|default(0)|int %}
|
|
||||||
{% set battery_round = (battery_level / 10) |int * 10 %}
|
|
||||||
{% if battery_round >= 100 %}
|
|
||||||
mdi:battery
|
|
||||||
{% elif battery_round > 0 %}
|
|
||||||
mdi:battery-{{ battery_round }}
|
|
||||||
{% else %}
|
|
||||||
mdi:battery-alert
|
|
||||||
{% endif %}
|
|
||||||
entity_id:
|
|
||||||
- device_tracker.paulus_pixel
|
|
||||||
```
|
```
|
||||||
{% endraw %}
|
{% endraw %}
|
||||||
|
@ -69,6 +69,11 @@ sensor:
|
|||||||
description: Defines a template for the entity picture of the sensor.
|
description: Defines a template for the entity picture of the sensor.
|
||||||
required: false
|
required: false
|
||||||
type: template
|
type: template
|
||||||
|
device_class:
|
||||||
|
description: The type/class of the sensor to set the icon in the frontend.
|
||||||
|
required: false
|
||||||
|
type: device_class
|
||||||
|
default: None
|
||||||
{% endconfiguration %}
|
{% endconfiguration %}
|
||||||
|
|
||||||
## {% linkable_title Considerations %}
|
## {% linkable_title Considerations %}
|
||||||
|
@ -12,7 +12,7 @@ ha_category: Automation Examples
|
|||||||
|
|
||||||
### {% linkable_title iOS Devices %}
|
### {% linkable_title iOS Devices %}
|
||||||
|
|
||||||
If you have a device running iOS (iPhone, iPad, etc), The [iCloud](/components/device_tracker.icloud/) is gathering various details about your device including the battery level. To display it in the Frontend use a [template sensor](/components/sensor.template/). You can also use the icon template option to create a dynamic icon that changes with the battery level.
|
If you have a device running iOS (iPhone, iPad, etc), The [iCloud](/components/device_tracker.icloud/) is gathering various details about your device including the battery level. To display it in the Frontend use a [template sensor](/components/sensor.template/). You can also the `battery` [sensor device class](/components/sensor/#device-class) to dynamically change the icon with the battery level.
|
||||||
|
|
||||||
{% raw %}
|
{% raw %}
|
||||||
```yaml
|
```yaml
|
||||||
@ -21,8 +21,6 @@ sensor:
|
|||||||
sensors:
|
sensors:
|
||||||
battery_iphone:
|
battery_iphone:
|
||||||
friendly_name: iPhone Battery
|
friendly_name: iPhone Battery
|
||||||
# "entity_id:" ensures that this sensor will only update when your device tracker does.
|
|
||||||
entity_id: device_tracker.iphone
|
|
||||||
unit_of_measurement: '%'
|
unit_of_measurement: '%'
|
||||||
value_template: >-
|
value_template: >-
|
||||||
{%- if states.device_tracker.iphone.attributes.battery %}
|
{%- if states.device_tracker.iphone.attributes.battery %}
|
||||||
@ -30,21 +28,10 @@ sensor:
|
|||||||
{% else %}
|
{% else %}
|
||||||
{{ states.sensor.battery_iphone.state }}
|
{{ states.sensor.battery_iphone.state }}
|
||||||
{%- endif %}
|
{%- endif %}
|
||||||
icon_template: >
|
device_class: battery
|
||||||
{% set battery_level = states.sensor.battery_iphone.state|default(0)|int %}
|
|
||||||
{% set battery_round = (battery_level / 10) |int * 10 %}
|
|
||||||
{% if battery_round >= 100 %}
|
|
||||||
mdi:battery
|
|
||||||
{% elif battery_round > 0 %}
|
|
||||||
mdi:battery-{{ battery_round }}
|
|
||||||
{% else %}
|
|
||||||
mdi:battery-alert
|
|
||||||
{% endif %}
|
|
||||||
```
|
```
|
||||||
{% endraw %}
|
{% endraw %}
|
||||||
|
|
||||||
The `else` part is used to have the sensor keep its last state if the newest [iCloud](/components/device_tracker.icloud/) update doesn't have any battery state in it (which happens sometimes). Otherwise the sensor will be blank.
|
|
||||||
|
|
||||||
### {% linkable_title Android and iOS Devices %}
|
### {% linkable_title Android and iOS Devices %}
|
||||||
|
|
||||||
While running the [Owntracks](/components/device_tracker.owntracks/) device tracker you can retrieve the battery level with a MQTT sensor. Replace username with your MQTT username (for the embedded MQTT it's simply homeassistant), and deviceid with the set Device ID in Owntracks.
|
While running the [Owntracks](/components/device_tracker.owntracks/) device tracker you can retrieve the battery level with a MQTT sensor. Replace username with your MQTT username (for the embedded MQTT it's simply homeassistant), and deviceid with the set Device ID in Owntracks.
|
||||||
@ -57,5 +44,6 @@ sensor:
|
|||||||
name: "Battery Tablet"
|
name: "Battery Tablet"
|
||||||
unit_of_measurement: "%"
|
unit_of_measurement: "%"
|
||||||
value_template: '{{ value_json.batt }}'
|
value_template: '{{ value_json.batt }}'
|
||||||
|
device_class: battery
|
||||||
```
|
```
|
||||||
{% endraw %}
|
{% endraw %}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user