Automation turning a device on with no action configured to do it

So I have an automation that turns off our TVS. It’s quite complex and it does so starting at 10pm (based on a helper device) for the kids TVs and then starting at 1am, it will turn off any TVs that were turned back on every hour until 5am (I often wake up and will watch TV to go back to sleep. Don’t judge me! LOL)

Anyways, in my son’s room, the automation correctly turns off his TV, but then a few seconds later, it turns it back on! I can’t figure this out.

This is the Trace Timeline from last night’s execution:

Here is my automation yaml:

id: '1686578849785'
alias: Turn off TVs at night.
description: ''
triggers:
  - at: input_datetime.turn_off_tv_time
    id: Turn off all tvs
    trigger: time
  - entity_id:
      - binary_sensor.kids_tv_off
    id: Kids TV Sensor Changed
    to:
      - 'on'
      - 'off'
    trigger: state
  - value_template: |-
      {% set rightNow = now().replace(second=0,microsecond=0) %}
      {% set startTime = today_at(states('input_datetime.turn_off_tv_time')) %}
      {% set endTime = today_at('05:00:00') %}
       
      {{ rightNow > startTime and rightNow <= endTime
              and rightNow.minute == 0 }}
    id: Turn off all tvs
    trigger: template
  - value_template: >-
      {% set rightNow = now() %}

      {% set startTime = today_at(states('input_datetime.turn_off_tv_time')) -
      timedelta(minutes=10) %}

      {% set endTime = today_at('5:00') %}

      {% if timedelta(minutes=0) < (rightNow - (startTime+ timedelta(days=1))) <
      timedelta(minutes=1) %}
        {% set startTime = startTime + timedelta(days=1) %}
        {% set endTime = endTime + timedelta(days=1) %}
      {% endif %}

      {{ rightNow >= startTime 
        and rightNow <= endTime
        and rightNow.minute == startTime.minute }}
    id: Notify TVs will be turning off
    trigger: template
conditions: []
actions:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - Turn off all tvs
        sequence:
          - if:
              - condition: device
                type: is_on
                device_id: 789c80edfc8da7b976f8ca5cad665f44
                entity_id: c7cd5e201ed041de60279f8dc2998925
                domain: remote
            then:
              - type: turn_off
                device_id: 789c80edfc8da7b976f8ca5cad665f44
                entity_id: remote.front_bedroom_harmony
                domain: remote
            alias: Turn off front bedroom harmony if it's on
          - if:
              - condition: device
                type: is_on
                device_id: e4a14e8b13307745b509cd2b988b6a2b
                entity_id: ee95a2a5f270f1d2a11ffe6784c64e6f
                domain: remote
            then:
              - type: turn_off
                device_id: e4a14e8b13307745b509cd2b988b6a2b
                entity_id: ee95a2a5f270f1d2a11ffe6784c64e6f
                domain: remote
            alias: Turn off middle bedroom harmony if it's on
          - if:
              - condition: device
                type: is_on
                device_id: 1dc17bef2b089f9c75b9157abb70e8a1
                entity_id: 130f87f0387f7225db8028288025f68b
                domain: remote
            then:
              - type: turn_off
                device_id: 1dc17bef2b089f9c75b9157abb70e8a1
                entity_id: remote.back_bedroom_harmony
                domain: remote
                enabled: true
            alias: Turn off back bedroom harmony if it's on
          - if:
              - condition: device
                type: is_on
                device_id: da60c2d867476ed304694196285ca1d8
                entity_id: b0fadeb1cab7ca220337c507ae49ec85
                domain: remote
            then:
              - type: turn_off
                device_id: da60c2d867476ed304694196285ca1d8
                entity_id: remote.master_bedroom_harmony
                domain: remote
                enabled: true
            alias: Turn off master bedroom harmony if it's on
          - alias: Turn off living room harmony if it's on
            if:
              - condition: device
                type: is_on
                device_id: 29b1717e190031db1a44aa78e9dd8318
                entity_id: ed597dfb50fd107e3f31667e1cd5d078
                domain: remote
            then:
              - type: turn_off
                device_id: 29b1717e190031db1a44aa78e9dd8318
                entity_id: ed597dfb50fd107e3f31667e1cd5d078
                domain: remote
      - conditions:
          - condition: trigger
            id:
              - Kids TV Sensor Changed
        sequence:
          - choose:
              - conditions:
                  - condition: state
                    entity_id: binary_sensor.kids_tv_off
                    state: 'on'
                sequence:
                  - type: turn_off
                    device_id: e4a14e8b13307745b509cd2b988b6a2b
                    entity_id: ee95a2a5f270f1d2a11ffe6784c64e6f
                    domain: remote
                  - delay:
                      hours: 0
                      minutes: 1
                      seconds: 0
                      milliseconds: 0
                    enabled: true
                  - type: turn_off
                    device_id: 53b5bb662e1226cda3e2bbce33216b29
                    entity_id: e8170e3285431c20f9f449d0e6e8f91d
                    domain: switch
                  - metadata: {}
                    data:
                      message: Boys tv off
                    action: notify.mobile_app_pixel_6_pro
                    enabled: false
                  - type: turn_off
                    device_id: 789c80edfc8da7b976f8ca5cad665f44
                    entity_id: c7cd5e201ed041de60279f8dc2998925
                    domain: remote
              - conditions:
                  - condition: state
                    entity_id: binary_sensor.kids_tv_off
                    state: 'off'
                sequence:
                  - type: turn_on
                    device_id: 53b5bb662e1226cda3e2bbce33216b29
                    entity_id: e8170e3285431c20f9f449d0e6e8f91d
                    domain: switch
                  - metadata: {}
                    data:
                      message: Boys tv on
                    action: notify.mobile_app_pixel_6_pro
                    enabled: false
                  - if:
                      - condition: state
                        entity_id: input_boolean.kids_tv_exception
                        state: 'on'
                    then:
                      - metadata: {}
                        data: {}
                        target:
                          entity_id: input_boolean.kids_tv_exception
                        action: input_boolean.turn_off
      - conditions:
          - condition: trigger
            id:
              - Notify TVs will be turning off
        sequence:
          - if:
              - condition: device
                type: is_on
                device_id: 1dc17bef2b089f9c75b9157abb70e8a1
                entity_id: 130f87f0387f7225db8028288025f68b
                domain: remote
            then:
              - data:
                  data:
                    duration: 4
                    position: bottom-lright
                    fontsize: medium
                    transparency: 90%
                    color: black
                    interrupt: 0
                  message: The TV is going to turn off in 10 minutes.
                action: notify.back_bedroom_tv
            alias: If Back Bedroom TV is on, send notification
          - if:
              - condition: device
                type: is_on
                device_id: 29b1717e190031db1a44aa78e9dd8318
                entity_id: ed597dfb50fd107e3f31667e1cd5d078
                domain: remote
            then:
              - data:
                  entity_id: media_player.lg_webos_smart_tv
                  command: system.notifications/createAlert
                  payload:
                    message: The TV is going to turn off in 10 minutes.
                action: webostv.command
              - delay:
                  hours: 0
                  minutes: 0
                  seconds: 15
                  milliseconds: 0
              - data:
                  entity_id: media_player.lg_webos_smart_tv
                  button: ENTER
                action: webostv.button
            alias: If Living Room TV is on, send notification
          - alias: If Middle Bedroom TV is on, send notification
            if:
              - condition: device
                type: is_on
                device_id: e4a14e8b13307745b509cd2b988b6a2b
                entity_id: ee95a2a5f270f1d2a11ffe6784c64e6f
                domain: remote
            then:
              - data:
                  data:
                    duration: 4
                    position: bottom-lright
                    fontsize: medium
                    transparency: 90%
                    color: black
                    interrupt: 0
                  message: The TV is going to turn off in 10 minutes.
                action: notify.middle_bedroom_tv
