#Automation if sensor not seen for extended period of time, but limit how often it triggers

1 messages · Page 1 of 1 (latest)

random isle
#

I have some cheap water sensors which have useless battery monitoring, and so they may die having never reported a battery level lower than 100%. As a workaround, I created an automation which triggers every 15 minutes, is conditioned on any one of the sensors' ..._last_seen entity being greater than 8 hours ago, and it sends me an e-mail.

This works, but the problem is once the condition becomes true, I'll keep getting an e-mail every 15 minutes until I fix it. Is there something I can add to the condition, or anything else I can do, to prevent the automation from performing its action for the same device and last_seen value? Is there anything I can use to record state information, and refer back to it?

harsh matrix
#

Why don’t you just trigger when the last_seen is older than 8 hours?
You can do that with a template trigger.
Then you don’t have to worry about repeated notifications.

#

Time pattern triggers are almost always the worst way to trigger an automation

random isle
#

because my understanding of how triggers work means that won't work

#

according to the documentation, triggers are only evaluated when the entities they refer to change

#

in this case, it is the fact that the entity hasn't changed that i want to trigger my automation

harsh matrix
#

When you use now() in a template, that template will be rendered at the start of every minute

random isle
#

where is that documented???

#

fwiw i'm not getting annoyed at you, just annoyed that seems to not be mentioned anywhere i looked

random isle
#

FML

#

ok, thank you!

harsh matrix
#

No worries.

random isle
#

well, that would help me clean up this script a bit

#

but i think i still have the underlying issue of repeated notifications

harsh matrix
#

FYI, there are ways to prevent automations from running again (like a cool-down period) but it doesn’t seem like you need that here

random isle
#

i do

#

that is the main issue i have. if the trigger occurs every minute, now i will get an email every minute

#

i can prevent the automation from running again by setting it to mode single, and having one of the actions be a delay

harsh matrix
#

Template triggers (and every other kind of trigger) will fire when they transitions from false to true. If they re-evaluate as true again, nothing happens

random isle
#

ok so by moving my condition template to the trigger, it would eliminate this problem

harsh matrix
#

Correct

random isle
#

ok let me try this out

#

thank you

#

is there any way to pass data from the trigger template to the action template?

harsh matrix
random isle
#

but that just makes certain data available. i mean passing arbitrary data.

#

actually forget that for a moment.... i followed your suggestion, but my automation is not triggering

harsh matrix
#

It has to transition from false to true. If it is true when you create the automation, then it didn’t transition

random isle
#

😦

#

is there some way i can force it to trigger once, or some way to test this then?

harsh matrix
#

You can just run the automation if you want to test the actions. If you want to see it actually trigger, calculate the duration you’d need to enter for it to trigger in a minute from now, and then wait

random isle
#

actually i guess i could just make it False deliberately

harsh matrix
#

You can also go to developer tools -> states and override the state of a sensor

random isle
#

i made my automation trigger template return false at the end. if i wait a minute, then remove the explicit false, would that work?

harsh matrix
#

It has to transition without being edited. When you save an automation, it will de-arm the old one and re-arm the new one. It has to transition while it is armed.

random isle
#

sigh

harsh matrix
#

If the template is true when you press save, then it won’t fire until it goes to false and back to true

random isle
#

ok i guess i'll change the state manually then

harsh matrix
#

If last_seen was 9 hours and 37 minutes ago, just change the template to check if is been 9 hours and 38 minutes

random isle
#

🤞

harsh matrix
#

Changing state will work too

random isle
#

ok well while i'm waiting for that, any way to pass arbitrary data from the trigger template to the action template? my trigger template is calculating a list of last_seen entities which are too old. i'd like to pass that result so my action template can include it in the email without me having to calculate it again (which i am currently doing)

harsh matrix
#

There are ways to pass arbitrary data, but not data from within the template that is used for the template trigger

#

You could put the info in a template sensor, and then use that in both the trigger and the email

random isle
#

ok, my original issue is solved. moving the criteria to the trigger definitely works

harsh matrix
#

Well, if you use the variables: key in a trigger, you are not restricted to limited templates, but you still can’t pull data out of the template that was defined for the trigger

random isle
#

as for this passing data thing, i can actually access trigger.entity_id in the actions and i don't understand how

#

