Help with Invalid config for [template] error

Hi there!

I’m running into an issue when attempting to use a template…

I have this in my configuration.yaml

homeassistant:
    packages: !include_dir_named sensors

Here’s my /config/sensors/templates.yaml file

template:
    sensors:
    kitchen_smoke_co_alarm:
        friendly_name: 'Kitchen Smoke & CO Detector Status'
        value_template: >-
            {% if is_state("sensor.kitchen_smokeco_alarm_alarmlevel", "255") and is_state("sensor.kitchen_smokeco_alarm_alarmtype", "1")%}
            SMOKE DETECTED!!!!
            {% elif is_state("sensor.kitchen_smokeco_alarm_alarmlevel", "255") and is_state("sensor.kitchen_smokeco_alarm_alarmtype", "2")%}
            CARBON MONOXIDE ALARM!!!
            {% elif is_state("sensor.kitchen_smokeco_alarm_alarmlevel", "0") and is_state("sensor.kitchen_smokeco_alarm_alarmtype", "12")%}
            Test!!! XXX Test!!!
            {% elif is_state("sensor.kitchen_smokeco_alarm_alarmlevel", "255") and is_state("sensor.kitchen_smokeco_alarm_alarmtype", "13")%}
            Safe - Normal Operation
            {% else %}
            UNKOWN
            {% endif %}
        icon_template: >-
            {% if is_state("sensor.kitchen_smokeco_alarm_alarmlevel", "255") and is_state("sensor.kitchen_smokeco_alarm_alarmtype", "1")%}
            mdi:fire
            {% elif is_state("sensor.kitchen_smokeco_alarm_alarmlevel", "255") and is_state("sensor.kitchen_smokeco_alarm_alarmtype", "2")%}
            mdi:cloud-outline
            {% elif is_state("sensor.kitchen_smokeco_alarm_alarmlevel", "0") and is_state("sensor.kitchen_smokeco_alarm_alarmtype", "12")%}
            mdi:alert-octagram
            {% elif is_state("sensor.kitchen_smokeco_alarm_alarmlevel", "255") and is_state("sensor.kitchen_smokeco_alarm_alarmtype", "13")%}
            mdi:smoke-detector
            {% endif %}

Thank you for the help, really appreciate it!

You didn’t post the actual error but from the code above the indentation on the second line (sensors:) is off. It needs moved to the left by two spaces.

1 Like

And the icon template has no “else”-part.

I don’t know if that would cause an error but I think it would be better to add that part so there is always in icon.

1 Like

Thank you so much, this fixed it!! I’ve been trying various different things to try and resolve this for like 3 days now!

I’ve been using just tabs and not spaces this entire time (I thought tabs was the proper way to do this!)

Really appreciate your help, thank you for taking the time!!

If you are using the new template domain then you should be using the ‘modern’ format for defining sensors. Your example starts by declaring template but then employs the ‘legacy’ format, not the ‘modern’ format.


EDIT

I’m surprised that works because the documentation clearly shows the ‘legacy’ format is for use with sensor and binary_sensor domains, not template.

1 Like

Thank you for taking the time to help!!! I really appreciate it!

Looks like @finity 's tip fixed my issue here

Thanks again for taking the time!

Yeah, I’m going to try and switch it to modern now and see if it still works… basically had it in modern before and then I started trying combinations of things to see if it would fix my issue.

Thanks for the heads up!

Adopting ‘modern’ format will force you to make a decision about the sensor’s name. The ‘modern’ format uses name to create the entity’s object_id and its friendly_name.

Currently, your sensor’s object_id is:

kitchen_smoke_co_alarm

and its friendly_name is:

Kitchen Smoke & CO Detector Status

They’re not interchangeable; the modern format will convert this:

Kitchen Smoke & CO Detector Status

to an object_id that looks something like this:

kitchen_smoke___co_detector_status

If you want to preserve the sensor’s existing object_id, you will have to change its name to:

Kitchen Smoke CO Alarm

EXAMPLE

template:
  - sensor:
      - name: 'Kitchen Smoke CO Alarm'
        state: >-
          {% set l = states('sensor.kitchen_smokeco_alarm_alarmlevel') | int %}
          {% set t = states('sensor.kitchen_smokeco_alarm_alarmtype') | int %}
          {% if l == 255 and t == 1 %} SMOKE DETECTED!!!!
          {% elif l == 255 and t == 2 %} CARBON MONOXIDE ALARM!!!
          {% elif l == 0 and t == 12 %} Test!!! XXX Test!!!
          {% elif l == 255 t == 13 %} Safe - Normal Operation
          {% else %} UNKNOWN
          {% endif %}
        icon: >-
          {% set l = states('sensor.kitchen_smokeco_alarm_alarmlevel') | int %}
          {% set t = states('sensor.kitchen_smokeco_alarm_alarmtype') | int %}
          {% if l == 255 and t == 1 %} mdi:fire
          {% elif l == 255 and t == 2 %} mdi:cloud-outline
          {% elif l == 0 and t == 12 %} mdi:alert-octagram
          {% elif l == 255 t == 13 %} mdi:smoke-detector
          {% else %} mdi:smoke-detector
          {% endif %}
1 Like

And then you can customize the friendly name using the “customize:” section.

It’s a silly hacky work around but it should work. …I think…I haven’t tried it yet…but it should work…

1 Like

I am curious to learn the result of your test. In addition, I wonder what it does if name is a template and customize: is employed.

Sorry, I should have been more clear. I’ll edit my post.

I said that I haven’t tried it yet.

I’m not even on the latest version yet. The dust hasn’t settled enough to be confident in trying it ouit.

The longer I see the new format, the less “modern” it gets. Something tells me, this “feature” was added before it was thought through…