[Custom Component] Battery Notes

Thanks, no card as yet, I don’t use custom cards much myself, I tend to use auto entities a lot to create lists of devices needing attention and have never looked into them, cards are quite a different skill to learn.
It may be something I will explore when I get some free time unless someone else comes up with one.

I have one that I’ve been using for a while. I just need to update it to use some of the new features of this integration and can post it when I get time.

It uses auto-entities and config-template-card. I’ll try and break it out into a single piece of YAML at some point.

The battery type is only shown if there’s a a matching entity for it. My aim will be to have the icon act as the “replace” button with the rest of the cars being more-info.

Screenshot just in case you decide it’s too ugly for you. :rofl:

2 Likes

Can you post your yaml code?

Try this, hopefully it’ll provide you a starter.

You’ll need to have the following custom cards installed: -

If the battery is managed by Battery Notes you will see the battery type in parentheses after the name and the icon tap action will prompt for confirmation that you’ve replaced the battery.

If the battery is not managed by Battery Notes then the card behaves like the normal Tile card (i.e. will display more-info)

type: custom:auto-entities
card:
  type: vertical-stack
card_param: cards
filter:
  exclude:
    - state: unavailable
  include:
    - attributes:
        device_class: battery
      options:
        type: custom:config-template-card
        entities:
          - this.entity_id
        variables:
          getBatteryLabel: |
            () => {
              let battery_label = states[this._config.entities[0]].attributes.friendly_name.replace(/(battery|level)/gi, '').trim()
              let battery_type = states[this._config.entities[0] + "_type"]
              if (battery_type) {
                return `${battery_label} (${battery_type.state})`
              }
              return battery_label
            }
          getIconAction: |
            () => {
              let battery_button = states['button.' + this._config.entities[0].split('.')[1] + '_replaced']
              let ret = {'action': 'more-info'}
              if (battery_button) {
                ret.action = 'call-service'
                ret.confirmation = {'text': 'Battery replaced?'}
                ret.data = {'entity_id': battery_button.entity_id}
                ret.service = 'button.press'
              }
              return ret
            }
        card:
          type: tile
          entity: this.entity_id
          icon_tap_action: ${getIconAction()}
          name: ${getBatteryLabel()}
      sort:
        method: name
sort:
  method: state
  numeric: true

3 Likes

Thanks. This works. From here it is easy to see what needs changing. Is there anyway to configure the change date other than through the integration?

You’ll need to dig in to the JSON files that hold the entities in the storage location. Much of that isn’t documented overly well by the Home Assistant team and probably for good reason; bad config could cause issues.

First of all - I pretty much like batterynotes.
I have a question or request.
After last update the names of new devices are not anymore in english even though I have not changed anything in my setup and in my profile I have alected english as language. It seems to me language of browser is used in new releases.
Is there a way to configure or will it be changed in the future and language is derived from user profile ?

I’m just relying on HA’s translation capabilities so this should be standard for all devices, not just Battery Notes.
Can you give me a screenshot of where this is happening?

Thank for following up so fast.
Maybe its a user problem, but I don’t know how to solve. Besides english as language setting for HA I also switched firefox to english on my German Windos desktop. Even in the android app with language set to english I get german names.

Here some info:
This water sensor was installed with the englisch entityname with earlier versions of batterynotes, update with later versions lead to german names for the replacement entities.


After removing and adding again also “battery_type” now is german "batterieart.

My language settings:


My log file for recent battery notes acticities

2024-01-02 22:43:32.695 DEBUG (MainThread) [custom_components.battery_notes] Removed Device a64665fc65e6081666c5fa39ba466b68
2024-01-02 22:45:48.059 DEBUG (MainThread) [custom_components.battery_notes.library_coordinator] Skipping library updates
2024-01-02 22:45:48.059 DEBUG (MainThread) [custom_components.battery_notes] Finished fetching battery_notes data in 0.001 seconds (success: True)
2024-01-02 22:45:48.063 DEBUG (MainThread) [custom_components.battery_notes.config_flow] Looking up device Xiaomi LYWSD03MMC
2024-01-02 22:45:48.064 DEBUG (MainThread) [custom_components.battery_notes.config_flow] Found device Xiaomi LYWSD03MMC
2024-01-02 22:45:57.112 DEBUG (MainThread) [custom_components.battery_notes.coordinator] Update coordinator
2024-01-02 22:45:57.113 DEBUG (MainThread) [custom_components.battery_notes.coordinator] Finished fetching battery_notes data in 0.001 seconds (success: True)
2024-01-02 22:45:57.188 DEBUG (MainThread) [custom_components.battery_notes.library_coordinator] Skipping library updates
2024-01-02 22:45:57.189 DEBUG (MainThread) [custom_components.battery_notes] Finished fetching battery_notes data in 0.001 seconds (success: True)
2024-01-02 22:49:14.947 DEBUG (MainThread) [custom_components.battery_notes] Removed Device a0da0025c623821e7583404ba7645c4b
2024-01-02 22:49:24.166 DEBUG (MainThread) [custom_components.battery_notes.library_coordinator] Skipping library updates
2024-01-02 22:49:24.167 DEBUG (MainThread) [custom_components.battery_notes] Finished fetching battery_notes data in 0.001 seconds (success: True)
2024-01-02 22:49:24.174 DEBUG (MainThread) [custom_components.battery_notes.config_flow] Looking up device LUMI lumi.sensor_wleak.aq1
2024-01-02 22:49:24.174 DEBUG (MainThread) [custom_components.battery_notes.config_flow] Found device LUMI lumi.sensor_wleak.aq1
2024-01-02 22:49:26.562 DEBUG (MainThread) [custom_components.battery_notes.coordinator] Update coordinator
2024-01-02 22:49:26.562 DEBUG (MainThread) [custom_components.battery_notes.coordinator] Finished fetching battery_notes data in 0.001 seconds (success: True)
2024-01-02 22:49:26.645 DEBUG (MainThread) [custom_components.battery_notes.library_coordinator] Skipping library updates
2024-01-02 22:49:26.646 DEBUG (MainThread) [custom_components.battery_notes] Finished fetching battery_notes data in 0.001 seconds (success: True)"

