Testing jinja macro

This commit is contained in:
2025-02-22 17:45:14 +01:00
parent b79a7abfcd
commit bd08d22d03
2 changed files with 76 additions and 7 deletions

View File

@@ -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

View File

@@ -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 %}