Help creating a script

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 :slight_smile: 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:

service: notify.xefilbot
data:
  title: '*Update Status*'
  message: '{{ message }}'

How to put all that together in a script?

Thanks!!

Simon

You just need to copy the template code into the automation action:

service: notify.xefilbot
data:
  title: '*Update Status*'
  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 }}

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

Hi @Didgeridrew and @exxamalte , thanks for the suggestions.
Trying the script, I’m getting:

2022-06-08 17:20:52 ERROR (SyncWorker_21) [homeassistant.components.telegram_bot] Error sending message: Can't parse entities: can't find end of the entity starting at byte offset 49. Args: (491832890, 'Update Status\nAttention Camera Sensore Ambientale_linkquality last updated 5 days, Soggiorno Sensore Ambientale linkquality last updated 5 days'), kwargs: {'parse_mode': 'Markdown', 'disable_web_page_preview': None, 'disable_notification': False, 'reply_to_message_id': None, 'reply_markup': None, 'timeout': None}

Ideas why?

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.