Hello.
I have been trying to figure out a way of having an automation triggered for a specific set of entities in case its state has NOT changed for a period of time (let’s say 24hours, for instance).
I considered the following:
automation:
- alias: 'Longtime unchanged'
id: longtime_unchanged
trigger:
- platform: template
value_template: >-
{% set check_entities = (
'sensor.temperatura_diogo_bt',
'sensor.temperatura_quarto_bt',
'sensor.temperatura_sala_bt',
'sensor.temperatura_sara_bt',
'sensor.temperatura_wc_bt',
'sensor.temperatura_wc_quarto_bt'
) %}
{% for item in states -%}
{%- if item.entity_id in check_entities %}
{{ (as_timestamp(now()) - as_timestamp(as_local(item.last_changed))) > 86400 }}
{%- endif %}
{%- endfor %}
action:
- service: notify.ios_papa
data:
title: "{{ states('sensor.ha_message_alert') }}"
message: "{{ trigger.to_state.attributes.friendly_name }} unchanged for 1 day now"
data:
group: "not_available"
and thought that once my template trigger condition would become true (one entity without state changes for more than 24h), it would spam me with notifications until the condition would become false…
I also considered the following (triggering the automation at a specific time each day and use template as a condition instead):
automation:
- alias: 'Longtime unchanged'
id: longtime_unchanged
trigger:
- platform: time
at: '15:00:00'
condition:
condition: template
value_template: >-
{% set check_entities = (
'sensor.temperatura_diogo_bt',
'sensor.temperatura_quarto_bt',
'sensor.temperatura_sala_bt',
'sensor.temperatura_sara_bt',
'sensor.temperatura_wc_bt',
'sensor.temperatura_wc_quarto_bt'
) %}
{% for item in states -%}
{%- if item.entity_id in check_entities %}
{{ (as_timestamp(now()) - as_timestamp(as_local(item.last_changed))) > 86400 }}
{%- endif %}
{%- endfor %}
action:
- service: notify.ios_papa
data:
title: "{{ states('sensor.ha_message_alert') }}"
message: >-
{% set check_entities = (
'sensor.temperatura_diogo_bt',
'sensor.temperatura_quarto_bt',
'sensor.temperatura_sala_bt',
'sensor.temperatura_sara_bt',
'sensor.temperatura_wc_bt',
'sensor.temperatura_wc_quarto_bt'
) %}
{% for item in states -%}
{%- if item.entity_id in check_entities %}
{%- if (as_timestamp(now()) - as_timestamp(as_local(item.last_changed))) > 86400 %}
{{ item.attributes.friendly_name }} unchanged for 1 day now
{%- endif %}
{%- endif %}
{%- endfor %}
data:
group: "not_available"
but “repeating” the template in the condition and the action, besides odd, it doesn’t guarantee that I get the same exact result in case more than 1 entity is state-unchanged for the last 24h.
I could not figure out if I could somehow bring the template condition result to the action (to allow identifying the exact entity ID friendly name that caused the condition to be true). Is that even possible?
Maybe I’m “overcomplicating”, but I would really appreciate any ideas on how to achieve my purpose, even if completely different from the ones I have shown above.
Has anyone have anything similar built only based on automations (not appdaemon or something)?
Thanks in advance for any insights.
Kind regards.