Trying to figure out how to work multiple conditions and multiple lights in 1 automation

That’s the expected results of toggle.

Maybe try:

  action:
  - service: light.turn_on
    data_template:
      entity_id: light.stair_top,light.stair_bottom
      brightness_pct: >
        {%- if now().hour >= 6 and now().hour <=21 %}
          254
        {% else %}
          64
        {% endif %}

That would get the light turned on… I think maybe… a combination of or’ed and and’ed conditions to check light state and either turn on or turn off and set time conditions to set the brightness…

God this is going to hurt my head!

Isn’t that what you want?

This does exactly what you’re looking to do, I think, unless I’m missing your object.
image
image

No… my goal is to get in bed and click the switch to turn the lights off or on. And depending upon the time of day set a given brightness level to the lights.

This works, but gives me no brightness control.

- alias: 'Single Click 21 to toggle stair lights'
  trigger:
    platform: event
    event_type: click
    event_data:
      entity_id: binary_sensor.switch_158d0001f3de21
      click_type: single
  action:
    service: light.toggle
    data:
      entity_id: light.stair_top,light.stair_bottom

The tempate above was to give me the brightness control during the deep night hours so I do not turn a light on to 850 lumens right outside the kids room. But, I do see where toggle does not support setting brightness.

Again I’m pretty sure you can’t have 2 different services in one automation. I don’t think what you want is possible. Again I suggest getting into chat on Discord with Dale.

What is discord? I am serious!

So I found it! Trying to figure it out now!

Thanks

Home assistant live chat

Thanks, yes I found the right doc on HA and managed to connect and did find the forum, if that is what it is called on there.

Is the preferred place for rookies on here or there? It looks like on here first and then there… to me.

It’s just a hotline to the best support and it’s immediate. I use both. Especially for an issue like this talking with some of the Devs is great. Dale is an expert at automations and if he’s there he’ll fix you up.

Thanks, sent a PM… bet he is in bed will check at a more reasonable hour on Wednesday.

Then toggle is definitely not going to work.

Try this:
Create a group called XXX with light.stair_top & light.stair_bottom

action:
  - service_template: >
      {% if is_state('group,XXX', 'on') %}
        light.turn_off
      {% elif now().hour < 6 %}
        light_.turn_on
        brightness_pct: 64
      {% elif now().hour < 22 %}
        light.turn_on
        brightness_pct: 255
      {% endif %}
    data:
      entity_id: group.XXX

Because light.toggle does not accept a brightness, you have to use a condition to check if the light is on after the toggle service is called, and then call light.turn_on with the time-based brightness.

Here is a working approach that still uses just one automation:

1 Like

I think a possible solution here would be to have 2 scripts. An on script and an off script. Have both scripts accept variables.

    script:
      turn_light_on:
        sequence:
          - service: light.turn_on
            data_template:
              entity_id: {{ myentity }}
              brightness: {{ mybrightness }}

      turn_light_off:
        sequence:
          - service: light.turn_off
            data_template:
              entity_id: {{ myentity }}

Then your action would be:

    action:
      - service: script.turn_on
        data_template:
          entity_id: >
            {% if is_state('light.stair_top', 'on') %}
              script.turn_light_off
            {% else %}
              script.turn_light_on
            {% endif %}
          variables:
            # You can change this variable to make this whole thing dynamic if you feel like it
            myentity: light.stair_top 
            mybrightness: >
              {%- if now().hour >= 6 and now().hour <=21 %}
                {{254}} 
              {% else %}
                {{64}}
              {% endif %}

@petro This is precisely what my light_control package accomplishes: https://github.com/dale3h/homeassistant-config/blob/3a67147a964f093fae209f8ea9ee1759a5421b39/packages/drop-in/light_control.yaml

However, for this one implementation, it is not necessary to have multiple scripts plus one automation. Also, it is worth mentioning that one limitation to using scripts is that in the rare instance that the automation tries to fire twice simultaneously, you will get a “Script is already running” warning, preventing the second firing of the automation to fail.

There are a few minor changes that need to be made for your config to work properly, and a couple of others that I would recommend for “best practices.”

Required Changes

  1. Use double quotes around templates (see #3 on https://www.home-assistant.io/docs/automation/templating/#important-template-rules)

Recommended Changes

  1. Use >- instead of > on entity_id: and brightness:
    • While it is acceptable to use whitespace control inside the template itself (i.e. {%-, -%}), it is a much cleaner approach to just use >- when you have only one output segment. If you had multiple if statements in the same template that each output something, it would then be recommended to use whitespace control inside of the template itself.
  2. Remove Jinja2 output delimiters from before and after the brightness values (254 and 64)

TIL, I thought the interpreter would remove all leading and trailing whitespace

I only recommended this because he was getting errors in regards to setting the value. The error was explicitly returning a string of the integer instead of the integer. I wasn’t sure if that was causing the problem so I was trying to have it force the object as an integer.