Not triggering automation if trigger is caused by another automation

Hi there,

I’m completely new to Home Assistant and I’m looking for the most elegant way to solve this.

I have a “TV Mode” automation, it is triggered by an input_boolean.tv_mode. It dims some lights in the living room and hallway and closes one of my covers. The input boolean for TV Mode should be switched off as soon as any of the living room or hallway lights change brightness or are turned off.

I wrote a “TV Mode Stop” automation that turns off the input boolean if those lights change brightness or are turned off. Now obviously whenever I turn on the TV Mode, the lights are dimmed which is then triggering the TV Mode Stop right away as well.

I know I can get around that various ways but all I can think of seems overly complicated. Is there an elegant way to not run an automation if the state change that is triggering it is caused by another automation?

I would take the actions from your tv mode and put them into a script instead. To activate “TV mode” I would use a button on a dashboard that calls the script. The last action in that script would be to turn on the “TV mode” input_boolean.

Essentially your “TV mode” boolean becomes a status indicator only, not a dual purpose status and command.

If you want to be able to turn it off you could set up your script so that it performs different actions depending on if the Boolean is on or off. Then the button would toggle the state.

If you don’t want buttons, you could just create another input Boolean. One would be “tv mode status” and the other would be “tv mode command”

the trigger.event should contain an indication of what caused the trigger to fire (context, user). check that variable on the docs.

I’m already using a button and that’s the issue. The TV Mode Stop doesn’t perform any action apart from turning off the boolean.

Here’s a typical scenario:

I want to watch a movie so I tap the “TV Mode” button on my dashboard, the lights dim down. After I’ve finished watching I don’t want to remember to manually turn it off every single time. Instead I wanna be able to get up from my couch, turn the lights to 80% and the input boolean should be automatically turned off because I’m obviously done watching. So when I come back to my couch 8h later the state is consistent and I do not have to then double tap to first turn off (because I forgot before) and turn on again to trigger the TV Mode script.

What I suggested would do that. Perhaps I didn’t explain it well enough. My suggestion of moving the actions to the script also eliminates the trigger that you had, which was triggering tv mode when the toggle was turned on. A little more detail:

  1. Have a script called “tv_mode_script” that has all the actions that dim the lights and set the blinds etc.
  2. Have a button on your dash called “start TV mode” that runs the script.
  3. You can stop at this point if you don’t need a status indicator of whether “tv mode” is currently on or off. For example I have a bedtime script that turns off lights and locks doors, but HA doesn’t know or care if anyone changed anything after I run the script. So ask yourself “does this really need to be stateful?”
  4. If you need it to be stateful, create an input_boolean called “tv mode status”
  5. As the very last action in the script from item #3, turn the “tv mode status” input_boolean to “on”. You may also need a 1 or 2 second delay immediately prior to this action.
  6. Create an automation that is triggered by the state changing of any entity that is controlled in your script. The action would be to run the inout_boolean “off”. Make sure this automation mode is set to “restart”.

Now, when you use the button on your dash, the script will be launched and lights will start dimming. This will trigger your automation to turn off the tv mode toggle, but it’s already off anyway. Once the script completes, it will then toggle it “on”. Later on, when you turn up the brightness on your lights, the automation will run and toggle the tv mode switch to “off”.

Note that you should not toggle the switch manually. It is a status indicator, not an actual switch.

If you want the toggle to be read-only, you could create a trigger-based binary template sensor in YAML, and use custom events to change its state.

——-

Option #2
Keep what you already have, and use the context as a condition to halt the automation if the entities were changed by an automation. Detail is here; you’d want to check that {{ trigger.to_state.context.user_id == none and trigger.to_state.context.parent_id != none }}

However if you are in tv mode and you trigger some other automation to change the lights (for example you add a bedtime script to turn off all your lights), the tv mode won’t turn off.

—-
There are still many more options you could pursue.

Got it, thanks for your explanation.
You are using a button and a status indicator - I was using just a button and wanted it to display the status as well. I’ve ended up with this solution partly based on your suggestions but having both status indicator and toggle button in one button. If you have the time please let me know if there’s anything to improve on - I’m still learning.

Script

alias: TV Mode
sequence:
  - repeat:
      for_each:
        - light.living_room
        - light.hallway
      sequence:
        - target:
            entity_id: "{{ repeat.item }}"
          data:
            brightness: >-
              {% set current_brightness = state_attr(repeat.item,
              'brightness')|default(0)|int %} {% if current_brightness > 15 and
              is_state(repeat.item, 'on') %}
                15
              {% else %}
                {{ current_brightness }}
              {% endif %}
          action: light.turn_on
  - action: cover.set_cover_position
    data:
      position: 40
    target:
      entity_id: cover.shutter_window_living_room
  - delay:
      hours: 0
      minutes: 0
      seconds: 2
      milliseconds: 0
  - action: input_boolean.turn_on
    target:
      entity_id: input_boolean.tv_mode_status
icon: mdi:television

Automation:

alias: TV Mode Status Update
trigger:
  - platform: state
    entity_id:
      - light.living_room
      - light.hallway
condition:
  - condition: state
    entity_id: input_boolean.tv_mode_status
    state: "on"
action:
  - action: input_boolean.turn_off
    target:
      entity_id: input_boolean.tv_mode_status
mode: restart

Button:

type: custom:bubble-card
card_type: button
entity: input_boolean.tv_mode_status
show_state: true
card_layout: large
columns: 2
scrolling_effect: false
tap_action:
  action: call-service
  service: script.turn_on
  target:
    entity_id: script.tv_mode
button_action:
  hold_action:
    action: none
  tap_action:
    action: call-service
    service: script.turn_on
    target:
      entity_id: script.tv_mode
  double_tap_action:
    action: none
button_type: state
double_tap_action:
  action: none
hold_action:
  action: none
name: TV Mode