Hi,
I hope here is somebody with ‘extended’ knowledge who is willing to support a grumpy old guy. I have a developer background and working with ha since more then two years - I am also providing multiple custom integrations - so I would consider myself not as a rookie.
But since a couple of days I am struggling (for me personal use) with configuration of (multiple) template sensors via yaml. My challenge is, that my sensors will use some base information that must be calculated from different other sensor data. So instead of providing the (complex) code with every single template-sensor I want to make use of ‘variables’ declared in my sensor template yaml file (mainly for clean code reasons - to avoid code duplication)…
The documentation indicates, that HA is supporting variables (in the templates integration) - Template - Home Assistant
But I am struggling (hard) to access/use a value of such a defined variable in a simple template sensor:
Here is my (very simple) example:
In my ha configuration.yaml:
homeassistant:
packages:
my_var_test: !include sensor-tests.yaml
and the sensor-tests.yaml:
# https://www.home-assistant.io/integrations/template
# https://www.home-assistant.io/docs/configuration/templating/
# https://community.home-assistant.io/t/global-template-variables/367611/4
template:
- variables:
var001: "{{now()}}"
var002: "MyValue"
- sensor:
- name: "VariablesTestTemplate"
unique_id: var_test_template_sensor
state: >
{{var001}} {{var002}}
but when in the log’s I can see:
- 2024-11-16 10:25:00.354 WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: ‘var001’ is undefined when rendering ‘{{var001}} {{var002}}’
- 2024-11-16 10:25:00.354 WARNING (MainThread) [homeassistant.helpers.template] Template variable warning: ‘var002’ is undefined when rendering ‘{{var001}} {{var002}}’
So ok looks like, that in the scope of the sensor my declared variables does not exist… I could not find a way to access the values of my (external) declared variables, tried multiple things like '{{this.var001}} or {{variables.var001}} or {{this.parent.var001}}… but all of that failed as well…
So I initially thought, that only in blueprints the variables will be populated & send via the **kwargs - BUT when I make use of an additional external test.jinja file (placed in ‘custom_templates’ like this one:
{% macro test_macro2() -%}
{{now()}}
{%- endmacro %}
{% set test_variable2 = test_macro2() %}
then I am able to access these external variables (& macros) from my sensor:
template:
- sensor:
- name: "VariablesTestTemplate"
unique_id: var_test_template_sensor
state: >
{% import 'test.jinja' as t %}
{{ t.test_macro2() }} {{ t.test_variable2 }}
So before I now refactor all my stuff (and move my variables (that I want to declare in my ‘sensor-tests.yaml’ file) to a separate custom_templates/*.jinja file) I wanted to ask, if somebody of you might have a hint, how I can access a declared variable value (no matter if static value or another template) from such a templates platform sensor definiation?!