Determine if Lights are On and How Many (Sensors)

Somewhat new to HA and one of the main things I wanted was a way to track how many lights are on in my house. Most of my lights are on z-wave switches (zooz) and various z-wave plugs. By having a sensor for how many lights are on I can easily tell if I left something on in the house and then run a scene to turn them off.

There are many ways you can do this, and I saw various approaches, but wanted to share the final version of what I came up with…

First, a tip for keeping your configuration files clean. You can create a separate yaml file for your templates by adding the line template: !include templates.yaml in your configuration.yaml file and then creating a new templates.yaml file. (I used the add-on File Editor to do this)

In the templates.yaml file, I created a binary sensor and general sensor, one to show if any lights are on, the other to count how many lights are on:

    - sensor:
        - name: "# Lights On"
          state: >
                 {% set reject = ['light.plug_in_1_outlet_dimmer_500s_2', 'light.plug_in_1_outlet_dimmer_500s', 'light.kitchen_sconce'] %}
                 {{ states.light | rejectattr('entity_id', 'in', reject) | selectattr('state','equalto','on')| list | length }}
    - binary_sensor:
        - name: "Any Lights On"
          state: >
                 {% set reject = ['light.plug_in_1_outlet_dimmer_500s_2', 'light.plug_in_1_outlet_dimmer_500s', 'light.kitchen_sconce'] %} 
                 {{ states.light | rejectattr('entity_id', 'in', reject) | selectattr('state','equalto','on')| list | length > 0 }}
        

These are setup to count any light entities that are set to on. You can specify entities in the set reject string to ignore them (in my use case, I do this to exclude 3 lights that I use as nightlights on automations that I do not want to count as lights being on).

Finally, if you have any devices that do not show up as light entities (the entity does not start with “light.”), you should create Helpers to change the device type (I had a few that were switches without doing this and thus would not be counted by the code above). This can be done via the UI Settings → Devices & Services → Helpers.

Now restart home assistant and you will have new sensors you can use in your automations or display on your dashboards.

I actually go a step further and combine this with a Zooz ZEN32 Scene Controller at my garage door entrance to turn the LED indicator to RED if there are any lights on in the house. Then all I have to do is press that button and it automatically turns off all the lights in the house via a Scene Automation.

1 Like