Template Sensor does not recognize it's name correctly

I am using the following template sensor, where the name of the sensor is dependent on the trigger data:

  - trigger:
      platform: event
      event_type: downloader_download_completed
    sensor:
      - name: >-
          {% if "NOC-human" is in trigger.event.data ['filename'] %}
            Snapshot time NOC-human
          {% elif "NOC-vehicle" is in trigger.event.data ['filename'] %}
            Snapshot time NOC-vehicle
          {% elif "NOC-animal" is in trigger.event.data ['filename'] %}
            Snapshot time NOC-animal
          {% else %}
            Snapshot time NOC-unknown
          {% endif %}

        state: >
          {{ trigger.event.data ['filename'].split ("_")[0]
              | int | timestamp_custom ('%Y-%m-%d %H:%M:%S', true) }}

The sensor shows up as sensor.unnamed_device, where as the friendly name is picked up correctly!

Since the template works correct for the friendly name, I consider it as a software issue, that the entity name is sensor.unnamed_device.

Has anybody an explanation or similiar experiences?

Add the unique_id option. Assign it a slug (an alphanumeric string in lowercase and no spaces) such as snapshot_time_noc

thanks for the hint - some progress…

I added unique_id’s

  - trigger:
      platform: event
      event_type: downloader_download_completed
    unique_id: noc_snapshot_
    sensor:
      - unique_id: time_pic_
        name: >-
          {% if "NOC-human" is in trigger.event.data ['filename'] %}
            human
          {% elif "NOC-vehicle" is in trigger.event.data ['filename'] %}
            vehicle
          {% elif "NOC-animal" is in trigger.event.data ['filename'] %}
            animal
          {% else %}
            unknown
          {% endif %}

        state: >
          {{ trigger.event.data ['filename'].split ("_")[0]
              | int | timestamp_custom ('%Y-%m-%d %H:%M:%S', true) }}

Now the sensor contains the unique_id’s, but still not the dynamic part from the template

  • The sensor’s entity_id is now being derived from unique_id

  • The sensor’s friendly_name is still derived from the name option, same as it was before you added unique_id.

You want to dynamically change the sensor’s entity_id via its name option and I don’t believe that’s possible. I’m led to that conclusion because there are only two sensor options available for controlling the sensor’s naming and you’re now using both of them.

It took me a minute or two to figure out what’s going on here. So basically, this is trying to blueprint sensors, right? Very interesting. Since we have blueprints for scripts and automations, it might be valuable to create an FR for this.

1 Like