Template Not Creating a Sensor

I am working with a very simple template, that looks like this:

template:
  - sensor:
      - name: "nextsunrise"
        friendly_name: "Next Sunrise"
        value_template: >
          {{ as_timestamp(states.sun.sun.attributes.next_rising) | timestamp_custom(' %I:%M %p') | replace(" 0", "") }}
        icon_template: mdi:weather-sunset-up

The entity sun.sun does exist, and the attribute next_rising also exists, with a valid value. When I put the template into the the Template Editor, the correct value is returned. HOWEVER - the sensor “sensor.nextsunrise” does not get created. I cannot find it using STATES, nor can I find it when trying to use that value in a dashboard. I’ve been looking through the documentation, and I cannot seem to figure out where I’m screwing this up. Help?

Sean

When you add a new entity for the first time, you have to restart the system. After that, you can reload yaml and it will pick up changes for new template entities.

As a sidebar, you can jsut create this in the UI without needing to deal with yaml.

Lastly, you should add some safety into your template. There’s many potential error points. You can use this template and it will not error

{{ (state_attr('sun.sun', 'next_rising') | as_datetime(now()) | as_local).strftime('%-I:%M %p') }}

Hi Petro -

I’ve tried that too. I did that again, just now, to see if maybe something was stuck. But no luck. Hence, why I’m pulling what little bit of hair I have left - everything seems to be right.

Sean

If that’s not working, then you have something wrong in configuration.yaml. Check your log for errors and/or post your entire config here.

Anything that saves from having to deal with YAML would be helpful. :slight_smile: I was not aware that I could create this in the UI without needing a template, though.

Sean

You can create template entities in the ui, you still need a jinja template for any template entity

OK, here is the entire configuration.yaml file:

https://dpaste.org/GcvPm

There’s not much there, but there must be something making it very unhappy.

Sean

Yes, you should have 3 errors in your logs about friendly_name, value_template, and icon_template.

The valid fields are name, state, and icon respectively.

That did it. Thank you! Tutorial time… which log would I expect to see that, as there are several logs available?

Sean

I’d have to see the logs, I don’t have the errors memorized but I can recognize it.

Thank you for your help. Your assistance helped me clean up this template, and several others as well. Very, very much appreciated.

Sean

1 Like