Binary sensor for when any within list of other sensors are on

This seems really simple and I feel quite dumb asking…but yet here I am

I have 13 irrigation zones that each have their own binary sensor in HA. I would like to have a single binary sensor that tells me when any zone is on. Would it have to be a brutish 13 case if else template or is there a better way? Also while I’m here, is there a better way to define my irrigation zones? It’s a similar situation where I’m manually doing each one where I’d rather have some sort of for loop to do it. Currently looks like this: (grabs state data from irrigation software JSON)

binary_sensor:
    # Irrigation zones
  - platform: rest
    name: irrigation_zone1
    resource_template: http://localhost:88/json/zones
    value_template: '{{ value_json["zones"][0]["state"] }}'
    scan_interval: 10    
  - platform: rest
    name: irrigation_zone2
    resource_template: http://localhost:88/json/zones
    value_template: '{{ value_json["zones"][1]["state"] }}'
    scan_interval: 10 
  - platform: rest
    name: irrigation_zone3
    resource_template: http://localhost:88/json/zones
    value_template: '{{ value_json["zones"][2]["state"] }}'
    scan_interval: 10 
  - platform: rest
    name: irrigation_zone4
    resource_template: http://localhost:88/json/zones
    value_template: '{{ value_json["zones"][3]["state"] }}'
    scan_interval: 10    
  - platform: rest
    name: irrigation_zone5
    resource_template: http://localhost:88/json/zones
    value_template: '{{ value_json["zones"][4]["state"] }}'
    scan_interval: 10 
  - platform: rest
    name: irrigation_zone6
    resource_template: http://localhost:88/json/zones
    value_template: '{{ value_json["zones"][5]["state"] }}'
    scan_interval: 10 
  - platform: rest
    name: irrigation_zone7
    resource_template: http://localhost:88/json/zones
    value_template: '{{ value_json["zones"][6]["state"] }}'
    scan_interval: 10 
  - platform: rest
    name: irrigation_zone8
    resource_template: http://localhost:88/json/zones
    value_template: '{{ value_json["zones"][7]["state"] }}'
    scan_interval: 10 
  - platform: rest
    name: irrigation_zone9
    resource_template: http://localhost:88/json/zones
    value_template: '{{ value_json["zones"][8]["state"] }}'
    scan_interval: 10 
  - platform: rest
    name: irrigation_zone10
    resource_template: http://localhost:88/json/zones
    value_template: '{{ value_json["zones"][9]["state"] }}'
    scan_interval: 10    
  - platform: rest
    name: irrigation_zone11
    resource_template: http://localhost:88/json/zones
    value_template: '{{ value_json["zones"][10]["state"] }}'
    scan_interval: 10 
  - platform: rest
    name: irrigation_zone12
    resource_template: http://localhost:88/json/zones
    value_template: '{{ value_json["zones"][11]["state"] }}'
    scan_interval: 10 
  - platform: rest
    name: irrigation_zone13
    resource_template: http://localhost:88/json/zones
    value_template: '{{ value_json["zones"][12]["state"] }}'
    scan_interval: 10 

That’s where Group is for :point_right: Group - Home Assistant

By default when any member of a group is on then the group will also be on. Similarly with a device tracker, when any member of the group is home then the group is home. If you set the all option to true though, this behavior is inverted and all members of the group have to be on for the group to turn on as well.

Perfect. TIL about groups :slight_smile: Thank you for your help

1 Like

There’s no facility in Home Assistant to generate YAML code. In other words, there’s nothing to crank out YAML for 13 nearly identical binary_sensors.

Your best bet is to adopt the new ‘modern’ way of defining RESTful sensors. It eliminates duplicated options (like resource_template and scan_interval which occur for each one of your binary_sensors) by consolidating them. Here’s a partial example of your ‘legacy’ style converted to ‘modern’ style:

rest:
  - resource: 'http://localhost:88/json/zones'
    scan_interval: 10
    binary_sensor:
      - name: irrigation_zone1
        value_template: '{{ value_json["zones"][0]["state"] }}'
      - name: irrigation_zone2
        value_template: '{{ value_json["zones"][1]["state"] }}'
      - name: irrigation_zone3
        value_template: '{{ value_json["zones"][2]["state"] }}'
      - name: irrigation_zone4
        value_template: '{{ value_json["zones"][3]["state"] }}'
      ... etc ...

Oh very nice, learned something else there. That is an improvement for sure. Thanks!