thanks to @petro who answered in this topic, I’ve built configurable/universal notification script. I have telegram bots for my users pjy & aga, with services: notify.ntf_bot_pjy and notify.ntf_bot_aga (right now one for each - but I guess I’ll make it configurabie via variables too, so I can have only one). then, every user can set his/her preference regarding the notifications, by choosing one of the input_select list options:
input_select.kfg_isl_notifytg_pjy and input_select.kfg_isl_notifytg_aga are:
- turned off
- turned off when at home
- show all
- show all, but only important when at home
- only important
(numbers are read from the input_select, they are not a part of the option). the aim of my notification script is, that wherever you want, you can call it by giving it the target user, importance flag (true/false) and the message. script will check the settings, user location and should take action based on message’s importance - so, if you want to tell user “pjy” that “kitchen lamp is turned on” and you think that it’s important, you call the script like:
- service: script.spt_event_notification
data_template:
temp_target: "pjy"
temp_important: true
temp_message: "kitchen lamp is turned on"
then, the script is executed:
spt_event_notification:
sequence:
- condition: template
value_template: >
{% if (temp_target == 'pjy') %}
{% set temp_notifytg = state_attr('input_select.kfg_isl_notifytg_pjy','options').index(states('input_select.kfg_isl_notifytg_pjy')) %}
{% if (states.device_tracker.pjxel.state == 'home') %}
{% set temp_home = true %}
{% else %}
{% set temp_home = false %}
{% endif %}
{% elif (temp_target == 'aga') %}
{% set temp_notifytg = state_attr('input_select.kfg_isl_notifytg_aga','options').index(states('input_select.kfg_isl_notifytg_aga')) %}
{% if (states.device_tracker.agaxel.state == 'home') %}
{% set temp_home = true %}
{% else %}
{% set temp_home = false %}
{% endif %}
{% endif %}
{% if (temp_home) %}
{% if ((temp_notifytg > 1) and (temp_important)) %}
true
{% elif ((temp_notifytg == 2) and not(temp_important)) %}
true
{% else %}
false
{% endif %}
{% elif not(temp_home) %}
{% if ((temp_notifytg > 0) and (temp_important)) %}
true
{% elif ((temp_notifytg > 0) and (temp_notifytg < 4) and not(temp_important)) %}
true
{% else -%}
false
{%- endif -%}
{%- else -%}
false
{%- endif -%}
- service_template: >
{%- if (temp_target == 'pjy') -%}
notify.ntf_bot_pjy
{%- elif (temp_target == 'aga') -%}
notify.ntf_bot_aga
{%- endif -%}
data_template:
message: "{{ temp_message}}"
when I test this huge, nested if-elif-else-endif template in dev-template part of HA’s frontend, everything works fine. template shows true/false status like it should, but when used “in production”, it looks like it’s skipping some parts of the template.
example - I call the script with not important message:
- service: script.spt_event_notification
data_template:
temp_target: "pjy"
temp_important: false
temp_message: "something not important happened"
user pjy is home, so temp_home = true, my notification settings are only important, so temp_notifytg = 4. dev-template gives correct answer - it shows just false, but script is giving the notification anyway. can somebody tell me why? is it something I did wrong? or maybe the condition in script’s sequence isn’t giving a proper “false” answer, but just spits out “false” string and not logic value? what’s strange - if I choose to set my notifications as turned off it is indeed turned off and script works correctly.
FYI while checking the logs I can see that there are sometimes messages like:
2018-10-06 23:30:01 WARNING (MainThread) [homeassistant.components.script] Script script.spt_event_notification already running.
but don’t know if it’s related to script’s wrong behavior.