Unavailable / Unknown Entity Monitoring - Template Sensor

Yeah the “\” and “\n” characters. Where are you copying the automation code from?


I follow you now. I recopied it from github. I have no clue how that happened. Let me retest and see if the push notify still doesn’t work.

I apologize, I’m a literal wombat sometimes.

Yeah, I think you have some issues there with wayward escape characters. Did you copy the RAW code from github?

image

Ok, see if it is reposted properly above. Yaml is going to be the death of me.

Much better. In your original post you had notify.mobile_app_weston as the service call. In the automation you posted you have notify.notify as the service call.
Updated the choose to if. (If is a new thing and is prefereable here. Need to update the git!)

Also I shortened the entity id list template. If you want to use the name like you asked about before you have to go back to the old way and expand the list.

Try this. Copy and paste the whole thing.

automation:
  - alias: Unavailable Entities Notification
    description: "Create persistent notification if there are unavailable entities, dismiss if none."
    trigger:
      - platform: state
        entity_id: sensor.unavailable_entities
        to: ~
    condition:
      - condition: template
        value_template: |
          {{ is_number(trigger.from_state.state)
              and is_number(trigger.to_state.state) }}
        action:
          - if:
              - condition: numeric_state
                entity_id: sensor.unavailable_entities
                below: 1
            then:
              - service: persistent_notification.dismiss
                data:
                  notification_id: unavailable_entities
            else:
              - service: persistent_notification.create
                data:
                  title: Unavailable Entities
                  message: "- {{ state_attr('sensor.unavailable_entities','entity_id')|join('\n- ') }}"
                  notification_id: unavailable_entities

              - service: notify.notify
                data:
                  title: Unavailable Devices
                  message: "- {{ state_attr('sensor.unavailable_entities','entity_id')|join('\n- ') }}"

Just so you realize how that trigger works - this will only trigger once, when the sensor goes above 0 for the first time. It won’t trigger if more entities become unavailable unless the sensor goes back to 0 first.

If you want notifications every time any sensor goes on or offline, you’ll have to adjust the trigger.

Yeah, I would prefer the weston one like you ended up doing. Ahhh, I follow you. I would have never gotten that, just don’t understand alot of the programming yet. I’m in a technical career field but I’m a monkey with programming, 3 lines take me 3 hours. haha. Thank you and I’m testing it now. And I follow you on the id list template. I think.

I am getting a “bad indentation” red exclamation when pasting to file editor add on

The entity_id attribute of sensor.unavailable_entities is already a list object of the entity id’s so there is no point in expanding it and creating a list of the entity id’s - you already had that!

