Example template from documentation not working

Dear Home Assistant Community

I’m new to Home Assistant and I’m having a hard time getting a simple template to work and after hours of try-and-error and lots of googling i’m at the end of my wits.

Eventually, I even tried to use one of the examples provided by the documentation without success.

I really don’t know what I’m missing:

  • The configured state works when I test it in the developer tools
  • No errors are shown in the log
  • It works when I create the template through the UI
  • Whatever I try, when creating the template in a file, the resulting state is Unknown

I’m running:

  • Core: 2024.11.1
  • Frontend: 20241106.2

configuration.yaml

...
template: !include 'template.yaml'

template.yaml

trigger: []
condition: []
action: []
sensor:
  - name: 'Sun Angle test'
    unit_of_measurement: "°"
    state: "{{ '%+.1f'|format(state_attr('sun.sun', 'elevation')) }}"
  - name: "Sun State"
    state: >
      {% if is_state('sun.sun', 'above_horizon') %}
        up
      {% else %}
        down
      {% endif %}

Hi Michel,

When you put that into the developer - template, what do you get?

Open your Home Assistant instance and show your template developer tools.

Hi @Sir_Goodenough
It works:

1 Like

I don’t think you want the trigger/condition/action in there.

You just want the sensor.

With the trigger in there and it is blank, that will never run.

1 Like

Hi @jeffcrum
This solved it and it works as expected now. Thanks a lot!

I’m a little confused by this behaviour. So just for my understanding, considering this example from the documentation and paying attention to the intention of sensor (afaik, this is relevant for yaml) , I assumed there are 2 different types of sensors defined as a template, trigger-based and non-trigger-based. Is my assumption incorrect?

Or asked differently:
How can you concurrently define sensors that are trigger-based and those that are not trigger-based?

You use hyphens to define a YAML list

  - trigger: #List item 1. Everything from here to the next list item depends on this trigger block.
      - trigger:
    sensor:
      - name: ...

  - sensor: #List item 2. There is no trigger defined, so these are state-based template sensors
      - name: 'Sun Angle test'
        unit_of_measurement: "°"
        state: "{{ '%+.1f'|format(state_attr('sun.sun', 'elevation')) }}"
      - name: "Sun State"
        state: >
          {% if is_state('sun.sun', 'above_horizon') %}
            up
          {% else %}
            down
          {% endif %}

  - trigger: #List item 3. Everything from here to the next list item depends on this trigger block.
      - trigger:
    binary_sensor:
      - name: ...
2 Likes

:see_no_evil: thanks a lot for clarifying this.

I was convinced that:

trigger: ...
sensor: ...

equals to

- trigger: ...
- sensor: ...

as long as the implementation that interprets the configuration accepts both property and list elements

And that:

trigger:
    sensor: ...

(in json this is to me)

{
    "trigger": {
        "sensor": ...
    }
}

does not equal to

trigger: ...
sensor: ...

(in json this is to me)

{
    "trigger": ... ,
    "sensor": 
}

I never was the biggest fan of yaml :smile:

Great community :mechanical_arm:. Thanks everyone for the quick help :pray:

Now it all makes sense, for completeness sake:

It also works with

- trigger: []
- condition: []
- action: []
- sensor:
  - name: 'Sun Angle test'
    unit_of_measurement: "°"
    state: "{{ '%+.1f'|format(state_attr('sun.sun', 'elevation')) }}"
  - name: 'Sun State'
    state: >
      {% if is_state('sun.sun', 'above_horizon') %}
        up
      {% else %}
        down
      {% endif %}

in JSON this equals to (considering configuration.yaml as well)

...
"template": [
    {
        "trigger": []
    },
    {
        "condition": []
    },
    {
        "action": []
    },
    {
        "sensor": [
            { "name": "Sun Angle test", ... },
            { "name": "Sun State", ... }
        ]
    }
]

opposed to

trigger: []
condition: []
action: []
sensor:
  - name: 'Sun Angle test'
    unit_of_measurement: "°"
    state: "{{ '%+.1f'|format(state_attr('sun.sun', 'elevation')) }}"
  - name: 'Sun State'
    state: >
      {% if is_state('sun.sun', 'above_horizon') %}
        up
      {% else %}
        down
      {% endif %}

in JSON equals to (considering configuration.yaml as well)

...
"template": {
    "trigger": [],
    "condition": [],
    "action": [],
    "sensor": [
        {"name": "Sun Angle test", ... },
        {"name": "Sun State", ... }
    ]
}

I’ll never understand why people choose YAML instead of JSON

But why would you do that? The configuration variables condition and action are not actually usable under the template integration without a trigger

1 Like

I’m migrating from openHAB and anticipate needing trigger-based sensors. But how I ended up today with this configuration has more to do with the migration approach I chose. Since I’m new to Home Assistant, I tend to start by copying the full configuration examples from the documentation to get a baseline.