i don't understand how HA can glean that information from my trigger template

harsh matrix
#

It will be the entity that caused the template to render true

random isle
#

but how can it know that? utcnow() is what caused the template to render

#
{%- set watersensors_last_seen_entities = states
                                        | map(attribute="entity_id")
                                        | select("match", "sensor\.watersensor[^.]+_last_seen$")
                                        | list
-%}

{%- set ns = namespace(stale_watersensors = []) -%}
{%- set current_time_utc = utcnow() -%}

{%- for last_seen_entity in watersensors_last_seen_entities -%}
  {%- set last_seen = as_datetime(states(last_seen_entity)) -%}
  {%- if current_time_utc - last_seen > timedelta(hours=8, minutes=15) -%}
    {%- set device_name = last_seen_entity | regex_replace("^sensor\.(watersensor[^.]+)_last_seen$", "\\1") -%}
    {%- set ns.stale_watersensors = ns.stale_watersensors + [(device_name, last_seen)] -%}
  {%- endif -%}
{%- endfor -%}

{{ ns.stale_watersensors | length > 0 }}

that's my trigger template

#

if i had to guess, i'd say it's just checking what entity changed state between when this evaluated false and when it evaluated true. if so, then that would explain why it worked, because i manually changed its state from developer tools

but when this is running for real, that won't happen

#

also, theoretically my trigger could occur for multiple entities, so i'd still ideally like to pass some data to actions so i can get the full list, not just what HA thinks the triggering entity is

harsh matrix
#

Are you saying you verified the trigger variable populated the correct entity_id when you tested it by changing the state manually?

random isle
#

yes

#

and i don't see how HA could have known, other than my hypothesis above

harsh matrix
#

I’m not sure, I haven’t looked at the code for it. But you are correct, multiple entities could be the cause for it to render true. I can’t answer how it works (or whether it will in this case)

#

Unrelated to that question, but I would change the first line to states.sensor (instead of states) so it at least is a little more efficient and doesn’t have to search the entire state machine

random isle
#

ok i simplified my trigger to just

{{ utcnow() - states.sensor
            | map(attribute="entity_id")
            | select("match", "sensor\.watersensor[^.]+_last_seen$")
            | map('states')
            | map('as_datetime')
            | min
            > timedelta(hours=8, minutes=15) }}
#

and left the longer code (what i posted before) to the template code for the action, which determines the e-mail body

#

i'd still love to find some way to not repeat this code, because i also want to set the subject dynamically

#

but for now it's just a static string "Have not heard from water sensor(s) in a while"

harsh matrix
#

Use a variable as the first action in the automation, and put the code there

random isle
#

aha!

#

thanks

#

was looking for something like that, but i just noticed Script and it seemed more complicated

harsh matrix
#

Often what people will do in situations like this is to create a template sensor that has the count of entities as the state, and then has an attribute that contains all the entity id’s. Then the automation is triggered when the count changes state, with a template condition that it increased, and then the attributes can be used in the notifications. Plus this gives you some history about what has been happening.

random isle
#

well it shouldn't be when the count changes state, because one entity could "swap" places with another and the count would remain the same

harsh matrix
#

They would have to swap in the same second, but yes. You could trigger on a change to the attribute instead.

random isle
#

ok i defined the sensor, but how can i define its attributes?

harsh matrix
#

It has to be done in YAML

random isle
#

ok

harsh matrix
#

Can’t use the ui

random isle
#

which file is it in? i created a sensor already, but i cannot find the file it's in

harsh matrix
#

Sensors created in the ui aren’t put into a file that is editable. You have to create a new sensor in yaml from scratch

random isle
#

i'll add that to my list of mind boggling design decisions. it's a very long list now.

#

ok, so which file should i put it in, just the regular configuration.yaml?

harsh matrix
#

Yes, if you haven’t split up your configuration file then it just goes into that file.

random isle
#

i mean it's already partially split up

#

i didn't do it, HA just set it up that way

#
default_config:

...

automation: !include automations.yaml
script: !include scripts.yaml
scene: !include scenes.yaml
harsh matrix
#

You can do the same for template: if you want, or just put the config in the main file. Up to you.

#

I’m signing off, hopefully someone else can pick up if you’ve got more questions. Good luck!

random isle
#

would it be a trigger-based sensor?