Count Lights and switches on

How can i Count switches and Lights in on state?

{{ states.light | selectattr('state', 'eq', 'on') | rejectattr('attributes.entity_id', 'defined')|  list | count }}

this counts the number of lights that are one. the “rejectattr” is there to not count groups… so that you don’t double count lights. but if you want to also count groups, remove that. do the same for switches…

{{ expand(states.light, states.switch) 
  | selectattr('state', 'eq', 'on') | list | count }}

If you want to exclude counting Light Groups and Switch Groups, employ armedad’s suggestion and add the rejectattr filter.

{{ expand(states.light, states.switch) 
  | selectattr('state', 'eq', 'on')
  | rejectattr('attributes.entity_id', 'defined')
  | list | count }}