Sensor not being created

Hi,
I’ve created a sensor based on information in youtube video “Move from custom garbage collection to local calendars!” (https://www.youtube.com/watch?v=RpwAhy7f52I)

There is a sensor to calculate number of days to the calendar entry for a certain type of garbage collection.

template:
  - sensor:
    - name: recycling_days
      state: >-
        {{ ((state_attr('calendar.recycling','start_time') | as_timestamp - today_at('00:00') | as_timestamp) / 86400) | int }}

When this is entered into the template page of developer tools it produces the expected result.
But it turns out that the sensor itself is not being created. It does not appear in the entities in the states page.
It’s also not available as an option in the logbook entities.

Could any of you fine much smarter than me people point out where I’ve gone wrong ?
Thanks

Silly question: what do you mean by “it turns out”? I assume you are transferring the template into configuration.yaml (or wherever you keep your templates), then reloading template yaml?

Yes the code used in the template produces the expected result when tested in the template page, but the same code (as above) when added to configuration.yaml does not then create the entity.

HA has been restarted, yaml reloaded many times since, but the entity is not created.

Check the Log for related errors.

The entity does not exist in Logbook, and theres no mention of it in the full log.

What I have noticed though is this

2023-08-21 21:29:42.149 WARNING (SyncWorker_0) [homeassistant.util.yaml.loader] YAML file /config/configuration.yaml contains duplicate key "template". Check lines 86 and 99

The line numbers correspond to blank lines before a sensor is defined below.

So what I do have is multiple entries of

template: 
  - sensor:
    - name:

for 3 different sensors.
Is the problem that I should only have “template:” once, and following sensor definitions should just start with

template: 
  - sensor:
    - name:

  - sensor:
    - name:

  - sensor:
    - name:

and only have template: in configuration.yaml once ?

Yup. That’s it.

I asked for related errors in the Log (not Logbook).

Not sure why you said that because what you posted here is from the Log:

2023-08-21 21:29:42.149 WARNING (SyncWorker_0) [homeassistant.util.yaml.loader] YAML file /config/configuration.yaml contains duplicate key “template”. Check lines 86 and 99

It clearly indicates there’s a configuration error. You have duplicates of the template key. That’s why your Template Sensor isn’t being loaded.

Correct. The key word template: should only appear once.

The configuration of all Template entities should be located under a single template: key.

Sorry for the delay, working lots of hours, limted time off etc.

But many thanks for your help.

1 Like