home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
73.64k stars 30.79k forks source link

Blueprint For Template Entities - Triggers doesn't work #130330

Open panhans opened 2 days ago

panhans commented 2 days ago

The problem

When setting up a blueprint for template entities triggers won't work and ends in a runtime error.

What version of Home Assistant Core has the issue?

2023.11.1

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant OS

Integration causing the issue

No response

Link to integration documentation on our website

https://www.home-assistant.io/integrations/template/#using-blueprints

Diagnostics information

No response

Example YAML snippet

blueprint:
  name: Invert a binary sensor
  description: Creates a binary_sensor which holds the inverted value of a reference binary_sensor
  domain: template
  source_url: https://github.com/home-assistant/core/blob/dev/homeassistant/components/template/blueprints/inverted_binary_sensor.yaml
  input:
    reference_entity:
      name: Binary sensor to be inverted
      description: The binary_sensor which needs to have its value inverted
      selector:
        entity:
variables:
  reference_entity: !input reference_entity
trigger:
  - trigger: event
    event_type: MY_CUSTOM_EVENT
    event_data:
      automation: !input reference_entity
binary_sensor:
  availability: "{{ states(reference_entity) not in ('unknown', 'unavailable') }}"
  state: "{{ trigger.event.data.state }}"

Anything in the logs that might be useful for us?

2024-11-11 10:03:20.995 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'reference_entity' is undefined when rendering '{{ states(reference_entity) not in ('unknown', 'unavailable') }}'
2024-11-11 10:03:20.996 ERROR (MainThread) [homeassistant.helpers.binary_sensor] Error rendering availability template for binary_sensor.ahc_test_room_data2: UndefinedError: 'reference_entity' is undefined

Additional information

No response

home-assistant[bot] commented 2 days ago

Hey there @phracturedblue, @tetienne, @home-assistant/core, mind taking a look at this issue as it has been labeled with an integration (template) you are listed as a code owner for? Thanks!

Code owner commands Code owners of `template` can trigger bot actions by commenting: - `@home-assistant close` Closes the issue. - `@home-assistant rename Awesome new title` Renames the issue. - `@home-assistant reopen` Reopen the issue. - `@home-assistant unassign template` Removes the current integration label and assignees on the issue, add the integration domain after the command. - `@home-assistant add-label needs-more-information` Add a label (needs-more-information, problem in dependency, problem in custom component) to the issue. - `@home-assistant remove-label needs-more-information` Remove a label (needs-more-information, problem in dependency, problem in custom component) on the issue.

(message by CodeOwnersMention)


template documentation template source (message by IssueLinks)

panhans commented 2 days ago

@tetele implemented this regarding the change log.

tetele commented 2 days ago

What you're showing is expected behavior with the current implementation. I see several issues in your code:

  1. You're using !input in a trigger, which won't work
  2. Even if you were using trigger_variables (as with automations), that is not yet implemented in blueprints for template triggers.
  3. Even if it were implemented, i fail to see why {{ trigger.event.data.state }} would work
panhans commented 2 days ago

Thanks for your fast respond.

  1. Ok, this won't work? For automations and script it does.
  2. So the conclusion ist that triggers aren't supported yet at all for this kind of blueprint?
  3. My template trigger is listening for a custom event where the state is existent. Maybe a had better chosen a default event for the example.

A last question: Are triggers on the roadmap for this features or is it not planned for now?

tetele commented 2 days ago
  1. No, it won't. There are no trigger_variables for template yet. There may be soon, but not in the current implementation.
  2. Triggers are supported, but trigger_variables aren't. You can have a static trigger in your blueprint, which does not use any inputs and you're fine.
panhans commented 2 days ago

Ok, this is what I've tried to filter the events by entity_id with a default ha event. But it results in the same error:

blueprint:
  name: Trigger
  description: ""
  domain: template
  input:
    reference_entity:
      name: Entity
      description: ""
      selector:
        entity:
variables:
  reference_entity: !input reference_entity
trigger:
  - trigger: event
    event_type: state_changed
binary_sensor:
  availability: "{{ iif(trigger.event.data.entity_id != reference_entity, this.attributes.availability, states(reference_entity) not in ('unknown', 'unavailable')) }}"
  state: "{{ iif(trigger.event.data.entity_id != reference_entity, this.attributes.state, trigger.event.data.state) }}"

Or did you mean with trigger_variables not the trigger variables section but e.g. this: trigger.event.data.entity_id

tetele commented 2 days ago

I can replicate your issue. Intriguing behavior, I'll have to look into it a bit more until I can provide an answer.