My point is the sensor does not return the same output as the dev template with the exact same yaml . I don’t understand why that is, or how to fix that
seems the template: engine is just not properly evaluating those in the backend, and the dev tools template is.
(btw, this is a regular template sensor, not a trigger based template, so that cause can be ruled out)
I’ve already explained it. A set does not execute code. It’s already set. When you import it, it does not run it. It just gets the value that it has. You have to use a macro to run anything. And because you have to use a macro, you have to account for the output always being a string when you use the macro.
{% from 'alerts.jinja' import alerts_macro %}
{{ alerts_macro }}
return
<Macro ‘alerts_macro’>
maybe I am just better off using the this variable in these type of templates.
at least this works:
- unique_id: alerts_notifying_this
state: >
{{this.attributes.alerts|count}}
name: >
{% set count = this.state|int(default=-1) %}
{% set phrase = 'Alert' if count == 1 else 'Alerts' %}
{{count}} {{phrase}} actief
icon: >
{% set count = this.state|int(default=-1) %}
mdi:numeric-{{count}}-box
attributes:
alerts: >
{{ expand(state_attr('binary_sensor.alerts','entity_id'))
|selectattr('state','eq','on')|map(attribute='name')|list }}
message: >-
{% set count = this.state|int(default=-1) %}
{% set alerts = this.attributes.get('alerts') %}
{%- if count == 0 -%} Ok, geen alerts, alles is in orde
{%- elif count == 1 -%}{{count}} Alert: {{alerts[0]}}
{%- elif count == 2 -%}
{{count}} Alerts: {{alerts[0]}} en {{alerts[1]}}
{%- else -%}
{{count}} Alerts:{{alerts[:-1]|join(', ')}}, en {{alerts[-1]}}
{%- endif %}
… I am not sure though if this is just as efficient as using the custom_templates. as in, whether the attribute is evaluated only once, and not in every single config option of the template sensor
Please, can you go back to my original response and reread it? I already explained everything you need to do in the first post. End your macro with a join, and split the macros result.
yes, thanks Petro and 123, sometimes one has to go over things oneself to truly grasp.
for the sake of completeness/reference:
wat is your opinion on this final template sensor using the macro in the custom_templates, compared to the version with the this variable, from an efficiency perspective?
I started this whole migration with that in mind after all…
- unique_id: critical_switches_offline
state: >
{% from 'alerts.jinja' import critical_off %}
{{critical_off().split(',')|count}}
name: >
{% from 'alerts.jinja' import critical_off %}
{% set count = critical_off().split(',')|count %}
{% set phrase = 'switch' if count == 1 else 'switches' %}
{{count}} Critical {{phrase}} offline
icon: >
{% from 'alerts.jinja' import critical_off %}
{% set count = critical_off().split(',')|count %}
mdi:numeric-{{count}}-box
attributes:
message: >-
{% from 'alerts.jinja' import critical_off %}
{% set critical = critical_off().split(',') %}
{% set count = critical_off().split(',')|count %}
{%- if count == 0 -%} Ok, all critical switches are online
{%- elif count == 1 -%}{{count}} Critical switch offline: {{critical[0]}}
{%- elif count == 2 -%}
{{count}} Critical switches offline: {{critical[0]}} and {{critical[1]}}
{% else %}
{{count}} Critical switches are offline: {{critical[:-1]|join(', ')}}, and {{critical[-1]}}
{% endif %}
cant it handle an empty list?, and the split there causing the empty list to be [''] which returns 1 when |count that. Note there is NO switch listed after the 1 Critical switch offline: (and that is actually correct)