Harmony Channel Picker. If Activity is "something"

Hi all. I been checking around the forum for something that works as followed but not found it yett.
So here goes.

I know about the input_select channel picker that you can setup so you from dropp down can pick channels.
But as that code that i found wont check if an activity is on or not.

Example: I select a Tv Channel. but the Activity TV is not on. So then it shoots commands in to nothing sins Tv is not on.

Is there a way to make this work? i mean homeassistent can understand if the Tv is on or not sins it can see if the activity is on. So can phones using harmony app.

Example: If channel picked. Check Activity. If activity is not “TV” then turn on activity “TV” then run Channel command.

Here the Final code that Jer78 came upp with

Automasation:

- alias: "ChannelChooserTVoff"
  initial_state: 'on'
  hide_entity: true
  trigger:
    - platform: state
      entity_id: input_select.freetoair
  condition:
    - condition: template
      value_template: '{{states.remote.vardagsrum.attributes.current_activity != "Tv"}}'
  action:
    - service: homeassistant.turn_on
      entity_id: switch.tv
    - delay:
        seconds: 15
    - service: homeassistant.turn_on
      data_template:
        entity_id: >
          {% if is_state('input_select.freetoair', 'TV4') %}script.tv4
          {% elif is_state('input_select.freetoair', 'SVT1') %}script.svt1
          {% elif is_state('input_select.freetoair', 'SVT2') %}script.svt2
          {% elif is_state('input_select.freetoair', 'TV3') %}script.tv3
          {% elif is_state('input_select.freetoair', 'Kanal 5') %}script.kanal5
          {% elif is_state('input_select.freetoair', 'Disney XD') %}script.disneyxd
          {% elif is_state('input_select.freetoair', 'Disney Junior') %}script.disneyjunior
          {% elif is_state('input_select.freetoair', 'Disney Channel') %}script.disney
          {% endif %}
          
- alias: "ChannelChooserTVon"
  initial_state: 'on'
  hide_entity: true
  trigger:
    - platform: state
      entity_id: input_select.freetoair
  condition:
    - condition: template
      value_template: '{{states.remote.vardagsrum.attributes.current_activity == "Tv"}}'
  action:
    - service: homeassistant.turn_on
      data_template:
        entity_id: >
          {% if is_state('input_select.freetoair', 'TV4') %}script.tv4
          {% elif is_state('input_select.freetoair', 'SVT1') %}script.svt1
          {% elif is_state('input_select.freetoair', 'SVT2') %}script.svt2
          {% elif is_state('input_select.freetoair', 'TV3') %}script.tv3
          {% elif is_state('input_select.freetoair', 'Kanal 5') %}script.kanal5
          {% elif is_state('input_select.freetoair', 'Disney XD') %}script.disneyxd
          {% elif is_state('input_select.freetoair', 'Disney Junior') %}script.disneyjunior
          {% elif is_state('input_select.freetoair', 'Disney Channel') %}script.disney
          {% endif %}

I been talking to the verry nice user Jer78 here on Homeassistant forum. And he came upp with a fast solution.
So all credits to him for helping and fixing this solution.

In automasation:

  - alias: "ChannelChooser"
    initial_state: 'on'
    hide_entity: true
    trigger:
      - platform: state
        entity_id: input_select.freetoair
    action:
     - service: homeassistant.turn_on
       entity_id: script.check_tv
     - delay:
         seconds: 5
     - service: homeassistant.turn_on
       data_template:
         entity_id: >
           {% if is_state('input_select.freetoair', 'TV4') %}script.tv4
           {% elif is_state('input_select.freetoair', 'SVT1') %}script.svt1
           {% elif is_state('input_select.freetoair', 'SVT2') %}script.svt2
           {% elif is_state('input_select.freetoair', 'TV3') %}script.tv3
           {% elif is_state('input_select.freetoair', 'Kanal 5') %}script.kanal5
           {% elif is_state('input_select.freetoair', 'Disney XD') %}script.disneyxd
           {% elif is_state('input_select.freetoair', 'Disney Junior') %}script.disneyjunior
           {% elif is_state('input_select.freetoair', 'Disney Channel') %}script.disney
           {% endif %}

In Script add:

  check_tv:
  sequence:
    - condition: and
      conditions:
        - condition: template
          value_template: '{% if is_state("remote.vardagsrum", "Tv")%}false{%else%}true{%endif%}'
    - service: homeassistant.turn_on
      entity_id: switch.tv

Can you edit all your posts and encase your yaml in 3 ` (button top left on keyboard) or use the </> button. Cannot help if your yaml is formatted poorly.

ah ty for the instruktions. Edited.

Oh, I didn’t even know your solution was solved! Nice. I’ll move along now.

After some back and fort talking and testing the final code for the channel picker that starts activity is done. All Credits goes to Jer78. Without him it would just have stayed an idee in my mind.

No script is needed. Only an automation:

- alias: "ChannelChooserTVoff"
  initial_state: 'on'
  hide_entity: true
  trigger:
    - platform: state
      entity_id: input_select.freetoair
  condition:
    - condition: template
      value_template: '{{states.remote.vardagsrum.attributes.current_activity != "Tv"}}'
  action:
    - service: homeassistant.turn_on
      entity_id: switch.tv
    - delay:
        seconds: 15
    - service: homeassistant.turn_on
      data_template:
        entity_id: >
          {% if is_state('input_select.freetoair', 'TV4') %}script.tv4
          {% elif is_state('input_select.freetoair', 'SVT1') %}script.svt1
          {% elif is_state('input_select.freetoair', 'SVT2') %}script.svt2
          {% elif is_state('input_select.freetoair', 'TV3') %}script.tv3
          {% elif is_state('input_select.freetoair', 'Kanal 5') %}script.kanal5
          {% elif is_state('input_select.freetoair', 'Disney XD') %}script.disneyxd
          {% elif is_state('input_select.freetoair', 'Disney Junior') %}script.disneyjunior
          {% elif is_state('input_select.freetoair', 'Disney Channel') %}script.disney
          {% endif %}
          
- alias: "ChannelChooserTVon"
  initial_state: 'on'
  hide_entity: true
  trigger:
    - platform: state
      entity_id: input_select.freetoair
  condition:
    - condition: template
      value_template: '{{states.remote.vardagsrum.attributes.current_activity == "Tv"}}'
  action:
    - service: homeassistant.turn_on
      data_template:
        entity_id: >
          {% if is_state('input_select.freetoair', 'TV4') %}script.tv4
          {% elif is_state('input_select.freetoair', 'SVT1') %}script.svt1
          {% elif is_state('input_select.freetoair', 'SVT2') %}script.svt2
          {% elif is_state('input_select.freetoair', 'TV3') %}script.tv3
          {% elif is_state('input_select.freetoair', 'Kanal 5') %}script.kanal5
          {% elif is_state('input_select.freetoair', 'Disney XD') %}script.disneyxd
          {% elif is_state('input_select.freetoair', 'Disney Junior') %}script.disneyjunior
          {% elif is_state('input_select.freetoair', 'Disney Channel') %}script.disney
          {% endif %}