Harmony Cleaning code and making it better

Hi @ehendrix. I saw your posing some Harmony automasations that made me see that i could maby learn a thing or to and clean upp my own code.

I saw the post you done and what id like to see is the following. Your script.remote_harmony_poweroff and script.remote_harmony_start_activity

Could you maby show me? Right now my automasation does everything but select the correkt input_select when starting upp.

Id realy appriciate it. And ty for your work for the community.

I have the following within my automation to set my input_select on startup:

    - alias: 'Setup after startup'
      hide_entity: true
      initial_state: on
      trigger:
        - platform: homeassistant
          event: start
      action:
        # Turn off the automation to enable an activity based on the input_select.
        # Need to do this 1st time as the state can go from PowerOff to an activity.
        - service: automation.turn_off
          entity_id: automation.Harmony_Activity_Selected
        # Set the input select box to current activity for the family room.
        - service: input_select.select_option
          entity_id: input_select.family_room_tv
          data_template:
            option: "{{ state_attr('remote.family_room','current_activity') }}"
        # Set the input select box to current activity for the master bedroom.
        - service: input_select.select_option
          entity_id: input_select.master_bedroom_tv
          data_template:
            option: "{{ state_attr('remote.master_bedroom','current_activity') }}"
        - delay: 00:00:01 # Wait 1 second before turning on the automation again.
        # Now turn the automation on again, from now on we should act on it.
        - service: automation.turn_on
          entity_id: automation.Harmony_Activity_Selected

And here is what I have then for both when an activity is selected from within HASS:

    - alias: "Harmony_Activity_Selected"
      hide_entity: true
      trigger:
        - platform: state
          entity_id: input_select.family_room_tv
        - platform: state
          entity_id: input_select.master_bedroom_tv
      condition:
        - condition: template
          value_template: "{{ trigger.from_state.state != trigger.to_state.state }}"
        - condition: template
          value_template: >
            {% set entity = trigger.entity_id.split(".")[1] %}
            {% set room = entity.split("_")[0] + "_" + entity.split("_")[1] %}
            {{ trigger.to_state.state != state_attr("remote." + room,'current_activity') }}
      action:
        service_template: >
          {% if is_state(trigger.entity_id, "PowerOff") %}
            script.remote_harmony_poweroff
          {% else %}
            script.remote_harmony_start_activity
          {% endif %}
        data_template:
          room: >
            {% set entity = trigger.entity_id.split(".")[1] %}
            {% set room = entity.split("_")[0] + "_" + entity.split("_")[1] %}
            {{ room }}
          activity: " {{ trigger.to_state.state }} "

And here when an activity is selected from somewhere else (i.e. remote):

    - alias: "Harmony_Update_Activity_Selected"
      hide_entity: true
      initial_state: on
      trigger:
        - platform: state
          entity_id: remote.family_room
        - platform: state
          entity_id: remote.master_bedroom
      action:
        service: input_select.select_option
        data_template:
          entity_id: >
            {% set entity = trigger.entity_id.split(".")[1] %}
            {% set room = entity.split("_")[0] + "_" + entity.split("_")[1] %}
            input_select.{{ room }}_tv
          option: "{{ state_attr(trigger.entity_id,'current_activity') }}"

Absolutely. :slight_smile:
Here they are:

  remote_harmony_start_activity:
  alias: "Harmony Start Activity Script"
  sequence:
    - service: logbook.log
      data_template:
        name: Harmony Remote
        message: Starting activity {{ activity }} in {{ room }}
        entity_id: remote.{{ room }}
        domain: remote
    - service: remote.turn_on
      data_template:
        entity_id: remote.{{ room }}
        activity: "{{ activity }}"
    - delay: '00:00:30'
    - service: script.remote_harmony_turn_tv_on
      data_template:
        room: "{{ room }}"
    - service: homeassistant.update_entity
      data_template:
        entity_id: remote.{{ room }}


remote_harmony_poweroff:
  alias: "Harmony Power Off Script"
  sequence:
    - service: logbook.log
      data_template:
        name: Harmony Remote
        message: Powering off {{ room }}
        entity_id: remote.{{ room }}
        domain: remote
    - service: remote.turn_off
      data_template:
        entity_id: remote.{{ room }}
    - service: homeassistant.update_entity
      data_template:
        entity_id: remote.{{ room }}

Here is for turning TV on. I sometimes noticed that TV ended up not being turned on. Hence wanted to make sure. Note, this is using the newer version of the Harmony component (HASS 0.84.5) as then one can specify the name of the device as well. Makes it easier since then you can specify TV and the actual TV the command is send to depends on the HUB. Before with ID you had to then still determine in the script which HUB and thus which TV.

remote_harmony_turn_tv_on:
  alias: "Power on TV Script"
  sequence:
    - service: logbook.log
      data_template:
        name: Harmony Remote
        message: Turning TV on in {{ room }}
        entity_id: remote.{{ room }}
        domain: remote
    - service: remote.send_command
      data_template:
        entity_id: remote.{{ room }}
        command:
          - PowerOn
        device: TV

Im using below to my setup but ive tried diffrent option to make it work with my swedish setup.
But if its in power off state i get error that input select does not have the option. Ive tried to make a allse if code but ive not gotten it to work.

Anyone have a insight to if it start with harmony on poweroff then it should select input select = “Välj Aktivitet”
Thats allso as far as i seen the only option that aint working.

- alias: 'Setup after startup'
  hide_entity: true
  initial_state: on
  trigger:
    - platform: homeassistant
      event: start
  action:
    - service: input_select.select_option
      entity_id: input_select.living_room_tv
      data_template:
        option: "{{ state_attr('remote.vardagsrum','current_activity') }}"

Anyone that has any input on hon to do this? Ive tired to make a Template and Home about it diffrent ways to make it as IF its poweroff then input selet ”stäng av” but nothing ive tried has Worked.

My skills wont cut it. Searches around the forums and Came out blank.

After some testing this was what made it work for me.

- alias: 'harmony vardagsrum setup after startup'
  hide_entity: true
  initial_state: on
  trigger:
    - platform: homeassistant
      event: start
  action:
    - service: input_select.select_option
      entity_id: input_select.living_room_tv
      data_template:
        option: >
          {% if states.remote.vardagsrum.attributes.current_activity == "PowerOff" %}
            Välj Aktivitet
          {% elif states.remote.vardagsrum.attributes.current_activity == "Tv" %}
            Tv
          {% elif states.remote.vardagsrum.attributes.current_activity == "Htpc" %}
            Htpc
          {% elif states.remote.vardagsrum.attributes.current_activity == "Apple tv" %}
            Apple tv
          {% elif states.remote.vardagsrum.attributes.current_activity == "Raspberry Pi" %}
            Raspberry Pi
          {% elif states.remote.vardagsrum.attributes.current_activity == "Unknown" %}
            Välj Aktivitet
          {% endif %}