Automation with multiple variables

Hi everyone,

I’d like my outside lights to turn on when an entire list of conditions is right, and I don’t know were to start.

So I’d like to have lights on outside if the outside luminance sensor is below a threashold, any of the downstairs lights or the TV is on, and also the window blinds should either be open or if closed, tilted to be “see through”.
Also I guess all of this should be a trigger, so that if any of these conditions change, say I close the blinds, the lights should turn off.

so do I need to trigger on all of these entities and also add them as conditions?, is there a way to just “toggle” the lights if the conditions are(n’t) met or do I need to have two, almost identical automations one for on and one for off?

thanks for any pointers!

You can add multiple triggers and give them an ID so later on you can use if then or choose to decide what todo. 1 automation should be enough.
I would advise to draw this out on paper on how you want it to flow.

Both, depending on your comfort level with templates.

If you are comfortable with templates you can make one large template trigger that evaluates to true only when all your conditions are met. And then your automation just has a single action: turn the lights on.

If you aren’t very comfortable with templates then you’ll need both. First you need to add triggers for all the entities that could cause your ligthts to turn on. So you’ll need a numeric state trigger for the luminance sensor, a state trigger for the downstairs lights, the tv and the blinds. And a state trigger that watches an attribute for the “blinds being closed but see through” part (I assume, not as sure how that one works).

Then write all out all your conditions in the condition part of the automation. So essentially your condition only evaluates to true if:

  1. illuminance is below a threshold
  2. AND (downstairs lights are on OR tv is on)
  3. AND (window blinds are open OR window blinds tilted to be see through)

The action part of your automation is still just one step - turn on the lights. The key is making sure it only gets to that when all your conditions are met and ensuring the automation evaluates your condition every time an entity that could cause the lights to turn on in changes.

So I’ve created two automations: one for turning the lights on one for off.
they both use a template trigger with mostly the same code. Just for the off variant, there’s a not to negate the final result.

here’s the Template, for anyones reference:

{% set ns = namespace(triggered=false) %}
{% set lightOrTvOn = is_state('light.eg','on') or is_state("media_player.tv",'on') %}
{% for entity in expand('cover.downstairs') %}
{% set cover = {
  "tilt": state_attr(entity.entity_id, "current_tilt_position"),
  "position": state_attr(entity.entity_id,'current_position'),
}
%}
{%if cover.position > 30 or (cover.tilt > 20 and cover.tilt < 60)%}
{#a cover is open/see through #}
{% set ns.triggered = ns.triggered or (lightOrTvOn and 400 > int(states("sensor.outsideIlluminance")))%}
{% endif %}
{%endfor%}
{{ns.triggered}}

FYI I believe you can simplify that to this:

{% set covers = expand('cover.downstairs') %}
{{ (covers | selectattr('attributes.current_position', 'gt', 30) | list | count > 0 or
    covers | selectattr('attributes.current_position', 'le', 30)
      | selectattr('attributes.current_tilt_position', 'in', range(21, 60)) | list | count > 0
  ) and (is_state('light.eg','on') or is_state("media_player.tv",'on'))
  and 400 > states("sensor.outsideIlluminance") | int }}
1 Like

Awesomeness :sunglasses: im so glad i shared my solution to get your much more elegant improvement back.
Thank you - What a great community! Id been looking at these filters but just couldn’t put it together like this. This will definitely help me with future templates.

1 Like

Just a quick note, I’ve added a | list For before the | count > 0

Without it I’m getting a typeError: “object of type generator has no len()”

Oh damn, yea that’s probably necessary. I thought I had tested that in the template editor but maybe something got lost moving from there to the forum.

I keep hoping someday Jinja will make count do that implicitly but I guess today is not that day.