mode: parallel
max: 5
trace:
  stored_traces: 20

If I’m reading it correctly (and maybe I’m not; it’s hard to follow YAML code generated by the UI because of the obscure entity_id), your automation turns the TV on when its state changes to off:

 - conditions:
                  - condition: state
                    entity_id: binary_sensor.kids_tv_off
                    state: 'off'
                sequence:
                  - type: turn_on
                    device_id: 53b5bb662e1226cda3e2bbce33216b29
                    entity_id: e8170e3285431c20f9f449d0e6e8f91d
                    domain: switch

So the automation turns the TV off as you expected it but in doing so triggers itself to turn it back on.

If you share more information about what this code is supposed to do, I’ll be glad to help you figure out how to fix it.

In that specific block there are 4 actions.

1 turn off kid 1s tv.
2 wait 60 seconds (to give the TV time to shut off)
3. Turn off the outlet on kid 1s TV (so they can’t turn it back on)
4. Turn off kid 2s tv

I don’t have an outlet in kid 2s room (kid 1 has an outlet because they kept turning it back on).

Steps 1 and 4 are both controlling harmony hubs and use the same command for their respective hub.

Step 3 is an outlet and that is the “switch” you see.

Step 4 is the last action and after that executes, the tv is turned back on. As a matter of fact you can see how it was turned back on via that automation in its history:

Here is a screenshot of the UI version (I agree the is are confusing since you have to have unique friendly names):

What’s the turn_on that I referenced supposed to turn on? It seems like literally the next part of the screenshot so I can’t see it there.

Cut out the portion that @d921 is referencing and see if it resolves your issue.

It’s best to compartmentalize each step to identify the issue. Create a duplicate automation and add each step individually.

I’d also run in mode: restart vs parallel

Can you share the automation trace timeline vs history?

So…I think I found the issue, or maybe I did. I removed the disabled steps (I don’t think this was causing the issue). But I think my son was quickly turning the TV back on and HA was seeing this as the Automation doing it. Very weird. That or maybe the automation was in a “bad state” and making any change fixed it? Not sure, but I’ll monitor it for now and keep an eye on it.

BTW, I didn’t think it was my son because HA is normally good about logging what is causing an action to happen and it even showed up in the Trace Timeline (which you can see the night before last night’s run in th original post)

Thank you both for taking the time to help me. I’ll report back if I start to see it happenning again.