Count number of entities by domain

What would be the secret for this secret??

I’m trying to figure out how to get a list (or lists) of entities by integration (i.e. OpenZWave, etc.)…

The api endpoint of your HA instance e.g.

http://192.168.1.100:8123/api

yes, in my case I use:

resource_ha_rpi4_config: https://mydomain.duckdns.org:port/api/config

Error loading /config/configuration.yaml: Secret api_bearer_token not defined

You need to create a token and add a secret for this as well.
See the REST API docs here for more info.

api_bearer_token created and added to secrets.
Config check passed but both

resource_hassio_main_config: http://my_domain.duckdns.org:8123/api/config

and

resource_hassio_main_config: http://my_domain.duckdns.org:8123/api

failed with

Logger: homeassistant.components.rest.data
Source: components/rest/data.py:66
Integration: rest (documentation, issues)
First occurred: 4:42:28 AM (1 occurrences)
Last logged: 4:42:28 AM
Error fetching data: http://[my_domain].duckdns.org:8123/api failed with

I don’t have split-dns set up to reolve duckdns to local IP and router does not support loop-back so I tried hassio.local and got name could not resolve so I tried 192.168.1.104 and which succeeded and created a sensor but it’s empty…

image

I changed to http://192.168.1.104:8123/api but now got a persistent notification:

Login attempt or request with invalid authentication from 192.168.1.104 (192.168.1.104) (Home Assistant REST sensor)

I tried just hassio and get



Logger: homeassistant.components.rest.data
Source: components/rest/data.py:66
Integration: rest (documentation, issues)
First occurred: 5:10:15 AM (1 occurrences)
Last logged: 5:10:15 AM
Error fetching data: http://hassio:8123/api/config failed with [Errno 111] Connect call failed ('172.30.32.2', 8123) 

using duckdns requires https://, as I posted in my secret :wink:
btw, you need to restart the instance for the config to read the new secrets.

I created split-dns entry in my DNS for [my_domain].duckdns,org A 192.168.1.104 and changed to https://[my_domain].duckdns.org:8123 but get:

