Have a Trigger turn off automation

Hi,

Is there a way to make an automation get turned off by a Trigger?

To explain what I want to do:

I have 2 RGB lights, I use a color loop script ([SOLVED]/Improving Color Loop) in them which I trigger by an automation.

Now the idea is that the Automation turns on 1 hour before sunset and it runs IF

  • My PC is turned on
  • It’s before 23:00

I’d also woud like it to turn off when my TV state CHANGES to turned off

this means, if TV is off during the initial trigger, it still works, but if it’s on and it turns off, then the automation would turn off aswell

(hope it makes a sense :smiley: )

alias: Daily Lights
description: ''
trigger:
  - platform: sun
    event: sunset
    offset: '-01:00:00'
condition:
  - type: is_plugged_in
    condition: device
    device_id: fa9d6783051d1e37279f3bf7fec9e5a1
    entity_id: binary_sensor.skleeny_pc_power_status
    domain: binary_sensor
action:
  - repeat:
      while:
        - type: is_plugged_in
          condition: device
          device_id: fa9d6783051d1e37279f3bf7fec9e5a1
          entity_id: binary_sensor.skleeny_pc_power_status
          domain: binary_sensor
        - condition: and
          conditions:
            - condition: time
              before: '23:00:00'
              after: '15:00:00'
            - condition: or
              conditions:
                - condition: device
                  device_id: 0e9fcc7c91e551bd1f2bc0dce703c6bf
                  domain: media_player
                  entity_id: media_player.pokoj_tv
                  type: is_on
                  enabled: true
              enabled: true
      sequence:
        - service: script.color_loop
          data:
            entity_id:
              - light.yeelight_stripe_0x1b7b2d16
              - light.yeelight_colorb_0x1b379742
        - delay: '00:00:01'
  - service: light.turn_on
    target:
      entity_id:
        - light.yeelight_stripe_0x1b7b2d16
        - light.yeelight_colorb_0x1b379742
    data: {}
mode: single

This is the automation i use currently, the problem with is that if TV is off during the initial trigger, it will not work…

I know that there is a trigger for “TV turned off” which is what I want, but I’m not sure if there is a way for this trigger to disable different automation.

Thanks for your help :slight_smile:

If you want an automation to perform its action only for certain conditions, define those conditions in condition. Don’t turn the automation on/off as a means of controlling when it should/shouldn’t execute its action.

The problem is that is that I’m not sure how to make it react to state change in the condition tab.

I need it in While loop (so the script is running as it’s actively changing colors)

I need some way to make the While loop end when TV is turned off (the actual action of turning it off)

To understand the situation more:

I’m the only one home, TV in living room is off as no one is there, the lights can start at 1hr before Sunset and turn off at 23:00. (this means they should start even if the TV is off in the beggining)

My roommate is home, they are watching TV almost everyday, when they turn it off they go to sleep, so the lights should turn off to not distrupt them (this means when the TV gets shut down, the light should turn off)

If I use “IF TV IS ON” as condition in the while loop, then the 1st scenario doesn’t start.

So I’m not sure what would be the best solution for these, I guess I could make 2 independent automations but it would be messy I think.

You trigger on all conditions that can cause the automation to fire. You can set some Trigger IDs to help you better identify what caused it to trigger.

Your next step is to set the conditions that must be met for all variations of your automation to run.

After that, you setup in the Actions section a choose step that can then have conditions of it’s own in it and if those conditions are met the actions of the choose step happen.

For instance, here’s the automation I have on my front entry lights. This automation does the following:

  • Triggers on the opening, closing (fires after 5 minutes of being closed) or unlocking of the front door
  • No special conditions for the rest of the actions to run
  • On the door closed, it turns off the front entryway lights
  • If my house is in “night mode” (special helper I use for other automations as well) and the door is opened or unlocked, then it turns on the front entryway lights.
alias: 0 - Entry Lights
description: ''
trigger:
  - type: not_opened
    platform: device
    device_id: 929a9be7a545e09d7c7de23809790944
    entity_id: binary_sensor.front_door_sensor_contact
    domain: binary_sensor
    id: door_closed
    for:
      hours: 0
      minutes: 5
      seconds: 0
  - type: opened
    platform: device
    device_id: 929a9be7a545e09d7c7de23809790944
    entity_id: binary_sensor.front_door_sensor_contact
    domain: binary_sensor
    id: door_opened
  - platform: device
    device_id: afd325439dba521e912cb58ef4d2bf1d
    domain: lock
    entity_id: lock.front_door
    type: unlocked
    id: door_unlocked
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: door_closed
        sequence:
          - type: turn_off
            device_id: 542639a67ba2576e7f61ecd38d839a00
            entity_id: switch.entry_lights
            domain: switch
      - conditions:
          - condition: and
            conditions:
              - condition: state
                entity_id: input_boolean.night_mode
                state: 'on'
              - condition: or
                conditions:
                  - condition: trigger
                    id: door_opened
                  - condition: trigger
                    id: door_unlocked
        sequence:
          - type: turn_on
            device_id: 542639a67ba2576e7f61ecd38d839a00
            entity_id: switch.entry_lights
            domain: switch
    default: []
mode: restart

Try this

              conditions:
                - condition: device
                  device_id: 0e9fcc7c91e551bd1f2bc0dce703c6bf
                  domain: media_player
                  entity_id: media_player.pokoj_tv
                  from: 'on'
                  to: 'off'
                  enabled: true

I’ve tried it but I can’t use the from, to switch

I get “extra keys not allowed @ data[‘from’]” error, I think that this would actually help me get it working but I guess the LGTV integration does not support this?

I actually use the from to state change in different integration (not for tv) and it’s working perfectly there