From bd08d22d03e86dd423a2c327afb9daa54c663bb5 Mon Sep 17 00:00:00 2001 From: ttrabatt Date: Sat, 22 Feb 2025 17:45:14 +0100 Subject: [PATCH] Testing jinja macro --- .../automation/homeassistant/test_macro.yaml | 51 +++++++++++++++++++ custom_templates/template_library.jinja | 32 +++++++++--- 2 files changed, 76 insertions(+), 7 deletions(-) create mode 100644 blueprints/automation/homeassistant/test_macro.yaml diff --git a/blueprints/automation/homeassistant/test_macro.yaml b/blueprints/automation/homeassistant/test_macro.yaml new file mode 100644 index 0000000..c778a16 --- /dev/null +++ b/blueprints/automation/homeassistant/test_macro.yaml @@ -0,0 +1,51 @@ +blueprint: + name: Test-Macro + description: Prova Jinja2 Macro + domain: automation + author: Tiziano Trabattoni + # input: + # window_sensor: + # name: Window Status + # description: Identifies the Window Sensor Device for Opened Status + # selector: + # entity: + # filter: + # - device_class: door + # domain: binary_sensor + +variables: + recipient_list: + - notify.mobile_app_tiziano_iphone15pro + # - notify.mobile_app_emanuele_s24 + evcharge_entity1_power_var: >- + {% from 'template_library.jinja' import getpower_id %} + {{ getpower_id('switch.evcharge_back') }} + +triggers: + - trigger: mqtt + topic: zigbee2mqtt/Pulsante-01 + payload: single + value_template: "{{ value_json['action'] }}" + id: Pulsante1-single + +conditions: [] +actions: + - choose: + - conditions: + - condition: trigger + id: + - p1-confort-light-on + - test_animation + sequence: + # Hereafter the attemped usage as variable initialization + - repeat: # send message to all recipient-list members + for_each: "{{ recipient_list }}" + sequence: + - action: "{{ repeat.item }}" + metadata: {} + data: + message: >- + "TEST -> >>{{ evcharge_entity1_power_var }}<<" + title: TEST TEST + +mode: single diff --git a/custom_templates/template_library.jinja b/custom_templates/template_library.jinja index 70e0999..a3b79ee 100644 --- a/custom_templates/template_library.jinja +++ b/custom_templates/template_library.jinja @@ -1,9 +1,27 @@ -{% macro getpower_id(evcharge_id) %} + {# This macro returns _power entity for a controlled plug device #} + {% macro getpower_id(evcharge_id) %} {% set attrib_list = device_entities( device_id (evcharge_id ) ) %} - {% for attrib in attrib_list %} - {% if ( attrib | regex_search('power$')) %} - {{ attrib }} - {% endif %} - {% endfor %} -{% endmacro %} + + {% for attrib in attrib_list -%} + {% if ( attrib | regex_search('power$')) -%} + {{ attrib }} + {%- endif %} + {%- endfor %} + {% endmacro %} + + + {########################################################################} + + {# This macro returns _energy entity for a controlled plug device #} + + {% macro getenergy_id(evcharge_id) %} + {% set attrib_list = device_entities( device_id (evcharge_id ) ) %} + + {% for attrib in attrib_list -%} + {% if ( attrib | regex_search('energy$')) -%} + {{ attrib }} + {%- endif %} + {%- endfor %} + {% endmacro %} +