Tips on writing and debugging template sensors


I’ve been busy writing template sensors as they are a good solution to gather information from various other sensors and expose a state (and additional attributes) consistently.
However, writing a proper template can be hard sometimes.
Usually, I have to check the config, reload the templates, trigger some events to see if my template sensor works properly.
At each check a problem can arise (even though the previous check was fine).
And when this go wrong, I only seem to have the system log.
Currently I view that from the UI, but I think this is not the place to debug your sensor as this logging is processed and presented in such a way that debugging is hard and the logging may be old.
Is there another way to debug this?

Test your templates here first: Developer Tools → Templates

Hi Tom,
Thanx, I forgot that one.
It does help to check your syntax, but it’s not entirely the same execution environment.
For instance, I had some script that worked well there but not in an actual template.
Especially checking whether something is none, defined, ‘’ is different and the parsing of attributes of a sensor can bring some suprises as well.
Thnx nevertheless for this important step to build your logic.

You can’t test scripts there, only templates.

See for instance an issue I’m having now, where I don’t know how to get information from home assistant to understand what is wrong:

I did an import of my macro in the action / variables section.
All seemed fine until the first event fired and the template sensor died.
The log in home assistant stated:

Logger: homeassistant.helpers.sensor
Bron: helpers/
First occurred: 21:01:47 (12 gebeurtenissen)
Laatst gelogd: 22:15:40

    Error rendering state template for sensor.woonkamer_magic_cube: TemplateSyntaxError: Encountered unknown tag 'is_cube_event'. Jinja was looking for the following tags: 'endmacro'. The innermost block that needs to be closed is 'macro'.
    Error rendering state template for sensor.woonkamer_magic_cube: UndefinedError: 'cube' is undefined
    Error rendering state template for sensor.woonkamer_magic_cube: TypeError: unhashable type: 'list'
    Error rendering state template for sensor.woonkamer_magic_cube: UndefinedError: 'cube_face' is undefined

Note that most of the error lines in de log are old (I know this due to history of what I’ve done, the log entries itself as shown lack a timestamp which is definitely not helping)

sorry, by script I meant some lines of jinja code.


I’ve used the Template Editor extensively for over 5 years and it has been a reliable and indispensable tool for developing and testing templates (whether for use in sensors or triggers or conditions, etc).

The first error message suggests the macro was malformed. Difficult to pinpoint the problem without seeing the actual state template for sensor.woonkamer_magic_cube.

My other post linked here shows both.
The first error message however was really old (but gets shown anyway for no clear reason).
The real problem was the last error message, which has let me to believe that macro’s can not be imported within a variables declaration under a state based trigger action entry.
Not sure about this though because I could not find documentation that confirms this.
So the link I posted is a question if anybody can confirm this and where you can/can’t put a yaml variables key (been struggling with that as well).

It’s defined in the Scripts documentation.

Post an example that produced an error and I’ll explain why.


Never mind. It seems that you are cross posting and are already discussing the same issue in another topic.