How to delete duplicate Template Sensors - Helper

After coming across the fact that I can now create template sensors in Helpers, I decided to switch over to using the Helper construct rather than definining the sensors in YAML.

I had created a lot of template sensors and still have a few because the Helpers don’t allow the definition of attributes for template sensors.

I deleted the sensors I wanted to shift over to the Helper construct from the sensors.yaml file, and then rebooted HA, thinking that this would delete the template sensors from HA. I then started creating the template sensors as Helpers.

The problem is that for some reason the unique_id for some of the sensors has been retained, although the state is unavailable. When I recreate the template sensors in Helper a _2 is appended to the unique_id.

The problem is that I have a lot of Automations that use the template sensors. Editing all the automations to add the _2 is infeasible.

How do I get rid of the sensors associated with the original unique_id so that I can reuse the original unique_id?

Below are my remaining template sensors created in YAML.

configuration.yaml:
sensor: !include sensors.yaml

sensors.yaml:

- platform: template
  sensors:
    pws_report_details: 
      unique_id: "pws_report_values"
      friendly_name: "PWS Report"
      value_template: '{{ states.sensor.pws_report.attributes.observations[0]["stationID"] }}'
      attribute_templates:
        obs_time: '{{ states.sensor.pws_report.attributes.observations[0].obsTimeLocal }}'
        solar_radiation: '{{states.sensor.pws_report.attributes.observations[0].solarRadiation }}'
        wind_dir: '{{ states.sensor.pws_report.attributes.observations[0].winddir }}'
        humidity: '{{ states.sensor.pws_report.attributes.observations[0].humidity }}'
        temperature: '{{ states.sensor.pws_report.attributes.observations[0].metric.temp }}'
        heat_index: '{{ states.sensor.pws_report.attributes.observations[0].metric.heatIndex }}'
        dewpt: '{{ states.sensor.pws_report.attributes.observations[0].metric.dewpt }}'
        windchill: '{{ states.sensor.pws_report.attributes.observations[0].metric.windChill }}'
        wind_speed: '{{ states.sensor.pws_report.attributes.observations[0].metric.windSpeed }}'
        wind_gust: '{{ states.sensor.pws_report.attributes.observations[0].metric.windGust }}'  
        pressure: '{{ states.sensor.pws_report.attributes.observations[0].metric.pressure }}'
        precip_rate: '{{ states.sensor.pws_report.attributes.observations[0].metric.precipRate }}'
        precip_total: '{{ states.sensor.pws_report.attributes.observations[0].metric.precipTotal }}'

    stair_brightness:
      unique_id: "stair_brightness"
      friendly_name: "Stair Brightness"
      value_template: >-
        {% set result = namespace(brightness=0.0) %}
        {% if states.sensor.stair_top_brightness.last_changed > 
          states.sensor.stair_bottom_brightness.last_changed %}
          {% set result.brightness = states('sensor.stair_top_brightness') | float(0) %}
        {% else %}
          {% set result.brightness = states('sensor.stair_top_brightness') | float(0) %}
        {% endif %}
        {{ result.brightness }}
      attribute_templates:
        which: >-
          {% set result = namespace( which='') %}
          {% if states.sensor.stair_top_brightness.last_changed > 
              states.sensor.stair_bottom_brightness.last_changed %}
            {% set result.which = 'top' %}
          {% else %}
            {% set result.which = 'bottom' %}
          {% endif %}
          {{ result.which }}

    unavailable_entities:
      friendly_name: "Unavailable Entities"
      value_template: >-
        {{ states | selectattr('state','eq', 'unavailable') | list | count }}
      attribute_templates: 
        which: >-
          {{ states | selectattr('state','eq', 'unavailable') | map(attribute='name') | list | join(';\n') }}

Below is a small sample of the issue exposed through Developer Tools → States.

I think there can be several causes here, as you mention ( for some )

One thing for sure, if you haven’t “wiped/purged” the DB, the entries for these entity_ids will remain
And If you have “references” to these entities, in script/automations/cards etc, these “entries/entities” will be called(upon restart/view etc), and therefore these “names” will still exist , and “block” a new/same unique_id/entity_id, and therefore a trailing _2

Im not sure what’s "unique/mutual denominator " for these (some entities) thou

The process for deleting YAML entities that have a unique id requires two steps.

  1. Delete the yaml and restart or reload.

  2. Go to Settings → Devices & Services then navigate to the Entities page using the tab at the top of the page. Click on the far right “Status” column to sort by this. Select and delete the “Restored” entities with this icon in the status column: 467eaa45300e347ea72a4e69c77201932f71bbf4

3 Likes

Note: do not delete the entities with this icon: Screenshot 2024-04-03 at 12-22-08 Settings – Home Assistant

They are legitimate entities with an “unknown” state.

2 Likes

Much appreciated tom_i

1 Like

Is there a more efficient way of deleting all the restored sensors? I have well over 100 after converting to the Helper. Deleting each one individually is going to be very tedious.

Related question: How can I see all the template sensors created through Helpers that have the “_2” appended? It will be a very tedious process to go into every template sensor in Helpers.

I’m looking for something similar to the automations.yaml file.

You can select more than one at a time.

I can’t see how. This is the screen I see.

The moment I click on any one of these restored entities, the following popup windows appears.

don’t be afraid to try and click the menu’s

It’s new.

same wish here. i can select multiple devices but not batch delete them. only change category for selection. (labled them “trash” at least)

It used to be up front and obvious but since 2024.4.0 it is now hidden in the three dots menu top right (after selecting).

ok. I just realized with entities its possible as described but not with devices (yet).
i got many trash sensor devices from a rtl433 integration i want to remove.

You remove devices from the integration. I’m not familiar with the rtl433 integration but if it is like other integrations set up from the UI you do it (one by one unfortunately) from the integration page. e.g. (using ESPHome as an example)

Then:

Thanks for your effort.
The RTL433 addon spawns MQTT devices, so going via that integration points me to the devices list again. I guess i still have to delete manually until the bulk delete surfaces.

Ah, yes. Mqtt is different.

Delete the retained discovery messages from your mqtt broker.

You can do this by sending a blank retained message to each discovery topic (under /homeassistant) you want to delete or by using MQTT Explorer and it’s GUI.