Hi all,
I need to create a script which will later on called by an external component on a time basis, but this is another story. First of all I would like to put my checks into a working script but I’m not able to figure out how My goal is to check the update status of my “*_linkquality” devices and notify via Telegram the status, if above 14400 seconds.
Thanks @petro I was able to do the most of the steps:
I’ve created a group to have those devices together:
- alias: Create Link Quality Group on Startup.
trigger:
- platform: homeassistant
event: start
action:
- service: group.set
data:
object_id: linkquality
entities: >
{{ states.sensor | map(attribute='entity_id') | select('search', '_linkquality') | list }}
Then I’m able to use the template in the Developer Tools to check it with this:
{% set ns = namespace(entities = []) %}
{% for e in expand('group.linkquality') %}
{% if (now() - e.last_updated).seconds > 14400 %}
{% set ns.entities = ns.entities + [ e.name ~ ' last updated ' ~ relative_time(e.last_updated) ] %}
{% endif %}
{% endfor %}
{% if ns.entities | length > 0 %}
{% set message = "Attention" + ns.entities | join(', ') %}
{% else %}
{% set message = "All Okay" %}
{% endif %}
{{ message }}
…and it works!
Now I would like to send the {{ message }} to telegram bot if the sensors are not updated. The telegram bot works in other automations, and I can call it so:
You can use your template to assign a value to a variable in a script:
notify_linkquality:
sequence:
- variables:
message: >
{% set ns = namespace(entities = []) %}
{% for e in expand('group.linkquality') %}
{% if (now() - e.last_updated).seconds > 14400 %}
{% set ns.entities = ns.entities + [ e.name ~ ' last updated '
~ relative_time(e.last_updated) ] %}
{% endif %}
{% endfor %}
{% if ns.entities | length > 0 %}
{% set message = "Attention" + ns.entities | join(', ') %}
{% else %}
{% set message = "All Okay" %}
{% endif %}
{{ message }}
- service: notify.xefilbot
data:
title: '*Update Status*'
message: '{{ message }}'
Then add a service call to the end of your automation to run the script. Depending on the size of the linkquality group and the speed of your system, you may need to add a short delay before calling the notify script to allow the group.set to complete.
- alias: Create Link Quality Group on Startup.
trigger:
- platform: homeassistant
event: start
action:
- service: group.set
data:
object_id: linkquality
entities: >
{{ states.sensor | map(attribute='entity_id') | select('search', '_linkquality') | list }}
- delay: "00:00:02"
- service: script.notify_linkquality
I’m not a telegram user myself, but if I count correctly the byte offset it’s complaining about is the underscore in your message, and underscores are used to format text in italics. It’s probably complaining because there is only a single underscore.