Looking for help making the automation a bit more steamlined

I’ve been using this automation for a long while and it “works”, but it’s a bit slow and clunky. When I wrote it, the goal was to fix an issue outside of HA between my Harmony Hub and my Sony TV that was IP controlled. Every month or so, the hub would loose it’s IP connection, and then the TV would not start. It I was home, easy enough, to go in and fix with the harmony App, but if the spouse was home, then she’d be stuck digging in drawers to get the sony remote, etc. So I aded the ROKU component to HA and did this kinda hacky trick, since HA could control my TV. I recently realized I could make this a bit faster

- alias: TV Force On for Tivo
  trigger:
  - platform: event
    event_type: roku_command
    event_data:
      source_name: Home Assistant
      type: keypress
      key: Fwd
  action:
  - delay:
      seconds: 5
  - condition: or
    conditions:
    - condition: template
      value_template: '{{ (states.media_player.living_room_tv.attributes.source) != "HDMI 1" }}'
    - condition: state
      entity_id: media_player.living_room_tv
      state: 'off'
  - service: media_player.turn_on
    entity_id: media_player.living_room_tv
  - delay:
      seconds: 5
  - service: media_player.select_source
    data:
      entity_id: media_player.living_room_tv
      source: 'HDMI 1'
  - service: notify.pushover
    data_template:
      target: Paul
      title: Sony TV lost Key
      message: Fix Harmony

Basically the roku FWD command is the last thing in the startup sequence for my activity.
If the TV is off, then turn it on and wait for 5 seconds and then set the input.

Overall there is a 10 second delay. I’d like to skip the 1st 5 second delay if the TV is on.
So something like this
ROKU fwd command
is TV on? and set to input 1. Do nothing
Is TV on , but wrong input? Switch Input
Is TV off? Turn it on, wait 5 seconds, then set input.

I’m struggling a bit with a service template, since there are two services I care about?

I’m sure it’s fairly obvious, but my brain is just not focusing in on the answer

If I understood correctly, one of the way is to setup another automation which fire when state of the TV switch to on, then do what you want with the input and so on.

Most of the time, I expect this not to run. This is really meant to run when the harmony failed to do it’s job.

Hmm, so maybe we can take care of the root problem. Isn’t the problem with hub loosing it’s IP address is that the hub doesn’t have IP reservation setup?

Thanks, It’s been an ongoing problem every once in a while for years. It’s known and I appreciate the help but I’ve spent more time adding/removing powering on/of/reseting than I care to do.

For me it’s most likely to happen after a power outage (which I have more of than I’d care). The hub loses the PSK from the Sony TV that is required for IP control. I dont know I’ve lost the key until I go to run an activity. That’s why I created this automation. It does what it’s supposed to (even if its 10 seconds slower), and then lets me know that I need to go into the app and fix it. It takes like 30 seconds to fix in the app and I’m good to go for another few months

Posting my own fix (Maybe?) need to test. Might be useful for logic for others

- alias: TV Force On for Apple TV
  trigger:
  - platform: event
    event_type: roku_command
    event_data:
      source_name: Home Assistant
      type: keypress
      key: Rev
  action:
  - service_template: >
      {% if (states.media_player.living_room_tv.state) == "off" %}
        media_player.turn_on
      {% else %}  {% endif %}
    entity_id: media_player.living_room_tv
  - wait_template: "{{ is_state(states('media_player.living_room_tv.state'), 'on') }}"
  - delay:
      seconds: 5
  - condition: template
    value_template: '{{ (states.media_player.living_room_tv.attributes.source) != "HDMI 3/ARC" }}'
  - service: media_player.select_source
    data:
      entity_id: media_player.living_room_tv
      source: 'HDMI 3/ARC'
  - service: notify.pushover
    data_template:
      target: Paul
      title: Sony TV lost Key
      message: Fix Harmony