Guidance with Groups of Binary Sensors

Can both the group and the template me named the same thing?

Log Details (ERROR)
Wed Sep 04 2019 17:18:01 GMT-0500 (Central Daylight Time)
Invalid config for [sensor.template]: value is not allowed for dictionary value @ data['sensors']['window_status']['device_class']. Got 'window'. (See ?, line ?). Please check the docs at https://home-assistant.io/components/sensor.template/

Same error

Ok working with this now thanks!

Log Details (ERROR)
Wed Sep 04 2019 16:45:22 GMT-0500 (Central Daylight Time)
Invalid config for [sensor.template]: value is not allowed for dictionary value @ data['sensors']['window_status']['device_class']. Got 'window'. (See ?, line ?). Please check the docs at https://home-assistant.io/components/sensor.template/


Log Details (ERROR)
Wed Sep 04 2019 16:32:04 GMT-0500 (Central Daylight Time)
Invalid config for [sensor.template]: [window_status] is an invalid option for [sensor.template]. Check: sensor.template->window_status. (See ?, line ?). Please check the docs at https://home-assistant.io/components/sensor.template/
Connection lost. Reconnecting…

Two errors for:

groups.yaml

group:
  name: windows
  entities:
    - binary_sensor.back_window_2_contact
    - binary_sensor.bedroom_window_3_contact
    - binary_sensor.dining_room_contact
    - binary_sensor.front_bedroom_contact
    - binary_sensor.front_window_1_contact
    - binary_sensor.front_window_2_contact
    - binary_sensor.nursery_window_1_contact
    - binary_sensor.nursery_window_2_contact
    - binary_sensor.office_window_1_contact
    - binary_sensor.office_window_2_contact

configuration.yaml

sensor:
  - platform: template
    sensors:
      window_status:
        friendly_name: "Window Status"
        entity_id:
          - binary_sensor.back_window_2_contact
          - binary_sensor.bedroom_window_3_contact
          - binary_sensor.dining_room_contact
          - binary_sensor.front_bedroom_contact
          - binary_sensor.front_window_1_contact
          - binary_sensor.front_window_2_contact
          - binary_sensor.nursery_window_1_contact
          - binary_sensor.nursery_window_2_contact
          - binary_sensor.office_window_1_contact
          - binary_sensor.office_window_2_contact
        device_class: window
        value_template: "{{ expand('group.window') | selectattr('state','eq','on') | list | count > 0 }}"

try binary_sensor instead of sensor as I’ve shown above

I use split config so you’ll need to use it like this if it is in your configuration.yaml

binary_sensor:
  - platform: template
    sensors:
      window_status:
        friendly_name: "Window Status"
        entity_id:
          - binary_sensor.living_room_window
          - binary_sensor.kitchen_window
          - binary_sensor.toilet_window
          - binary_sensor.bathroom_window
          - binary_sensor.ensuite_window
          - binary_sensor.office_window
          - binary_sensor.bedroom_window
          - binary_sensor.alices_window
          - binary_sensor.bens_window
        device_class: window
        value_template: "{{ expand('group.windows') | selectattr('state','eq','on') | list | count > 0 }}"

Also, where are you defining the group ? in groups.yaml ?

And, check your spelling of the group in the value_template it has to match the group name !

For starters, you’re not defining the group correctly. name is optional and therefore is not used to create the group’s entity_id.

If you do this:

group:
  windows:
    name: All Windows
    entities:
    - binary_sensor.back_window_2_contact
    - binary_sensor.bedroom_window_3_contact
    - binary_sensor.dining_room_contact
    - binary_sensor.front_bedroom_contact
    - binary_sensor.front_window_1_contact
    - binary_sensor.front_window_2_contact
    - binary_sensor.nursery_window_1_contact
    - binary_sensor.nursery_window_2_contact
    - binary_sensor.office_window_1_contact
    - binary_sensor.office_window_2_contact

its entity_id will be group.windows and not group.all_windows.

If you are using a separate yaml file for all groups then the first line containing group: is not needed.

Thats whats in the value_template:

value_template: "{{ expand('group.windows') | selectattr('state','eq','on') | list | count > 0 }}"

It’s getting hard to follow this thread; I was referring to this post by Literoya where the group is defined as Contact Sensors: and then is referred to by its name.

Also, check out the latest reported errors. There should be no complaint about the device_class yet there it is. Roya is doing something out of the ordinary to get that error message.

2019-09-04_17h57_07

And we are good to go!

Thanks for this learning experience. I am going to be using this for other groups as well.

Thanks to @Holdestmade, @123 and @petro for all the help!

In your previous post you stated you still received two errors. What did you change to eliminate them?

So @Holdestmade pointed out that I had a typo when referring to my group. I used window and windows. I changed it to be the same group name of windows.

The other fix was changing the sensor to binary_sensor and splitting it out.

