I have 2 template sensors:
– both have same structure;
– one of them is based on a blueprint
and they behave differently.
There are 2 input_number
helpers:
input_number:
testing_number: &ref_input_number
min: -1000
max: 1000
step: 1
mode: slider
testing_number_2: *ref_input_number
A template sensor:
template:
- sensor:
- name: testing_source
unit_of_measurement: uom
state: >-
{{ states('input_number.testing_number') }}
attributes:
some_attr: >-
{{ states('input_number.testing_number_2') }}
Now let’s create a trigger-based template sensor:
- trigger:
- platform: state
entity_id: sensor.testing_source
attribute: some_attr
sensor:
- name: testing_blueprint_attr_copy
state: >-
{% if trigger.to_state is not none -%}
{%- if trigger.to_state.attributes['some_attr'] is defined -%}
{{ trigger.to_state.attributes['some_attr'] }}
{%- else -%}
xxx
{%- endif -%}
{%- else -%}
yyy
{%- endif %}
The sensor uses the “sensor.testing_source” as a data source.
Let’s put a simple logic:
– if source attribute is defined - use it;
– otherwise use something else like “xxx” or “yyy”.
I intentionally did not use “condition” part here.
Next, a blueprint - which is supposed to do the same:
blueprint:
name: xxx
domain: template
input:
input_SOURCE_ENTITY:
input_SOURCE_ATTR:
variables:
SOURCE_ENTITY: !input input_SOURCE_ENTITY
SOURCE_ATTR: !input input_SOURCE_ATTR
trigger:
- platform: state
entity_id: !input input_SOURCE_ENTITY
attribute: !input input_SOURCE_ATTR
sensor:
state: >-
{% if trigger.to_state is not none -%}
{%- if trigger.to_state.attributes[SOURCE_ATTR] is defined -%}
{{ trigger.to_state.attributes[SOURCE_ATTR] }}
{%- else -%}
xxx
{%- endif -%}
{%- else -%}
yyy
{%- endif %}
Here there is same logic for the “state”.
And create a sensor based on this blueprint:
- name: testing_blueprint_attr
use_blueprint:
path: xxx.yaml
input:
input_SOURCE_ENTITY: sensor.testing_source
input_SOURCE_ATTR: some_attr
Reboot HA (needed due to this issue).
Start playing with “input_number” helpers (although only “testing_number_2” is supposed to influence).
Mysteriously the “traditional” sensor “testing_blueprint_attr_copy” works (= “testing_number_2”), and the blueprint-based “testing_blueprint_attr” is always “xxx”:
Seems that in the blueprint these vars are not set properly:
variables:
SOURCE_ENTITY: !input input_SOURCE_ENTITY
SOURCE_ATTR: !input input_SOURCE_ATTR
How to check: add this action into the blueprint:
action:
- action: notify.persistent_notification
data:
message: |-
SOURCE_ENTITY: {{SOURCE_ENTITY}}
SOURCE_ATTR: {{SOURCE_ATTR}}
which causes these errors in log:
2024-11-20 04:35:10.908 WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: 'SOURCE_ENTITY' is undefined when rendering 'SOURCE_ENTITY: {{SOURCE_ENTITY}}
SOURCE_ATTR: {{SOURCE_ATTR}}'
2024-11-20 04:35:10.909 WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: 'SOURCE_ATTR' is undefined when rendering 'SOURCE_ENTITY: {{SOURCE_ENTITY}}
SOURCE_ATTR: {{SOURCE_ATTR}}'
after HA reboot and after every change of “testing_number_2” helper - which is supposed to trigger the sensor.
Also, I may speculate that this is unsupported:
trigger:
- platform: state
entity_id: !input input_SOURCE_ENTITY
attribute: !input input_SOURCE_ATTR
but only can guess since we have no Docs so far.