I managed to reproduce this by going to Home Assistant Settings → System → General and changing the language there and restarting HA.

It is a bug with Battery Notes, the sensor entity ID’s should be English so button.xxxx_battery_replaced

The actual name displayed against the sensor is the System language, rather than what the user has selected in their profile. This is consistent with other integrations I’ve checked and seems to be an omission within HA’s translation implementation.

I have a fix for the entity id I’m currently testing and will hope to get that out today.

Thanks a lot for the fast fix.
I reinstalled everything from scratch and it works properly now.

Thanks for the confirmation and the coffee :smiley:

Many thanks for this integration @codechimp , really helpful!
Especially the possibility to administer the battery replacement dates :slight_smile:

I would like to create a list of batteries and amounts. In that way I can see how many CR2450’s I have, how many CR1632 batteries etc. etc.

Is this possible? I did not see any attributes, so I guess templating is not possible to create such an overview.

Any thoughts/help/tips would be much appreciated!

Thanks

1 Like

The battery type is in the state of each battery_type entity. You could get the count from that.

Aha! Okay, that’s a good start. I see it now via dev tools indeed.
My templating magic is ‘zero’ , so I was hoping somebody already had something like this. It would be great to have an overview of types and quantities. Than I know how much I need to buy at some point.

Maybe interesting to create an automatic shopping list to :sweat_smile: Or counting how much you have on stock, like people do with dishwasher tablets…

By the end of the week I’ll have a new version with attributes for type and quantity to make that easier.
Codes all written, I just don’t like releasing if I’ve got a busy work day :slight_smile:

2 Likes

Whoopa! Sounds great, I’ll wait for it :sunglasses:
Thanks for the scoop :sweat_smile:

1 Like

Does this help in the interim?

It uses card_mod and the markdown card. It also assumes that you are using the format that is suggested when adding battery type entities, i.e. <battery_count>x <battery_type>

type: markdown
title: Battery Summary
card_mod:
  style:
    ha-markdown$: >
      table { width: 100%; border-collapse: separate; border-spacing: 0px; }
      tbody tr:nth-child(2n+1) { background-color: var(--table-row-background-color); }
      thead tr th, tbody tr td {padding: 4px 10px; }
content: >
  {% set ns_batteries = namespace(batteries={}) %}

  {% for entity_id in integration_entities('battery_notes') if entity_id is
  search('_battery_type$', ignorecase=False) -%}
      {% set battery_type = states[entity_id].state %}
      {% set battery_split = battery_type.split('x') %}
      {% if battery_split | length > 1 %}
        {% set battery_type = battery_split[-1] | trim %}
        {% set battery_count = battery_split[0] | int(1) %}
      {% else %}
        {% set battery_count = 1 %}
      {% endif %}
      {% if battery_type not in ns_batteries.batteries %}
          {% set ns_batteries.batteries = dict(ns_batteries.batteries, **{battery_type: battery_count}) %}
        {% else %}
          {% set ns_batteries.batteries = dict(ns_batteries.batteries, **{battery_type: ns_batteries.batteries[battery_type] + battery_count}) %}
      {% endif %}
  {% endfor %}


  | Type | Count |
  
  | :-- | --: |
  
  {% for bt in ns_batteries.batteries -%}
    | {{ bt }} | {{ ns_batteries.batteries[bt] }} |
  {% endfor %}

image

2 Likes

You are a hero!
Thanks a lot for this (interim) solution!

Thank you :ok_hand:

image

You can use this tweaked one if you want the table sorted in reverse order by type count.

type: markdown
title: Battery Summary
content: >
  {% set ns_batteries = namespace(batteries={}) %}

  {% for entity_id in integration_entities('battery_notes') if entity_id is
  search('_battery_type$', ignorecase=False) -%}
      {% set battery_type = states[entity_id].state %}
      {% set battery_split = battery_type.split('x') %}
      {% if battery_split | length > 1 %}
        {% set battery_type = battery_split[-1] | trim %}
        {% set battery_count = battery_split[0] | int(1) %}
      {% else %}
        {% set battery_count = 1 %}
      {% endif %}
      {% if battery_type not in ns_batteries.batteries %}
          {% set ns_batteries.batteries = dict(ns_batteries.batteries, **{battery_type: battery_count}) %}
        {% else %}
          {% set ns_batteries.batteries = dict(ns_batteries.batteries, **{battery_type: ns_batteries.batteries[battery_type] + battery_count}) %}
      {% endif %}
  {% endfor %}


  | Type | Count |

  | :-- | --: |

  {% for bt in ns_batteries.batteries | dictsort(False, 'value') | reverse -%}
    | {{ bt[0] }} | {{ [bt][0][1] }} |
  {% endfor %}
card_mod:
  style:
    ha-markdown$: >
      table { width: 100%; border-collapse: separate; border-spacing: 0px; }
      tbody tr:nth-child(2n+1) { background-color:
      var(--table-row-background-color); } thead tr th, tbody tr td {padding:
      4px 10px; }

image

1 Like