sensor:

  # Weather prediction
  - platform: yr
  - platform: google_wifi
  
  - platform: min_max
    entity_ids:
      - sensor.living_room_nest_thermostat_temperature
      - sensor.family_room_temperature
    type: mean
    name: Ground Temp
    
  - platform: min_max
    entity_ids:
      - sensor.office_temperature
      - sensor.nursery_temperature
      - sensor.master_bedroom_temperature
    type: mean
    name: Upstairs Temp
    
  - platform: min_max
    entity_ids:
      - sensor.workshop_temperature
      - sensor.playroom_temperature
    type: mean
    name: Downstairs Temp

binary_sensor:
  - platform: template
    sensors:
      window_status:
        friendly_name: "Window Status"
        entity_id:
          - binary_sensor.back_window_2_contact
          - binary_sensor.bedroom_window_3_contact
          - binary_sensor.dining_room_contact
          - binary_sensor.front_bedroom_contact
          - binary_sensor.front_window_1_contact
          - binary_sensor.front_window_2_contact
          - binary_sensor.nursery_window_1_contact
          - binary_sensor.nursery_window_2_contact
          - binary_sensor.office_window_1_contact
          - binary_sensor.office_window_2_contact
        device_class: window
        value_template: "{{ expand('group.windows') | selectattr('state','eq','on') | list | count > 0 }}"

The typo clearly needed to be fixed but changing from Template Sensor to Template Binary Sensor shouldn’t make any difference with regards to the error messages. Petro provided a valid configuration for a Template Sensor and I use a similar one without the error message(s) you had.

I have a feeling the next time you attempt to define a Template Sensor, this problem will re-appear.

Kinda makes more sense to use a Binary Sensor if it will be a Binary Sensor ?

1 Like

Can’t argue with that … but changing it to a Template Binary Sensor doesn’t explain why it originally failed when it was defined as a Template Sensor.

I carried out an experiment and was able to replicate the error message Literoya received. The answer is actually quite simple. Although both Template Sensor and Template Binary Sensor support the device_class option, they don’t share the same types of device_class.

The available types for a sensor are listed here:


What you won’t find in that list is window. That’s a type of device_class only valid for binary_sensors.

That’s why switching from Template Sensor to Template Binary Sensor eliminated the error message. A window device_class is valid for binary_sensors, not sensors.

1 Like

I offer you yet another way to achieve the same thing.

In this version, you specify the binary_sensors you wish to monitor (windows) within the value_template. You don’t need a group and you don’t need to list the binary_sensors in entity_id.

binary_sensor:
  - platform: template
    sensors:
      window_status:
        friendly_name: 'Window Status'
        device_class: window
        value_template: >
          {% set sensors = [states.binary_sensor.living_room_window,
                            states.binary_sensor.kitchen_window,
                            states.binary_sensor.toilet_window,
                            states.binary_sensor.bathroom_window,
                            states.binary_sensor.ensuite_window,
                            states.binary_sensor.office_window,
                            states.binary_sensor.bedroom_window,
                            states.binary_sensor.alices_window,
                            states.binary_sensor.bens_window] %}
          {{ sensors | selectattr('state','eq','on') | list | count > 0 }}
6 Likes

So i am trying to do as much as i can looking at what others are doing since i am a newbie. However what am i doing wrong i have what he has but i am getting an error. in my groups file this is everything i have at this point. Please, what am i missing?

Error

Error loading /config/configuration.yaml: expected '<document start>', but found '<block sequence start>'
  in "/config/groups.yaml", line 2, column 3


    binary_sensor:
  - platform: template
    sensors:
      window_status:
        friendly_name: "Window Status"
        entity_id:
          -binary_sensor.basement_slider_door
          -binary_sensor.front_door_window_sensor
          -binary_sensor.Landon_window_sensor
          -binary_sensor.girls_bedroom_window_sensor
          -binary_sensor.Middle_slider_door_sensor
          -binary_sensor.Master_bedroom_window_sensor
          -binary_sensor.hall_window_sensor
          -binary_sensor.dinning_room_window_sensor
          -binary_sensor.Basement_window_sensor
          -binary_sensor.Entry_Door
        device_class: windows
        value_template: "{{ expand('group.windows') | selectattr('state','eq','on') | list | count > 0 }}"

It’s not formatted properly. Compare the first example in the documentation to what you posted above. Look at how each line is indented.

# Example configuration.yaml entry
binary_sensor:
  - platform: template
    sensors:
      sun_up:
        friendly_name: "Sun is up"
        value_template: >-
          {{ state_attr('sun.sun', 'elevation')|float > 0 }}

Got it thanks!

also… home assistant is case sensitive and entity_id’s never have capitals in them. You’re using capitals all over the place.

1 Like

Thank you! Very nice way to group (with OR) binary sensors.

How can the status of all those sensors be grouped in a way the individual sensors don’t need to be in the config, but instead the template includes all binary_sensors automatically? This would be based on this automation, but than inside a template. I can’t get my head around this how to do this.

EDIT: I mean something like this, which doesn’t seem to be working, as it reads unavailable, while all my sensors are online.

    lekkage_sensors:
        friendly_name: "Lekkage sensors"
        value_template: >-          
          {% if is_state.entity_id.startswith('binary_sensor.','unavailable') %}
            Sensor offline
          {% else %}
            Sensors online
          {% endif %}