petro
(Petro)
June 8, 2023, 1:08pm
21
So to recap, this logic is not correct:
sjofel:
- alias: When a device has been turned on manually
platform: template
value_template: >
{% set namespace = namespace(manually_triggered=false) %}
{% set devices = area_entities(area_name) |
expand |
selectattr("domain", "eq", device_domain_name) |
list
%}
{%- for device in devices %}
{% set device_context = device.context %}
{% set changed_recently = (as_timestamp(now(), 0) - as_timestamp(device.last_changed, 0)) | int(0) < 1 %}
{% set device_changed_by_user = this.context.user_id != None %}
{% set device_changed_outside_ha = this.context.user_id == None and this.context.parent_id == None %}
{% if (device_changed_by_user or device_changed_outside_ha) and changed_recently %}
{% set namespace.manually_triggered = true %}
{% break %}
{% endif %}
{% endfor %}
{{ namespace.manually_triggered }}
It’s a chicken-egg scenario. You’re not accessing the context you think you’re accessing.
sjofel
June 8, 2023, 1:10pm
22
Yea i know. The this doesn’t make sense. Forget that template. It also doesn’t work with this trigger:
platform: state
entity_id:
- light.living_room_standing
to: null
or this one for that matter:
platform: state
entity_id:
- light.living_room_standing
If the light state is changed by the time trigger (in another automation) context.parent_id stays null although the light has been changed by the automation.
petro
(Petro)
June 8, 2023, 1:28pm
23
Yes, I see that too. That would be a bug.
sjofel
June 8, 2023, 1:31pm
24
Yeah, i’ve linked a related github issue earlier. Currently this is still a problem.
Thanks for the help nonetheless.
I’ve found some other related issues:
Apparently it has something to do with time triggers not registering as an event in the database. This way home assistant can’t apply context.
1 Like