How to troubleshoot template issue?

I am working on some calendar automations with my weather entities to forecast upcoming trips. The template code works perfectly in the “Developer Tools” → Template with data supplied for the calendar entries.

For others struggling, I used “Developer Tools” → Services to get my calendar entries like this:

service: calendar.get_events
target:
  entity_id: calendar.andolinos
data:
  duration:
    hours: 576

Then converted the Response using Transform YAML into JSON - Online YAML Tools

And went back to the “Developer Tools” → Template and copied to json data into the ‘xxx’ below.

{%- set scheduled_events = xxx -%}

My template file (I have been using a split configuration for months now) is as follows

- trigger:
    - platform: time_pattern
      minutes: /30
      # hours: /24
    - platform: homeassistant
      event: start
    - platform: event
      event_type: event_template_reloaded
  action:
    - service: calendar.get_events
      target:
        entity_id: calendar.andolinos
      data:
        duration:
          hours: 576
      response_variable: scheduled_events
  sensor:
    - unique_id: andolino_events
      attributes:
        events: >
          {{ scheduled_events['calendar.andolinos'].events }}

So, the issue is when I check the template (“Developer Tools” → YAML and “Check Configuration”), I get a Notification:

Invalid config
The following integrations and platforms could not be set up:

template (Show logs)
Please check your config and logs.

I checked my logfile (home-assistant.log) and Settings → System → Logs for the “Home Assistant Core”, “Supervisor”, and “Host”, but I cannot find any error messages. I also searched the forums, but have not found any solutions or potential solutions.

I even tried to add debug for templates in the configuration.log

logger:
  default: critical
  logs:
    # critical, fatal, error, warning, warn, info, debug, notset
    # log level for HA core
    homeassistant.core: fatal
    homeassistant.components.system_log.external: info
    custom_components.pyscript: info
    homeassistant.components.http.forwarded: debug
    template: debug

So, how do I go about troubleshooting the source of the configuration error (and therefore my sensor is NOT created)? I am running Home Assistant in a VM with Addons and HACs, in case that is information someone needs to help me.

Thanks

nothing in your posted template jumps out at me as erroneous, however i may be missing something that someone else can pick up. or it’s also possible that as you were doing this, you accidentally hit a character somewhere else in the code and didn’t realize it.

so to answer this question:

when all else fails, consider binary search… backup everything. (in addition to doing a ha backup, i’d also copy all the yaml config files to my laptop). then delete the template you’re working on. see if it passes. if it doesn’t then you did indeed fat finger somewhere else. delete half of the remaining config and test… repeat till you find the issue.

make sure you don’t muck with anything you don’t have a backup of…
make sure you have samba installed as well… whenever you’re mucking around in the conifg and may screw things up so that the ui doesn’t boot, have an alternative way to get to the config files.

the beauty of binary search is that it’s fast… you’ll find the issue rather quickly…

you need state:, it can be anything. state: OK

@petro - yes, I fixed it with the state yesterday (that was the solution for this issue).

However, my outstanding generic question is how do you find the template issues that “Developer Tools” → YAML and “Check Configuration” finds?

Whenever I get those messages, there is nothing new in the home-assistant.log or any of the System log areas. Do I have to look inside the VM host or the HA docker container on the VM host?

Is there a way to set a flag within the configuration.yaml logger section or somewhere else?

I would just like to know where to look for any issue in the future.

The template editor is for templates, jinja templates. It’s not for template entities. A template entity is a “virtual” entity that uses jinja templates. FYI Jinja is everything between {{ }}, {% %}, {# #}. The rest of your configuration is yaml and unrelated to templating.

And to further clarify, you were encountering a YAML configuration issue, not a templating issue.

Yes there is, you must have missed it. It would be in homeassistant.log. You can view the raw log file using SSH, Samba Share, or download the full log file.

The error in your case would have been something like missing required field [state], it should have shown up when you clicked “check configuration” on the developer tools → yaml page.

@petro - sorry, maybe I was not clear (not enough coffee for me yet). I have a coding background and understand what the template editor vs my templates within the VS Code addon.

The issues in the past where from the template configurations within VS Code on my HA instance. I tested the jinja portion of the template in the “Developer Tools” → Template successfully before adding/changing the code in VS Code.

The VS Code (and Prettier extension) did not report any issues with the structure of the template code, but the “Developer Tools” → YAML and “Check Configuration” did. In troubleshooting, I never find an error message within the configuration.yaml or System logs.

I would just like to know how to troubleshoot and where to debug or look in the future when I have template issues.

The vscode addon is a 3rd party addon. The errors it produces (or not) should not be trusted. HA will produce the correct errors.

Okay, but ignoring VS Code, where do I look in the future to troubleshoot? There is nothing in the homeassistant.log OR the System Logs, which are separate from the VS Code tool. There is always just the generic message in the “Developer Tools” → YAML → “Check Configuration”. My point is that HA is producing an error BEFORE I reboot it or reload the YAML configurations, but I can never find the log message.

I have tried to click on the two hyperlinks that “Developer Tools” → YAML → “Check Configuration” pops up when there is a potential YAML issue and nothing ever shows up there. I have logged into the VM host and the HA docker container within, tried ha core check and many other things.

I have had this happen several times and spent lots of time checking my YAML syntax, searching the forums, Discord, the web etc. There are no issues right now - I am just looking for the right “location” to look when I have an issue in the future.

I replicated your template, ran check config, then checked the logs and had this error:

2024-04-15 07:56:12.765 ERROR (MainThread) [homeassistant.config] Invalid config for 'template' at template/time.yaml, line 10: required key 'state' not provided

Okay, so I just triggered another error (working on trying to template trigger a REST API based on an upcoming calendar event).

Here is the screenshot of the “Developer Tools” → YAML → “Check Configuration” notification.

My HA log WITHOUT any information (I used VS Code to see the file contents):

System log WITHOUT any information:

So, where do I look for details on this error?

Are you suppressing log errors and warnings from the template integration? It will show up in your logs. I’m not mincing my words here, I’ve been using the system for 8 years now. Errors show up in your logs, always. Especially if HA points you to the logs. The only way for them to not show up is if you’ve overridden logging in configuration.yaml or via an automation.

1 Like

Good question. I am not overriding logging in any automations. Here is the logging section of my configuration.yaml:

logger:
  default: critical
  logs:
    homeassistant.core: fatal
    homeassistant.components.system_log.external: info
    custom_components.pyscript: info
    homeassistant.components.http.forwarded: debug

Do any of these suppress the logs? Should I add anything to the logging?

yes

default: critical

will only show exceptions. That will suppress errors and warnings for all integrations.

1 Like

Form Logger’s documentation:

Okay, so which level should it be to catch these such issues? I know from the documentation that the levels from most severe to least are:

critical, fatal, error, warning, warn, info, debug, notset

Which one should I use?

I’d personally set it to warning or error.