@juan11perez @robmarkcole So I finally got it figured out. The problem in all these different approaches was wrapping the trigger.event.data.folder variable in double curly brackets, like this:
{{ trigger.event.data.folder }}
Wrapping the variable name in brackets is necessary when including the contents of the variable as part of the message to any of the notify integration platforms ( in my case, discord) , but it is not required when referencing the variable as part of the template logic.
From the beginning, the goal of all my automation has been to send a differently formatted discord message depending on which folder folder_watcher detects a file in. I attempted to do this with some if/else logic on the string contents of the trigger.event.data.folder variable provided by folder_watcher, exemplified in the following line:
{% if “/media/movies” in {{ trigger.event.data.folder }} %}
I also tried including this check as a condition to the automation running, like this:
condition:
condition: template
value_template: "{{ if '/media/movies' in {{ trigger.event.data.folder }} }}"
In both cases, I was met with an error similar to this one:
Invalid config for [automation]: invalid template (TemplateSyntaxError: expected token ‘:’, got ‘}’)
It was reading this post from @albert1 that finally made it start to click for me. I’m not by any means an expert on Home Assistants architecture, but I believe the reason for this is that the double brackets around trigger.event.data.folder are only needed in the message contents portion of the code because this is the only portion where Home Assistant is not already rendering the template. In the messages content, HA needs to be explicitly told to get the contents of the variable from the template, thus the double brackets, but, in the template logic, the {% %} around the whole statement have already got Home Assistant rendering a template, and thus it recognizes the trigger.event.data.folder variable without having to flag it specifically.
The same should hold true for preforming the check in the conditions, as Rob recommended. This will not work:
condition:
condition: template
value_template: "{{ if '/media/movies' in {{ trigger.event.data.folder }} }}"
but this should:
condition:
condition: template
value_template: "{{ '/media/movies' in trigger.event.data.folder }}"
(Notice above that, in addition to dropping the double curly brackets around trigger.event.data.folder, I also dropped the “if” check entirely, leaving just in. That is because condition templates only check for whether or not the given condition renders true- basically, the logic of “if this is true, continue, otherwise dont do anything” is already taken care of for us.)
TL;DR: Only wrap the trigger.event.data.folder variable in double curly brackets when including it as part of the message output. The rest of the time, HA is already rendering the template and knows what the variable is without the {{ }} flag. My complete, and working (!!) automation is as follows:
- alias: 'Send notification to Discord when new media is added to upload directory'
trigger:
platform: event
event_type: folder_watcher
event_data:
event_type: created
action:
service: notify.discord
data_template:
message: >
{% if "/uploads/MKV" in trigger.event.data.folder %}
Movie Added file {{ trigger.event.data.file }} folder {{ trigger.event.data.folder }}
{% elif "/uploads/TV_Seasons" in trigger.event.data.folder %}
TV Episode Added file {{ trigger.event.data.file }} folder {{ trigger.event.data.folder }}
{% else %}
Error, folder path not configured for notification
{% endif %}
target: !secret discord_channel_id
I’m still considering whether or not to split this into multiple automatons using automation conditions, as @robmarkcole recommended, but right now I’m leaning towards leaving the logic in the template, as this automation serves one purpose, I just want it to send slightly different messages depending on the folder in which the file is detected.
Being able to reliably preform different actions depending on the directory where a folder watcher event is detected greatly increases the utility of folder watcher for me- Hope this can help someone else