jazzyisj/unavailable-entities-sensor

Feature Request: Include Entities and run every x minutes

hapklaar opened this issue · 2 comments

Great blueprint, almost what I was looking for. It could become a bit more versatile if you would consider adding these features:

  • Instead of only providing the 'Exclude Entities' option in the blueprint, also give the options to specify 'Include Entities' to only detect state changes for entities you want to monitor.
  • Give the option to run the detection every x minutes, to be able to get notified immediately when an entity becomes unavailable.

This is a template, not a blueprint. Blueprints create scripts and automations, this creates a sensor that you can use in scripts and automations (with examples provided).

I keep the template as simple as possible in the git to make it easy to understand and because most people are going to customize it to their own needs anyway.

Give the option to run the detection every x minutes, to be able to get notified immediately when an entity becomes unavailable.

The template as is updates every time the entity_id attribute changes. You can set the automation that sends your notification to trigger whenever you want. The example automation in the package fires immediately when the sensor goes above 0.

You can change the sensor template to a trigger template style to limit state updates to once per minute using a time sensor as a trigger (you must have the time integration installed). In fact, that is exactly what I have done in my on configuration for my customize version of the sensor.

If you're really trying to limit how often your notifications get sent, it would be preferable to control that in your automation rather than the template itself.

Instead of only providing the 'Exclude Entities' option in the blueprint, also give the options to specify 'Include Entities' to only detect state changes for entities you want to monitor.

More or less covered by #16 but here's how to do it for a group. I also made it a trigger sensor to put it all together for you. Keep in mind using a trigger template sensor this way, the entity_id attribute will still update constantly but the state will only update once per minute. So it's possible for the state and number of entities listed in the attribute to be different for up to a minute until the sensor updates again.

- trigger:
    - platform: homeassistant
      event: start

    - platform: event
      event_type: event_template_reloaded

    # limit state update to once per minute
    - platform: state
      entity_id: sensor.time
  sensor:
    - name: "Unavailable Entities"
      unique_id: unavailable_entities
      icon: "{{ iif(states(this.entity_id)|int(-1) > 0,'mdi:alert-circle','mdi:check-circle') }}"
      state: >
        {% set entities = state_attr(this.entity_id,'entity_id') %}
        {{ entities|count if entities != none else none }}
      state_class: measurement
      attributes:
        entity_id: >
          {% set ignore_seconds = 60 %}
          {% set ignored = state_attr('group.ignored_unavailable_entities','entity_id') %}
          {% set ignore_ts = (now().timestamp() - ignore_seconds)|as_datetime %}
          {% set entities = expand('group.monitored_entities')
              |rejectattr('last_changed','ge',ignore_ts)
              |map(attribute='entity_id')|list
              |reject('has_value') %}
          {{ entities|list }}

group:
  monitored_entities:
    entities:
      - sensor.my_ignored_sensor

Oops I'm sorry... I placed this feature request in the wrong place, it was meant for https://github.com/gmlupatelli/blueprints_repo in regards to this blueprint

Your suggestions do help in my understanding however, thanks for that.