Condition IF with group entity?

Hello,

How do I write a condition that checks a group of lights? This is what I have, and it doesn’t work with a group entity.

- id: front_lights_a
  alias: Front Lights A
  trigger:
  - at: '00:00'
    platform: time
  condition: []
- service_template: >
      {% if is_state('group.front_lights_group', 'on') %}
        script.front_lights_s
      {% endif %}

What I want to do is to NOT trigger the automation at 00:00 IF the group (front_lights_group) is on.

In my group, I have 3 lights:

  • bulb1
  • builb2
  • bulb3

Note:
script.front_lights_s is just a script that dims the lights for a few hours and turns them all off.

Thanks.

- id: front_lights_a
  alias: Front Lights A
  trigger:
    at: '00:00'
    platform: time
  condition:
    platform: state
    entity_id: group.front_lights_group
    state: 'off'
  action:
    service: script.front_lights_s

What do you mean “it doesn’t work”? are you getting errors or is the script just not being called?

However, tom’s suggestion is better anyway since it’s easier to read without the template.

I imagine it would have been errors since specifying a service template at that position is not valid.

You’re right. I totally missed that there was no “action:” there.

OP - try putting in “action:” above the “service_template:” line

Then there’s the missing else statement.

that’s not 100% required so I doubt it would cause it to not work. It will just print an error in the log that it couldn’t find a valid service call when the template is evaluated.

Sure but there really is no need for a template that works but generates errors sometimes when instead a simple condition can handle the logic required without ever causing errors.

No argument on that here. I was just pointing out the facts.

I already said your way was better. :wink:

1 Like

Sorry for the late response. It does look simpler, I agree. Thanks, guys! I’m not sure what I was thinking why I was over complicating the template.

About the error I was getting, it kept telling me something like invalid. But I was only getting this error with a group, but not with an entity.

You should have received an error for group or any other entity. You had no action: block in your automation and the service template is not indented correctly. Maybe you accidentally deleted that when editing the entity?

FYI this is what it should have looked like:

- id: front_lights_a
  alias: Front Lights A
  trigger:
  - at: '00:00'
    platform: time
  condition: []
  action:
  - service_template: >
      {% if is_state('group.front_lights_group', 'on') %}
        script.front_lights_s
      {% endif %}

Though as pointed out above, because there is no else statement this will generate errors when the template evaluates as false (no entity id will be specified for the service call).