How to display template sensor attributes in HA home page?

Hi all,

I have one sensor called binary_sensor.openclose_13, which to track door open close state.

In this sensor, it also have other attributes such as battery level, as per screenshot below:

So, I would like to get this battery level attribute information by creating template sensor as mentioned in this link (https://www.home-assistant.io/components/sensor.template/), then displays this battery level under HA using sensor card configuration.

Then I created my own configuration, as per below:

- platform: template
  sensors:
    door_sensor_battery_level:
      friendly_name: "Door Sensor Battery Level"
      entity_id: sensor.door_sensor_battery_level
      unit_of_measurement: '%'
      value_template: "{{ state_attr('binary_sensor.openclose_13', 'battery_level') }}"

After created and restart HA, this sensor.door_sensor_battery_level entity id is not appeared under Developer > States > Current Entities in HA.

So, i can’t select sensor.door_sensor_battery_level to display it under HA homepage using sensor card configuration, whereby my purpose is just to display this battery level.

Could anyone assist on this?

Thanks.

2 Likes

the entity_id is the id of the entity to track, NOT the name of the template sensor that you’re trying to create (this will be sensor.door_sensor_battery_level)
So you need this:

- platform: template
  sensors:
    door_sensor_battery_level:
      friendly_name: "Door Sensor Battery Level"
      entity_id: binary_sensor.openclose_13
      unit_of_measurement: '%'
      value_template: "{{ state_attr('binary_sensor.openclose_13', 'battery_level') }}"
6 Likes

+1 to what lolouk44 said.

You may also consider adding the device_class option like this:

    device_class: battery
3 Likes

It is working now.

Thanks for the help.

1 Like

@lolouk44 and @123,

Just curious, how to change the icon of the template sensor into battery icon?
image

Currently it is an eye icon.

Thanks.

icon: mdi:battery in the config…

Not working.

Have config error after added icon: mdi:battery, as per screenshot below:
image

show your code

Here is my code:

- platform: template
  sensors:
    door_sensor_battery_level:
      friendly_name: "Door Sensor Battery Level"
      entity_id: binary_sensor.openclose_13
      unit_of_measurement: '%'
      value_template: "{{ state_attr('binary_sensor.openclose_13', 'battery_level') }}"
      icon: mdi:battery
1 Like

From the docs…

icon_template: mdi:battery

Seems you need an icon template

1 Like

device_class

(device_class)(Optional)Sets the class of the device, changing the device state and icon that is displayed on the UI (see below). It does not set the unit_of_measurement .

Default value: None

As was already suggested, use device_class: battery

1 Like

Yes, it is working perfectly now.

image

Thanks for the great help!

Is the definition of a custom sensor template the ONLY way to show said attributes in a card?

I find that going to the configuration and to introduce a new virtual sensor for each attribute is quite involved.

I’m still trying to figure all this out myself. But I found you can call the attribute directly by the card using service calls.

Here’s what I have so far for a conditional card during weather events.

card:
  color: red
  color_type: card
  entity: sensor.warnings
  layout: icon_state
  show_name: false
  show_state: true
  size: 100%
  styles:
    card:
      - height: 60px
    grid:
      - grid-template-areas: '"i s" "n n" "l l"'
      - grid-template-columns: 20% 1fr
      - grid-template-rows: 1fr min-content min-content
    state:
      - text-transform: uppercase
      - font-weight: bold
      - justify-self: start
  tap_action:
    action: call-service
    service: browser_mod.popup
    service_data:
      card:
        content: '{{ state_attr(''sensor.warnings'', ''alert detail'') }}'
        type: markdown
      deviceID:
        - this
      title: Alert Details
  type: 'custom:button-card'
conditions:
  - entity: sensor.bad_weather
    state_not: 'False'
type: conditional
2 Likes

This works as a charm also for my Eurotronic Zigbee thermostat. Please note that entity_id is now deprecated, so you can just skip it.

  - platform: template
    sensors:
      thermostat_ronald_current_temp:
        friendly_name: "Thermostat Ronald current temp"
        unit_of_measurement: "°C"
        value_template: "{{ state_attr('climate.thermostat_31', 'current_temperature') }} "
      thermostat_ronald_temp:
        friendly_name: "Thermostat Ronald temp"
        unit_of_measurement: "°C"
        value_template: "{{ state_attr('climate.thermostat_31', 'temperature') }} "

I am in the process of doing this, can you please confirm if the icon changes due to the battery % or do we need to show this a template for this? with an IF function.

I am trying to use the new energy dashboard with my vesync switches. Here is one of the sensors:

switch.washer

attributes:

current_power_w: 1.67
today_energy_kwh: 12.2886
voltage: 123.13
weekly_energy_total: 0.63
monthly_energy_total: 0.3915
yearly_energy_total: 23.8708
friendly_name: Washer

I am trying to create a template sensor for energy (kWh) and have the following within my template sensors config:

laundry_energy_today:
        friendly_name: "Landry Energy Today"
        entity_id: switch.washer
        unit_of_measurement: 'kWh'
        value_template: "{{ state_attr('switch.washer', 'today_energy_kwh') }}"

But I am getting a deprecated alert on using the switch as the entity_id.

Is there any other way I can get my smart plugs into my energy dashboard from a switch entity?

The entity_id option was deprecated many versions ago. You can simply remove it. However, the resulting Template Sensor will not be compatible with the Energy integration.

Why? Because it lacks this:
state_class: measurement

However, you cannot simply add that to the Template Sensor you posted because it uses the old-style method of configuring a Template Sensor (what is now known as the ‘legacy’ method). The ‘legacy’ style of Template Sensor doesn’t support the state_class option.

You have to define the Template Sensor using the new ‘modern’ format as shown in the documentation for the Template integration. It supports the state_class option.

There’s an example posted here with additional information:

1 Like

Do you put this under the yaml configuration file? I have something similar like the below that I cannot get to pull up as a sensor in any card.

template:
    sensor:
     - inside_humidity:
          friendly_name: "Carrier Inside Humidity"
          entity_id: climate.bedrooms_hallway_1
          unit_of_measurement: '%'
          value_template: "{{ state_attr('climate.bedrooms_hallway_1', 'current_humidity') }}"

``

Check the documentation link that was provided earlier:

It’ll give you the supported variables. friendly_name is not one of them
Otherwise your code looks ok at first glance…