Closed LeChuck2909 closed 3 years ago
My guess would be, that current
is a numeric (integer or float) value in this case, not a string (text) value. Your from
/to
are strings in your example.
trigger:
- platform: state
entity_id: automation.dimm_licht_schlafzimmer_fade
attribute: current
from: 1
to: 0
My guess would be, that
current
is a numeric (integer or float) value in this case, not a string (text) value. Yourfrom
/to
are strings in your example.trigger: - platform: state entity_id: automation.dimm_licht_schlafzimmer_fade attribute: current from: 1 to: 0
When edit to your recommendation, leave the automation page in frontend and re-enter it, the code is like:
platform: state
entity_id: automation.dimm_licht_schlafzimmer_fade
attribute: current
from: 1
to: '0'
the secont value have been changed to an string again. also all automations restart, when i apply changes to this automation.
That sounds like a frontend issue, not a core issue.
ok, but, i also changed the automaiton to your recommendation in file editor now and the triggiering still doesnt work.
ok categorizing 👍
automation documentation automation source (message by IssueLinks)
Hey there @home-assistant/core, mind taking a look at this issue as its been labeled with an integration (automation
) you are listed as a codeowner for? Thanks!
(message by CodeOwnersMention)
Version | 2021.1.4
-- | --
Installation Type | Home Assistant OS
Development | false
Supervisor | true
Docker | true
Virtual Environment | false
Python Version | 3.8.7
Operating System Family | Linux
Operating System Version | 5.4.79-v7
CPU Architecture | armv7l
Timezone | Europe/Oslo
I have the same issue and getting error: “Error executing script. Unexpected error for call_service at pos 1: Error rendering data template: UndefinedError: ‘trigger’ is undefine”
From the documentation I get the understanding that triggering using attribute behaves like triggering using state.
From the GUI -YAML editor:
alias: OpenTX RC Frimware Update MSG
description: OpenTX and Multiprotocol update Message
trigger:
- platform: state
entity_id: sensor.github_opentx
attribute: latest_release_tag
condition: []
action:
- service: notify.me
data:
title: HA - Information
message: >-
"{{trigger.from_state.attributes.friendly_name}}:
{{trigger.from_state.attributes.latest_release_tag}}->{{trigger.to_state.attributes.latest_release_tag}}"
mode: single
From the log:
2021-01-22 13:12:15 ERROR (MainThread) [homeassistant.components.automation.opentx_rc_fw_update_msg] OpenTX RC Frimware Update MSG: Error executing script. Unexpected error for call_service at pos 1: Error rendering data template: UndefinedError: 'trigger' is undefined
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 353, in async_render
render_result = compiled.render(kwargs)
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
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 188, in async_prepare_call_from_config
service_data.update(template.render_complex(config[conf], variables))
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 93, in render_complex
return {
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 94, in <dictcomp>
render_complex(key, variables): render_complex(item, variables)
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 98, in render_complex
return value.async_render(variables)
File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 355, in async_render
raise TemplateError(err) from 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 250, in _async_step
await getattr(
File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 429, in _async_call_service_step
domain, service_name, service_data = service.async_prepare_call_from_config(
File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 190, 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
UPDATE 27/1: Aftere latest OS update looks like it works fine.
I have the same issue. It took long until I narrowed down the cause as I have multiple conditions in my automation and the state attribute is one of them.
I think this is core issue, because when I use numeric_state
condition instead - it works fine - I just increase the value to 1, but in YAML both look the samem wrapped in '
marks.
The below test automation will never fire:
alias: 0 test trigger condition
description: ''
trigger:
- platform: time_pattern
seconds: /01
condition:
- condition: state
entity_id: automation.piec_harmonogram_dogrzewanie_w_nocy
attribute: current
state: '0'
action:
- service: system_log.write
data:
message: Test trigger
mode: single
Would be great if someone could take a look at this as this is a core issue in one of the most important components. Good luck!
I debugged that part and I think I've found the problem. State values are strings, but state attributes may also have other types like int
. The problem is that homeassistant.helpers.condition.py/state
function does comparison of condition value, which is always str
with current state attribute, which is int
. This always returns False
, so the condition will never be met.
In my opinion it is enough, to convert current attribute value always to str
before the comparison and this solves the problem.
Something like that (see last line):
def state(
hass: HomeAssistant,
entity: Union[None, str, State],
req_state: Any,
for_period: Optional[timedelta] = None,
attribute: Optional[str] = None,
) -> bool:
"""Test if state matches requirements.
Async friendly.
"""
if isinstance(entity, str):
entity = hass.states.get(entity)
if entity is None or (attribute is not None and attribute not in entity.attributes):
return False
assert isinstance(entity, State)
if attribute is None:
value: Any = entity.state
else:
value = str(entity.attributes.get(attribute))
I did it locally and it fixed the problem. But I'm not HA Core dev, so I may be wrong and it may be more complicated to cover all cases as I don't see the full picture.
What do you think @frenck ??
EDIT: I've just noticed that the OP's problem is related to trigger, while my is related to condition, but it's probably the same root cause - comparison of string and int between trigger/condition value and state attribute value.
@LeChuck2909 I believe this must be what Frenck mentioned. Have you reloaded your automations after editing them in file editor?
I had a similar issue with conditions (issue #45601) when using the current
attribute, but I also tried it with trigger and it only didn't work when the values in YAML were strings (converted by GUI/frontend editor). When I changed them to int as they should be - it worked. The trigger was fired.
I also debugged this part of HA Core today and found that it won't work when invalid data type is used in YAML, but when it is - no reason for it not to trigger. Check again your YAML, it may have got broken again by tje frontend editor (values got back to string), reload automations in HA GUI and don't touch them with the GUI automation editor - then I bet it will trigger.
@dgtal1 I think you are totally right, just had the same issue with my Wled preset
numeric attribute as a trigger. As you suggested the temporary workaround is to edit the automation configuration in the editor, not from the frontend, and then reload the automations.
Your code seems to make sense and I would suggest opening a PR if you can
There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates. Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍 This issue has now been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.
The problem
The automation with the alias "Dimm Licht Schlafzimmer Fade Notification" should be triggered if when the current instance of the automation with the alias "Dimm Licht Schlafzimmer Fade" finished processig. In other words when the "Current"-attribute of "Dimm Licht Schlafzimmer Fade" changes from 1 to 0 again, but "Dimm Licht Schlafzimmer Fade Notification" doe not get triggered. I watch "Dimm Licht Schlafzimmer Fade" in the Developer Section as the "Current"-Attribute is 1 while processing, and 0 when done, but no triggering of "Dimm Licht Schlafzimmer Fade Notification".
Environment
Problem-relevant
configuration.yaml
Traceback/Error logs
Additional information