Guidance with Groups of Binary Sensors

Surely you need to keep the group as the value_template references it ?

I figured group.window_status made a group from the entities listed in the window_status?

Yes, the group is needed but without it the template will simply fail quietly. The weird thing is that deleting group.yaml caused a seemingly unrelated error message concerning the template sensor’s device_class. That’s a puzzling result and indicative that there’s something else happening here that hasn’t been revealed yet.

No it doesn’t. You must explicitly define the group if you intend to refer to it within the template.

I’ll repeat my previous question, do you have other sensors defined in configuration.yaml or is this Template Sensor the only one?

Also, wouldn’t a template binary sensor be better suited here ?

Trying again!

groups.yaml
2019-09-04_17h14_54

Contact Sensors:
  name: window status
  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_status') | selectattr('state','eq','on') | list | count > 0 }}"

Error Log

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/

I included all my sensor code block just in case but I don’t see any conflicts there.

group needs to be:

group:
  name: window status
  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

OK so done this myself and works fine (note slightly different group name as I already have that group)

Binary Sensor

  binary_sensor:
    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 }}"

Group

windows:
  name: Windows
  view: false
  entities:
  - 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

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