jazzyisj/unavailable-entities-sensor

Feature Request: ignore entities time based

moehringn opened this issue · 3 comments

It would be awesome to also have the option to ignore entities time based.
E.g. I have ahoyDTU which gives me solar panel channels over MQTT. But 4 entities from that MQTT integration will be unavailable at night because there's obviously nothing to receive.
It would be great to be able to have some sensor/integration about sunrise and sunset (probably some weather forecast integration will do that) and based on that, a certain list of entities is then ignored between a certain time frame.
Would something dynamic like this be even possible?

Also highly intereseted on that ability

@Mar1usW3 @moehringn
Try this out and see if it does what you want.
You must have the Sun integration installed.
You must also create the group group.night_ignored_unavailable_entities.

template:
  - sensor:
      - name: "Unavailable Entities"
        unique_id: unavailable_entities
        icon: "{{ iif(states(this.entity_id)|int(-1) > 0,'mdi:alert-circle','mdi:check-circle') }}"
        state_class: measurement
        unit_of_measurement: entities
        state: >
          {% set entities = state_attr(this.entity_id,'entity_id') %}
          {{ entities|count if entities != none else none }}
        attributes:
          entity_id: >
            {% set ignore_seconds = 60 %}
            {% set ignore_ts = (now().timestamp() - ignore_seconds)|as_datetime %}
            {% set is_night = states('sun.sun') == 'below_horizon' %}
            {% set ignored = state_attr('group.ignored_unavailable_entities','entity_id') %}
            {% set ignore_night = state_attr('group.night_ignored_unavailable_entities','entity_id') %}
            {% set entities = states|rejectattr('domain','eq','group')
                |rejectattr('last_changed','ge',ignore_ts) %}
            {% set entities = entities|rejectattr('entity_id','in',ignored)
                if ignored != none else entities %}
            {% set entities =  entities|rejectattr('entity_id','in',ignore_night)
                if is_night and ignore_night != none else entities %}
            {{ entities|map(attribute='entity_id')|reject('has_value')|list|sort }}

group:
  ignored_unavailable_entities:
    entities:
      - sensor.my_ignored_sensor

  night_ignored_unavailable_entities:
    entities:
      - sensor.skin_type_1_safe_exposure_time
      - sensor.skin_type_2_safe_exposure_time
      - sensor.skin_type_3_safe_exposure_time
      - sensor.skin_type_4_safe_exposure_time
      - sensor.skin_type_5_safe_exposure_time
      - sensor.skin_type_6_safe_exposure_time

@jazzyisj thank you very much. I have tested your modification with some entities the last days and it works perfect. Thank you very much.