List all switch entities

First: i’m not a coder, but i try to make stuff
I’m working on getting all my stuff into imperihome and dont want to do it all maually.
Is there a way to list all entity_id’s of switches ? (and the same for sensors)

Open the “hamburger menu” (i.e., image) in the upper left of your main page. Then in the developer tools select the “states” icon (image). Then uncheck the Attributes box.

That gets a list of all of the entity_id’s in your system. You will see all of the switches start with switch.

You don’t say what you want to do with the list of entity_id’s. If you’re going to use them in a template sensor, or send them in a notification, etc., and you can use a template, then the following template will create a list of all switch entity_id’s:

{{ states.switch|map(attribute='entity_id')|list }}

Or if you want a comma separated string instead:

{{ states.switch|map(attribute='entity_id')|join(', ') }}
4 Likes

Just a quick one - why

{{ states.switch|map(attribute='entity_id')|list }}

works, but

{{ states.zwave|map(attribute='node_id')|list }}

doesn’t?
It returns

[Undefined, Undefined, ...., Undefined]

BTW, I actually need a list of all my zwave entities in AppDaemon but came across this thread.
An answer to both will be a bonus :smiley:
Thanks!

I don’t know why it doesn’t work, but do you really need the node_id? It will just be a list of numbers.

{{ states.zwave|map(attribute='entity_id')|list }}

Will give you a list of the entity ids of all zwave. Entities.

Because HA is very bad about consistency of terms. Try this:

{{ states.zwave|map(attribute='attributes.node_id')|list }}
2 Likes

Do you mean all your zwave.xxx entities? Or all the other entity types associated with zwave.xxx entities (like switch.xxx, light.xxx, etc.)? For the former use @Burningstone’s suggestion. For the latter, uh … good question!

All entities from a zwave device have an attribute “node_id”, but I’m not sure if this exclusive to zwave. @Qua3952 could you please explain what you want to do in AppDaemon? And also how do you plan to use the template in AppDaemon? I don’t think that this is possible.

Thank you @pnbruckner and @Burningstone very much.
For long time I have many sorts of updating issues between real state of zwave switches and state on HA web. Recently I’ve found that if I refresh the zwave node in question (every time a different node, always only lights), the situation solved for a while. So I thought of running such refresh (zwave.refresh_node service call which expects the node_id as a parameter) every night for each zwave node. The reason for AppDaemon is that I’m trying to set all my automations in one place. I hope it answers all questions above.
Any opinion on the above is more than welcome.

Are you sure your problem isn’t due to a lack of proper configuration? E.g., do you have Z-Wave dimmers? If so, have you set refresh_value and delay for them? Have you set polling_intensity when required? You might want to check out Z-Wave Configuration if you haven’t.

no dimmers… I also haven’t define any polling_intensity which maybe I need to check, but on the other hand, some zwave switches never fail, while others do every now and then, sometimes more now than then and other times the other way (that was a complicated one : )
Besides, when the switch is in that state, it also much less responsive to commands. I’m using Fibaro, if of interest.

There’s a lot of detail behind it, but basically, some devices need to be polled while others don’t. If HA is out of sync with some devices you might want to enable polling for them.

By ‘some devices’ can it be two of the same type (i.e. both are Fibaro FGS-223) that one will need the polling and the other won’t? Logically it doesn’t make sense, but from my experience with Fibaro from one hand and zwave networking on the other hand, I won’t be surprised…

Phil / @Burningstone
I found this really quite interesting, I’ve used it to create a notification (in home speaker) to say that ‘someone’ has just arrived home.
Basically (using the examples given) I’ve created an automation with the following as a condition (there’s no point alerting unless there is someone already home)

    condition:
      - condition: template
        value_template: "{{ states.device_tracker | map(attribute='home') | list | count > 1 }}"

It seems a bit of a catch all and thus there may be a cleaner and more efficient way of doing this, your thoughts welcome.

Just to close the loop, if serves anyone, here is my AppDaemon code for nightly refreshing the zwave light nodes:

import appdaemon.plugins.hass.hassapi as hass
import datetime

class zwave_refresh(hass.Hass):

  def initialize(self):
    self.utils = self.get_app("utils")   
    self.show_log = self.args["show_log"]
    
    self.run_daily(self.run_daily_callback, datetime.time(2, 0, 0))
    
    
  def run_daily_callback (self, kwargs):  
    zwaves = self.get_state("zwave")
    self.utils.mylog("init zwave_refresh", self.show_log, self.show_log)
    t = 0
    for entity, v in zwaves.items():
        if entity.startswith("zwave.light_"):
            self.utils.mylog(entity + ": " + str(v["attributes"]["node_id"]), self.show_log, self.show_log)
            self.run_in(self.timer_callback, t, node_id=v["attributes"]["node_id"], node_name=entity)
            t = t+180
            
  def timer_callback (self, kwargs):
    self.call_service("zwave/refresh_node", node_id=kwargs["node_id"])    
    self.utils.mylog("refreshing: " + kwargs["node_name"], self.show_log, self.show_log)

comments:

  1. all my zwave lights start with ‘zwave.light_’.
  2. self.utils.mylog is my implementation for self.log
2 Likes

is it possible to get list of entitiy_id of media_player where state eq different values?
I found a way how to get a list where state eq one value:

{{ states.media_player | selectattr('state', '==', 'playing') | join(', ', attribute='entity_id') }}

But i dont know how get a list where state eq playing or paused.

There might be a more elegant way to do this, but at least the following should work:

{{ (states.media_player | selectattr('state', '==', 'playing') | list +
    states.media_player | selectattr('state', '==', 'paused') | list)
   | join(', ', attribute='entity_id') }}

This should provide a list of playing and paused media players, in the more elegant way @pnbruckner suggested.

{{ states.media_player | selectattr('state','in',['playing','paused']) | map(attribute='entity_id') | list }}
2 Likes

Thanks! That is great!

1 Like