Quantcast
Channel: Configuration - Home Assistant Community
Viewing all articles
Browse latest Browse all 106517

Develop and rebuild template sensor to python_script

$
0
0

Continuing the discussion from Heads up! Upcoming breaking change in the Template integration:

since Ha 115 checks for all states mentioned in a template sensor continuously now, below template causes havoc in my system (with a lot of states)

various alternatives were suggested, from leaving it out, cutting my system, or rewriting using an automation and an input_text. The latter would be most suitable were it not that that has a max Character limit of 255,which, obviously isnt near suitable for showing a longer list of entities (that can have long names themselves) in a frontend sensor card. We need to steer away from a sensor template, and a python script seems most appropriate.

Have an automation or script handle running the script, and have that script write to a sensor which can have as many attributes as we need, with as many characters as we need.

Now my original template sensor was this:

sensor:

# https://gist.github.com/jazzyisj/45fce29d825b8f9b29c5974444299d90
# https://community.home-assistant.io/t/sensor-unavailable-offline-detection/147618/10
# https://community.home-assistant.io/t/how-to-list-all-sensors-with-state-unavailable-none-or-unknown/154606

  - platform: template
    sensors:
      entities_unavailable:
#        entity_id:
#          - script.update_entities_uun
#          - automation.check_for_unavailable_entities
        friendly_name: Entities Unavailable
        value_template: >
          {% set ignore_list = ['light.driveway_floodlight','light.garden_backyard_floodlight','light.garden_terrace_floodlight',
                                'light.porch_floodlight','light.parking_light'] if
                                 is_state('binary_sensor.outside_daylight_sensor','on') else [] %}
          {% set unavailable = states|selectattr('state','eq','unavailable')
                                     |rejectattr('entity_id','in',state_attr('group.entity_blacklist','entity_id'))
                                     |rejectattr('entity_id','in',ignore_list)
                                     |rejectattr('domain','eq','media_player')
                                     |list %}
          {{unavailable|count}}

#          {% if states('sensor.entities_unavailable')|int > 0 %} mdi:thumb-down
#          {% else %} mdi:thumb-up
#          {% endif %}

      entities_uun:
#        entity_id:
#          - script.update_entities_uun
#          - automation.check_for_unavailable_entities
        friendly_name: Entities U/U/N
        value_template: >
          {% set ignore_list = ['light.driveway_floodlight','light.garden_backyard_floodlight','light.garden_terrace_floodlight',
                                'light.porch_floodlight','light.parking_light'] if
                                 is_state('binary_sensor.outside_daylight_sensor','on') else [] %}

          {{states|selectattr('state','in',['unavailable','unknown','none'])
                  |rejectattr('entity_id','in',ignore_list)
                  |rejectattr('entity_id','in',state_attr('group.entity_blacklist','entity_id'))
                  |rejectattr('domain','in',['group','media_player'])
                  |list|length}}
        attribute_templates:
          Unknown: >
            {% set unknown = states|selectattr('state','eq','unknown')
                                   |rejectattr('entity_id','in',state_attr('group.entity_blacklist','entity_id'))
                                   |rejectattr('domain','in',['group'])
                                   |map(attribute='entity_id')
                                   |list %}
            {% if unknown|count == 0 %} 0
            {% else %}
            {{unknown|count}}:
            {{'\n' + unknown|join(',\n')}}
            {% endif %}
          Unknown sensors: >
            {% set unknown = states.sensor|selectattr('state','eq','unknown')
                                          |rejectattr('entity_id','in',state_attr('group.entity_blacklist','entity_id'))
                                          |map(attribute='entity_id')
                                          |list %}
            {% if unknown|count == 0 %} 0
            {% else %}
            {{unknown|count}}:
            {{'\n' + unknown|join(',\n')}}
            {% endif %}
          Unavailable: >
            {% set ignore_list = ['light.driveway_floodlight','light.garden_backyard_floodlight','light.garden_terrace_floodlight',
                                  'light.porch__floodlight','light.parking_light'] if
                                   is_state('binary_sensor.outside_daylight_sensor','on') else [] %}
            {% set unavailable = states|selectattr('state','eq','unavailable')
                                       |rejectattr('entity_id','in',state_attr('group.entity_blacklist','entity_id'))
                                       |rejectattr('entity_id','in',ignore_list)
                                       |rejectattr('domain','eq','media_player')
                                       |map(attribute='entity_id')
                                       |list %}
            {% if unavailable|count == 0 %} 0
            {% else %}
            {{unavailable|count}}:
            {{'\n'}}{{unavailable|join(',\n')}}
            {% endif %}
          None: >
            {% set none_ = states|selectattr('state','eq','none')
                                 |map(attribute='entity_id')
                                 |list %}
            {% if none_|count == 0 %} 0
            {% else %}
            {{none_|count}}:
            {{'\n' + none_|join(',\n')}}
            {% endif %}
          Full: >
            {% set full = states|selectattr('state','in',['unavailable','unknown','none'])
                                |map(attribute='entity_id')
                                |list %}
            {{full|count}}:
            {{'\n' + full|join(',\n')}}

rather large, I admit, but it was very useful. Have it run after a delayed startup, check any red flags, fix if possible, run again to see if things got better, and where to look for issues (imagine a weather integration not initializing or whatever other integration) If alright, dont look back and leave alone.

Previously that could be done by setting the entity_id in the sensors config, as you can see above, now commented.

@VDRainer already started with a bare python_script, which does what the bottom attribute Full in my template sensor does, write the full unfiltered list :

count = 0
attributes = {}

for entity_id in hass.states.entity_ids():
    state = hass.states.get(entity_id).state
    if state in ['unavailable','unknown','none']:
        attributes[entity_id] = state
        count = count + 1

attributes['friendly_name'] = 'Unavailable Entities'
attributes['icon'] = 'mdi:message-alert-outline'

hass.states.set('sensor.unavailable_entities', count, attributes)

which runs alright.
I would love to expand on that by at least having the states ‘none’, ‘unavailable’, and ‘unknown’ in their own ordered lists (attributes).

further more, we really need the ignore_list and black_list filtering, along with a few domain rejectors :wink:

Not really knowing where to start here, I turn to the community for assistance, please have a look if you can help me out here?

would be much appreciated! thanks…

1 post - 1 participant

Read full topic


Viewing all articles
Browse latest Browse all 106517

Trending Articles