If you wanted a list of a different attribute like the name (map(attribute=‘name’) you have to expand the object and remap it and recreate the list.

Clear as mud? :joy:

I follow you and yeah, that is more than likely what I would like because I really only plan on using this on the critical things like water leak sensor/ contact sensors for exterior doors etc.

I went back and reposted with the automation: section header. Maybe that will help you place it in the right spot.

Oh definitely, like the darkest mud you’ve ever seen. haha. I will spend some time on reading over this and hopefully understand/ figure it all out over the next few days. haha.

Let me know how you make out with the automation. I don’t use the File Editor so I’m not much help there. I’d look into setting up Samba and VSCode… much easier once you figure it out.

Why are there “-” at the start of your “alias” but not at the start of all of the ones in my file editor automations.yaml file?


yaml is hot garbage

D’oh. There’s no ID! You didn’t include it when you originally posted the automation.

I forgot you’re not posting into the automation UI editor which automatically puts an id.

Replace the alias line with this.

  - id: '3463875679457356848' 
    alias: Unavailable Entities Notification

*sighhh, it still doesn’t seem to be working. I’ve switched over to Studio Code Server. I got the Devil to work, my lord. spacing/ whitespace is a disaster in this programming language. Maybe it’s all of them but this is my first experience in these things.


Thank you Jason. It is working well now. I have notifications on my phone for unavailable important items!!

Well you did kinda jump right into the fire :laughing:

Glad you got 'er goin.

I’m packing it in tonight but when you realize what I meant by this and want to change it hit me up if you don’t get it figured out yourself.

I appreciate your willingness to help me with this especially since I’m just completely new to all of it and you were literally starting out in the negative with me–not even ground zero-- (I’m an engineer by trade but I play with bridges for a living so this coding blows my mind, never knew leading spaces could be such a nightmare in some languages, haha) and I agree with how you worded that. I’ll definitely have to digest some of this stuff to even understand it all and I would say your right, once I understand what is going on, I may end up coming back to you and being like “Onnnnn second thought, can this be done”. Let’s give that a little bit though. I would say you were blue in the face after dealing with this monkey for that little bit last night.

Thank you for offering your help, I really do appreciate it, these devices were really some of the only things (water sensor/ door sensors) I have in my house that were kind of “mission critical” and I just would like to make sure they were as bullet proof as possible (or as capable as I/ we ccould make them with a little help).

1 Like

Ok Jason. I’ve had a little breather and I hope you have had a bit of one also. Can you or someone here help me get the below to display friendly names in the notifications? I’m still learning this stuff and afraid I’m going to mess up the progress we’ve already made.

###################################################################################################
## Package - Unavailable Entities Sensor
## Count and list entities with a state of unavailable, unknown, or none (null)
## See README for customization options.
## https://github.com/jazzyisj/unavailable-entities-sensor/blob/main/README.md
###################################################################################################

# NOTE: Home Assistant v2021.12 required.  For older versions please see README
# REQUIRED - This is the template sensor
template:
  - sensor:
      - name: "Unavailable Entities"
        unique_id: unavailable_entities
        icon: "{{ 'mdi:alert-circle' if states('sensor.unavailable_entities')|int(0) > 0 else 'mdi:check-circle' }}"
        unit_of_measurement: entities
        state: >
          {% if state_attr('sensor.unavailable_entities','entity_id') != none %}
            {{ state_attr('sensor.unavailable_entities','entity_id')|count }}
          {% endif %}
        attributes:
          entity_id: >
            {% if state_attr('group.ignored_unavailable_entities','entity_id') != none %}
              {% set ignore_seconds = 300 %}
              {% set ignore_ts = (now().timestamp() - ignore_seconds)|as_datetime %}
              {% set entities = states|rejectattr('domain','in',['group','button','automation','scene'])|selectattr('state','in',['unavailable','unknown','none'])|list %}
              {% set buttons = states.button|selectattr('state','eq','unavailable')|list %}
              {{ (entities + buttons)
                |rejectattr('entity_id','in',state_attr('group.ignored_unavailable_entities','entity_id'))
                |rejectattr('entity_id','search','sensor.weatherbit')
                |rejectattr('entity_id','search','keymaster')
                |rejectattr('entity_id','search','media_player')
                |rejectattr('last_changed','ge',ignore_ts)
                |map(attribute='entity id')|list }}
            {% endif %}

# REQUIRED - Add any entities you do not wish to monitor in this group.
# IMPORTANT - This group MUST exist even if empty for sensor template to render.
group:
  ignored_unavailable_entities:
    entities:
      - sensor.unavailable_entities # prevent template loop warnings?
      - binary_sensor.back_porch_outlet_heat_alarm_overheat_detected
      - binary_sensor.garage_door_access_control_barrier_sensor_low_battery_warning
      - binary_sensor.garage_door_access_control_barrier_sensor_not_detected_supervisory_error
      - binary_sensor.garage_door_access_control_barrier_unattended_operation_has_been_disabled_per_ul_requirements
      - binary_sensor.garage_door_home_security_tampering_product_cover_removed
      - binary_sensor.garage_door_low_battery_level
      - light.desk_lamp
      - sensor.clarice_charger_type

# OPTIONAL - filter template loop warnings from the Home Assistant log.
logger:
  filters:
    homeassistant.components.template.template_entity:
      - "Template loop detected while processing event"
# OPTIONAL Example automation to demonstrate how you can utilize this sensor
# SEE Automation.yaml

Automation:

automation:
  - id: unavailable_entities_notification
    alias: 'Unavailable Entities Notification'
    description: 'Create persistent notification if there are unavailable entities, dismiss if none.'
    mode: restart
    trigger:
      - platform: state
        entity_id: sensor.unavailable_entities
        to: ~
    condition:
      - condition: template
        value_template: >
          {{ is_number(trigger.from_state.state)
              and is_number(trigger.to_state.state) }}
    action:
      - if:
          - condition: numeric_state
            entity_id: sensor.unavailable_entities
            below: 1
        then:
          - service: persistent_notification.dismiss
            data:
              notification_id: unavailable_entities
        else:
          - service: persistent_notification.create
            data:
              title: Unavailable Devices
              message: |-
                - {{ expand(state_attr('sensor.unavailable_entities','entity_id'))
                      |map(attribute='entity_id')|join('\n- ') }}
              notification_id: unavailable_entities
          - service: notify.mobile_app_weston
            data:
              title: Unavailable Devices
              message: |-
                - {{ expand(state_attr('sensor.unavailable_entities','entity_id'))
                      |map(attribute='entity_id')|join('\n- ') }}

EDITED: Automation Code