Elegant way to reduce repetition in template sensor templates

Hi!

My overall goal is to monitor my NAS backups with Home Assistant.

I have six instances of kopia runnning in a docker container each to backup folders from my NAS to external hard drives and to the cloud. I have a seventh container (I will call it scheduler) that starts a backup-test task for every of the six instances daily. The test has a test result (only pass or fail) which I want to monitor in Home Assistant. A notification should be sent to my phone once any test fails.

Currently my approach is to send a PUT request from the scheduler container to a Home Assistant webhook with two parameters: backup name and test result. I have set up six template sensors to receive those results. This is the config which doesn’t work because you can’t do this.name in a template sensor:

template:
  - trigger:
      - platform: webhook
        allowed_methods:
          - PUT
        local_only: true
        webhook_id: -somethingSomething
    sensor:
      - &kopia_backup_state
        name: "kopia-backup1"
        # Only update state if the container_name matches the sensor name
        state: "{{ iif(this.name == trigger.data.container_name, trigger.data.state, this.state) | replace('0', 'Test is running') | replace('1', 'Success') | replace('2', 'Failure') }}"
        icon: mdi:database
      - <<: *kopia_backup_state
        name: "kopia-backup2"
      - <<: *kopia_backup_state
        name: "kopia-backup3"
      - <<: *kopia_backup_state
        name: "kopia-backup4"
      - <<: *kopia_backup_state
        name: "kopia-backup5"
      - <<: *kopia_backup_state
        name: "kopia-backup6"

I wanted to implement it this way to reduce repetition in my config. But sadly this.name is not available in templates. My idea was to only have one webhook which only updates one sensor with the new state depending on the name.

I could just copy everything six times but I feel like there should be a more elegant way. Do you have any suggestions, or a completely different approach? I can modify the scheduler container and its logic however I want.

this represents the entity’s State Object.

Does trigger.data.container_name contain the sensor’s object_id? For example, the kopia_backup1 in sensor.kopia_backup1 is the object_id. Note that it uses an underscore not a hyphen because an object_id is a “slug” that can only contain letters and numbers and any other character in name is converted to an underscore.

If it does then try using this.object_id.

1 Like

does the scheduler save to a log file for pass / fail information? this would be helpful then you could just parse that log file , if you have any fails you could create a trigger on the word “fail” to then notify you of the error

1 Like

this is the previous state object of the sensor in question, but it is not defined until after the trigger has fired previously. You need to provide a default for the initial “setup” firing when this is undefined:

{% if this is undefined %} 
  Setting Up
{% else %}
  {# Only update state if the container_name matches the sensor name #}
  {{ iif(this.name == trigger.data.container_name, trigger.data.state, this.state) | replace('0', 'Test is running') | replace('1', 'Success') | replace('2', 'Failure') }}
{% endif %}
1 Like

this.object_id is empty for me. But interestingly this.entity_name is sensor.kopia_backup1. I could use that instead.

Even better thanks to your link to the documentation, I am just using this.attributes.friendly_name now since this is the exact name I get from the scheduler and which I have in the config.

this.name and this.attributes.friendly_name are equivalent.