The problem
The variable trigger is undefined when evaluating a data_template for a service action in an automation.
This used to work in 0.112.8 and prior. Documentation doesn't seem to indicate any change to this behavior.
Environment
- Home Assistant Core release with the issue: 0.113.2
- Last working Home Assistant Core release (if known): 0.112.8
- Operating environment (OS/Container/Supervised/Core): HassOS 4.11 aarch64
- Integration causing this issue: automation
- Link to integration documentation on our website: https://www.home-assistant.io/docs/automation/templating/
Problem-relevant configuration.yaml
Here are each of the relevant foo.yaml files, as currently on disk.
configuration.yaml
homeassistant:
# Load packages
packages: !include_dir_named packages
...
packages/integrations/input_select.yaml
input_select: !include_dir_merge_named ../../entities/input_selects
packages/integrations/automation.yaml
---
# This handles the loading of my automations
#
# https://www.home-assistant.io/docs/automation/
#
# The first include is for front-end support
automation: !include ../../automations.yaml
# The split include is for manually defined automations
automation split: !include_dir_list ../../automations
packages/integrations/sensor.yaml
sensor: !include_dir_list ../../entities/sensors
entities/sensors/indigo_house_mode.yaml
platform: mqtt
state_topic: "indigo/variables/1388442839/update"
json_attributes_topic: "indigo/variables/1388442839/update"
name: "Indigo House Mode"
value_template: "{{ value_json.value }}"
input_selects/house_mode.yaml
---
# House mode can be selected manually, or updated by device_trackers and motion
# sensors.
#
house_mode:
name: House Mode
options:
- Home
- Away
- Sleep
- Extended Away
- Unknown
icon:
mdi:home-map-marker
automations/house_mode/sync_house_mode_to_indigo.yaml
---
# Synchronize Indigo and Home Assistant's concept of house mode
alias: Synchronize HA house mode to Indigo
description: Update HA's house mode when the Indigo MQTT sensor changes state.
mode: restart
##### STATE PLATFORM CAN'T FIND trigger - STUFF BELOW IS BROKEN #####
#trigger:
# platform: state
# entity_id: sensor.indigo_house_mode
#action:
# service: input_select.select_option
# data_template:
# entity_id: input_select.house_mode
# # map indigo variable values to HA input select options
# # default to "Home" if value doesn't match
# option: >
# {% if trigger.to_state.state == 'home' %}
# Home
# {% elif trigger.to_state.state == 'away' %}
# Away
# {% elif trigger.to_state.state == 'sleep' %}
# Sleep
# {% elif trigger.to_state.state == 'vacation' %}
# Extended Away
# {% else %}
# Unknown
# {% endif %}
##### READING THE MQTT STATE DIRECTLY WORKS #####
trigger:
platform: mqtt
topic: "indigo/variables/1388442839/update"
action:
service: input_select.select_option
data_template:
entity_id: input_select.house_mode
option: >
{% if trigger.payload_json.value == 'home' %}
Home
{% elif trigger.payload_json.value == 'away' %}
Away
{% elif trigger.payload_json.value == 'sleep' %}
Sleep
{% elif trigger.payload_json.value == 'vacation' %}
Extended Away
{% else %}
Unknown
{% endif %}
Traceback/Error logs
Synchronize HA house mode to Indigo: Error executing script. Unexpected error for call_service at pos 1: Error rendering data template: UndefinedError: 'trigger' is undefined
While executing automation automation.synchronize_ha_house_mode_to_indigo
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 229, in async_render
return compiled.render(kwargs).strip()
File "/usr/local/lib/python3.8/site-packages/jinja2/environment.py", line 1090, in render
self.environment.handle_exception()
File "/usr/local/lib/python3.8/site-packages/jinja2/environment.py", line 832, in handle_exception
reraise(*rewrite_traceback_stack(source=source))
File "/usr/local/lib/python3.8/site-packages/jinja2/_compat.py", line 28, in reraise
raise value.with_traceback(tb)
File "<template>", line 1, in top-level template code
File "/usr/local/lib/python3.8/site-packages/jinja2/sandbox.py", line 407, in getattr
value = getattr(obj, attribute)
jinja2.exceptions.UndefinedError: 'trigger' is undefined
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 132, in async_prepare_call_from_config
template.render_complex(config[CONF_SERVICE_DATA_TEMPLATE], variables)
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 73, in render_complex
return {key: render_complex(item, variables) for key, item in value.items()}
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 73, in <dictcomp>
return {key: render_complex(item, variables) for key, item in value.items()}
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 75, in render_complex
return value.async_render(variables)
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 231, in async_render
raise TemplateError(err)
homeassistant.exceptions.TemplateError: UndefinedError: 'trigger' is undefined
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 193, in _async_step
await getattr(
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 354, in _async_call_service_step
domain, service, service_data = async_prepare_call_from_config(
File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 135, in async_prepare_call_from_config
raise HomeAssistantError(f"Error rendering data template: {ex}") from ex
homeassistant.exceptions.HomeAssistantError: Error rendering data template: UndefinedError: 'trigger' is undefined
Additional information
The MQTT sensor sensor.indigo_house_mode is updating correctly when the MQTT topic updates.
On the automation, changing the trigger type to MQTT and reading the MQTT message directly works fine - I get no error about an undefined "trigger" variable.
automation:
# Synchronize Indigo and Home Assistant's concept of house mode
- alias: Synchronize HA house mode to Indigo
description: Update HA's house mode when the Indigo MQTT sensor changes state.
mode: restart
trigger:
platform: mqtt
topic: "indigo/variables/1388442839/update"
action:
service: input_select.select_option
data_template:
entity_id: input_select.house_mode
option: >
{% if trigger.payload_json.value == 'home' %}
Home
{% elif trigger.payload_json.value == 'away' %}
Away
{% elif trigger.payload_json.value == 'sleep' %}
Sleep
{% elif trigger.payload_json.value == 'vacation' %}
Extended Away
{% else %}
Unknown
{% endif %}
The problem
The variable
triggeris undefined when evaluating adata_templatefor aserviceaction in anautomation.This used to work in 0.112.8 and prior. Documentation doesn't seem to indicate any change to this behavior.
Environment
Problem-relevant
configuration.yamlHere are each of the relevant foo.yaml files, as currently on disk.
configuration.yaml
packages/integrations/input_select.yaml
packages/integrations/automation.yaml
packages/integrations/sensor.yaml
entities/sensors/indigo_house_mode.yaml
input_selects/house_mode.yaml
automations/house_mode/sync_house_mode_to_indigo.yaml
Traceback/Error logs
Synchronize HA house mode to Indigo: Error executing script. Unexpected error for call_service at pos 1: Error rendering data template: UndefinedError: 'trigger' is undefined While executing automation automation.synchronize_ha_house_mode_to_indigo Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 229, in async_render return compiled.render(kwargs).strip() File "/usr/local/lib/python3.8/site-packages/jinja2/environment.py", line 1090, in render self.environment.handle_exception() File "/usr/local/lib/python3.8/site-packages/jinja2/environment.py", line 832, in handle_exception reraise(*rewrite_traceback_stack(source=source)) File "/usr/local/lib/python3.8/site-packages/jinja2/_compat.py", line 28, in reraise raise value.with_traceback(tb) File "<template>", line 1, in top-level template code File "/usr/local/lib/python3.8/site-packages/jinja2/sandbox.py", line 407, in getattr value = getattr(obj, attribute) jinja2.exceptions.UndefinedError: 'trigger' is undefined During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 132, in async_prepare_call_from_config template.render_complex(config[CONF_SERVICE_DATA_TEMPLATE], variables) File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 73, in render_complex return {key: render_complex(item, variables) for key, item in value.items()} File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 73, in <dictcomp> return {key: render_complex(item, variables) for key, item in value.items()} File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 75, in render_complex return value.async_render(variables) File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 231, in async_render raise TemplateError(err) homeassistant.exceptions.TemplateError: UndefinedError: 'trigger' is undefined The above exception was the direct cause of the following exception: Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 193, in _async_step await getattr( File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 354, in _async_call_service_step domain, service, service_data = async_prepare_call_from_config( File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 135, in async_prepare_call_from_config raise HomeAssistantError(f"Error rendering data template: {ex}") from ex homeassistant.exceptions.HomeAssistantError: Error rendering data template: UndefinedError: 'trigger' is undefinedAdditional information
The MQTT sensor
sensor.indigo_house_modeis updating correctly when the MQTT topic updates.On the
automation, changing thetriggertype to MQTT and reading the MQTT message directly works fine - I get no error about an undefined "trigger" variable.