Logger: homeassistant.components.rest.data
Source: components/rest/data.py:66
Integration: rest ([documentation](https://www.home-assistant.io/integrations/rest), [issues](https://github.com/home-assistant/home-assistant/issues?q=is%3Aissue+is%3Aopen+label%3A%22integration%3A+rest%22))
First occurred: 5:19:52 AM (2 occurrences)
Last logged: 5:20:32 AM

Error fetching data: https://[my_domain].duckdns.org:8123/api/config failed with

and also with https://[my_domain].duckdns.org:8123/api

Yes, I am restarting HA!

with https://192.168.1.104:8123

Logger: homeassistant.components.rest.data
Source: components/rest/data.py:66
Integration: rest (documentation, issues)
First occurred: 5:56:41 AM (1 occurrences)
Last logged: 5:56:41 AM
Error fetching data: https://192.168.1.104:8123/api failed with [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1124) 

Continuing today…

Insight and progress. I never did set my system up for SSL connections. Muddled my way through that. I had DuckDNS with LetsEncrypt but did not have:

http:
  base_url: https://[my_domain].duckdns.org
  server_port: 8123
  ssl_certificate: /ssl/fullchain.pem
  ssl_key: /ssl/privkey.pem

in configuration.yaml. That got my SSL up and running but REST was unable to resolve [my_domain].duckdns.org so now it’s DNS lookup issue. Used portainer to console into homeassistant to verify that. Query returned no response via nslookup. I’m running HA Supervised Debian Docker and after more googling found hosts file in /usr/share/hassio/dns, added [my_domain].duckdns.org to the end of 172.30.32.1 homeassistant homeassistant.local.hass.io home-assistant home-assistant.local.hass.io.
Now nslookup in homeassistant console session resolves [my_domain].duckdns.org to 172.30.33.1. Restarted and REST gave me a different error about JSON response format.
Added /config to resource_hassio_main_config: https://[my_domain].duckdns.org:8123/api
Now REST is giving me different errors:

Logger: homeassistant.components.http.ban
Source: components/http/ban.py:114
Integration: HTTP (documentation, issues)
First occurred: 3:35:42 AM (3 occurrences)
Last logged: 3:36:43 AM
Login attempt or request with invalid authentication from 192.168.1.104 (192.168.1.104) (Home Assistant REST sensor) 

followed by

Logger: homeassistant.components.rest.sensor
Source: components/rest/sensor.py:264
Integration: rest (documentation, issues)
First occurred: 3:35:43 AM (3 occurrences)
Last logged: 3:36:43 AM
REST result could not be parsed as JSON 

The saga continues…

Removed and recreated the api_refresh_token and now:

Logger: homeassistant.components.http.ban
Source: components/http/ban.py:114
Integration: HTTP (documentation, issues)
First occurred: 4:03:18 AM (4 occurrences)
Last logged: 4:04:48 AM
Login attempt or request with invalid authentication from [my_domain].duckdns.org (192.168.1.104) (Home Assistant REST sensor) 

More googling…
Added:

homeassistant:
  auth_providers:
    - type: trusted_networks
      trusted_networks:
        - 10.0.0.0/8
        - 172.16.0.0/12
        - 192.168.0.0/16
        - 127.0.0.1
    - type: homeassistant

but same errors…
I’ve abandoned the REST sensor! Cannot resolve DNS.

I’d like to see you lovelace yaml for this…

I have this so far…



for what exactly, you seem to have it show as desired?

Yes, I’ve gotten it close to what yours looks like! Curious though as to what code you have under your hood as the ‘look’ is different from my code. Only using HA here since June and I’ve learned a lot but…WANT TO LEARN MORE!!! I used a grid card and custom:button-card…

simple, a vertical stack, with a button for the title, and a horizontal stack for the counter buttons. Ever since the templating changes in 107( I think) I do not use these counters anymore so my card looks like this now:

type: vertical-stack
cards:

  - type: custom:button-card
    name: Ha Rpi4 counters
    template: button_default_title

  - type: horizontal-stack
    cards:

      - type: custom:button-card
        template: button_body
        entity: sensor.uun_ordered
        state:
          - operator: template
            value: >
              [[[
              return entity.state == 0;
              ]]]
            styles:
              state:
                - color: green
              icon:
                - color: green
          - operator: default
            styles:
              state:
                - color: red
              icon:
                - color: red

      - type: custom:button-card
        template: button_body
        entity: sensor.overview_entities
        styles:
          state:
            - color: green

      - type: custom:button-card
        template: button_body
        entity: sensor.overview_components
        styles:
          state:
            - color: green

      - type: custom:button-card
        color_type: blank-card

#      - type: custom:button-card
#        template: button_body
#        entity: sensor.entities_domains_counter
#        styles:
#          state:
#            - color: green

      - type: custom:button-card
        color_type: blank-card

Ive got space for 1 or 2 more buttons as you can see :wink:

Thanks! I’ll keep playing and tweaking! The REST issue is still in the back of mind all the time but I’ve figured out the python scripts previously discussed. Took me a while to piece them together.
Weird that REST can’t resolve the DNS name. Will keep thinking on it…

Here’s my domains sensor:

attributes = {}
count = 0
domains = []

for s in hass.states.all():
    res = str(s.domain)
    if res not in domains:
        domains.append(res)

count = len(domains)
domains.sort()

for d in domains:
    attributes[d] = len(hass.states.entity_ids(d))

attributes['friendly_name'] = 'Domains'
attributes['icon'] = 'mdi:domain'

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

and my entities:

count = 0
attributes = {}

for entity_id in sorted(hass.states.entity_ids()):
    count = count + 1
    attributes[entity_id] = ' '
### How to do this in python ###
#    attributes[entity_id] = {{ state_attr('entity_id','friendly_name') }}

attributes['friendly_name'] = 'Entities'
attributes['icon'] = 'mdi:format-list-numbered'

hass.states.set('sensor.overview_entities', count, attributes)
count_all = 0
domains = []
attributes = {}

for entity_id in hass.states.entity_ids():
    count_all = count_all + 1
    entity_domain = entity_id.split('.')[0]
    if entity_domain not in domains:
        domains.append(entity_domain)

attributes['Domains'] = len(domains)
attributes['--------------'] = '-------'

for domain in sorted(domains):
    attributes[domain] = len(hass.states.entity_ids(domain))

attributes['friendly_name'] = 'Entities'
attributes['icon'] = 'mdi:format-list-numbered'

hass.states.set('sensor.overview_entities', count_all, attributes)
1 Like

I FINALLY got the REST sensor working!
Combination of internal URL that hassio’s ha dns CAN resolve to an IP, SSL verify: false and correct API token (api_bearer_token: "Bearer " + ha_api_token)

sensors.yaml:

  - platform: rest
    name: Hassio Configuration
#    resource: !secret resource_hassio_main_config
    resource: https://homeassistant.local.hass.io:8123/api/config
    verify_ssl: false
    authentication: basic
    value_template: >
      {{ value_json.version }}
    json_attributes:
      - components
      - unit_system
      - config_dir
      - version
    headers:
      Content-Type: application/json
      Authorization: !secret api_bearer_token
      User-Agent: Home Assistant REST sensor

secrets.yaml:

ha_api_token: cyJ#eXAiOiJKV#QiLCJhbGciOiJIUzI#NiJ#.eyJpc#MiOiI#YzJjNDhmNThhNzE#N#VhOTQ#ZjhmMDc#Y#QwMDEzMSIsImlhdCI#MTYxMDEwNzMwOSwiZXhwIjoxOTI#NDY#MzA#fQ.#f#ph#FVBONDrKJoA#jP##zJFZmhkigzoG#adgePWQw

# resource_hassio_main_config: https://homeassistant.local:8123/api/config
api_bearer_token: "Bearer cyJ#eXAiOiJKV#QiLCJhbGciOiJIUzI#NiJ#.eyJpc#MiOiI#YzJjNDhmNThhNzE#N#VhOTQ#ZjhmMDc#Y#QwMDEzMSIsImlhdCI#MTYxMDEwNzMwOSwiZXhwIjoxOTI#NDY#MzA#fQ.#f#ph#FVBONDrKJoA#jP##zJFZmhkigzoG#adgePWQw
"

After reviewing the sensor info though, I really am no closer to getting a printed report of devices/entities configured by the OZW add-on…

the idea of secrets file is that you don’t have to post that sensitive info here in the community.

Not quote it :wink:
You should take out the secrets in your post above!