Hamony Automation: change scene at turn on//off activity

Tags: #<Tag:0x00007f326813a168>

Hello again!
I would like to make four automations for Harmony, but I can’t get them to work

FIRST automation:

  1. Power on
  2. If and only if a particular activity is active and the sun has set
  3. Change scene.

SECOND automation:

  1. shutdown
  2. if and only if a particular activity is active and the sun has set
  3. change scene

THIRD automation:

  1. Power on
  2. if and only if one of the listed activities is active
  3. change scene

FOURTH automation:

  1. shutdown
  2. if and only if one of the listed activities is active
  3. change scene
  4. wait 10 minutes
  5. turn off a plug

It doesn’t change my scene …
where am i wrong?

my code:

#FIRST#
- alias: kodi soggiorno off
  trigger:
  - entity_id: remote.harmony_soggiorno
    platform: state
    to: 'off'
  condition:
  - condition: state
    entity_id: sun.sun
    state: below_horizon
  - condition: template
    value_template: '{{ trigger.to_state.attributes.current_activity == "Watch Kodi" }}'
  action:
    service: scene.turn_on
    entity_id: scene.soggiorno_normale
  id: 683f8418b2b349e9a81bfbd03972b007
#SECOND#
- alias: kodi soggiorno on
  trigger:
  - entity_id: remote.harmony_soggiorno
    platform: state
    to: 'on'
  condition:
  - condition: state
    entity_id: sun.sun
    state: below_horizon
  - condition:template value_template:'{{ trigger.to_state.attributes.current_activity == "Watch Kodi" }}
  action:
    service: scene.turn_on
    entity_id: scene.soggiorno_spenta
  id: 82eae7d823e443febdac6d78bc835baa
#THIRD#
- alias: taverna off
  trigger:
  - entity_id: remote.harmony_taverna_2
    platform: state
    to: 'off'
  condition:
    condition: or
    conditions:
    - condition: template
      value_template: '{{ trigger.to_state.attributes.current_activity == "Project Kodi" }}'
    - condition: template
      value_template: '{{ trigger.to_state.attributes.current_activity == "Project Kodi 2" }}'
    - condition: template
      value_template: '{{ trigger.to_state.attributes.current_activity == "Project Wii" }}'
    - condition: template
      value_template: '{{ trigger.to_state.attributes.current_activity == "Project Xbox 360" }}'
    - condition: template
      value_template: '{{ trigger.to_state.attributes.current_activity == "Project Retro Console" }}'
    - condition: template
      value_template: '{{ trigger.to_state.attributes.current_activity == "Projector" }}'
  action:
  - service: scene.turn_on
    entity_id: scene.taverna_normale
  - delay: 00:10:00
  - service: switch.turn_off
    entity_id: switch.accessory_mss310_main_channel
  - service: switch.turn_off
    entity_id: switch.media_center_mss310_main_channel
  id: ef238f31c77346f18e3dc4678beb65c0
#FOURTH#
- alias: taverna on
  trigger:
  - entity_id: remote.harmony_taverna_2
    platform: state
    to: 'on'
  condition:
    condition: or
    conditions:
    - condition: template
      value_template: '{{ trigger.to_state.attributes.current_activity == "Project Kodi" }}'
    - condition: template
      value_template: '{{ trigger.to_state.attributes.current_activity == "Project Kodi 2" }}'
    - condition: template
      value_template: '{{ trigger.to_state.attributes.current_activity == "Project Wii" }}'
    - condition: template
      value_template: '{{ trigger.to_state.attributes.current_activity == "Project Xbox 360" }}'
    - condition: template
      value_template: '{{ trigger.to_state.attributes.current_activity == "Project Retro Console" }}'
    - condition: template
      value_template: '{{ trigger.to_state.attributes.current_activity == "Projector" }}'
  action:
    service: scene.turn_on
    entity_id: scene.taverna_spenta
  id: aa4a74f063bf406a82177bf3addb9419
  1. Go to Configuration > Automations.
  2. Find the first automation (kodi soggiorno off) and click EXECUTE.
  3. It will skip the automation’s trigger and condition and simply execute the action.
  4. It should turn on scene.soggiorno_normale.

If the scene is turned on when the automation is executed manually like this, that means there’s a problem with either its trigger or the condition. One of them isn’t working the way you expected and prevents the automation’s action from executing.

If the scene fails to turn on when you execute the automation manually, then the problem is just the scene.

Thanks for aswerd me.
I try execute, all work like it have to work.

The problem is or trigger o condition, but i don’t have idea where my code is wrong…

  1. Go to Developer Tools > States and find remote.harmony_soggiorno.
  2. Confirm off is a valid state.
  3. Confirm it has an attribute called current_activity.
  4. Confirm that when the state is off that the current_activity attribute can be Watch Kodi.

The remote does not have attributes when it’s state is off. The condition is holding the automation back

In that case, Kykkus will have to choose a completely different approach to achieve the desired goals.

I solved it with this code:

- alias: kodi soggiorno off
  trigger:
  - entity_id: remote.harmony_soggiorno
    platform: state
  condition:
  - condition: state
    entity_id: sun.sun
    state: below_horizon
  - condition: template
    value_template: >
     {{ trigger.from_state.attributes.current_activity == 'Watch Kodi' and
        trigger.to_state.attributes.current_activity == 'PowerOff' }}
  action:
    service: scene.turn_on
    entity_id: scene.soggiorno_normale
  id: 683f8418b2b349e9a81bfbd03972b007
- alias: kodi soggiorno on
  trigger:
  - entity_id: remote.harmony_soggiorno
    platform: state
  condition:
  - condition: state
    entity_id: sun.sun
    state: below_horizon
  - condition: template 
    value_template: >
     {{ trigger.from_state.attributes.current_activity == 'PowerOff' and
        trigger.to_state.attributes.current_activity == 'Watch Kodi' }}
  action:
    service: scene.turn_on
    entity_id: scene.soggiorno_spenta
  id: 82eae7d823e443febdac6d78bc835baa
- alias: kodi taverna off
  trigger:
  - entity_id: remote.harmony_taverna_2
    platform: state
  condition:
  - condition: or
    conditions:
    - condition: template
      value_template: >
       {{ trigger.to_state.attributes.current_activity == 'Project Kodi' and
           trigger.to_state.attributes.current_activity == 'PowerOff' }}
    - condition: template
      value_template: >
       {{ trigger.to_state.attributes.current_activity == 'Project Kodi 2' and
          trigger.to_state.attributes.current_activity == 'PowerOff' }}
    - condition: template
      value_template: >
       {{ trigger.to_state.attributes.current_activity == 'Project Wii' and
          trigger.to_state.attributes.current_activity == 'PowerOff' }}
    - condition: template
      value_template: >
       {{ trigger.to_state.attributes.current_activity == 'Project Xbox 360' and
          trigger.to_state.attributes.current_activity == 'PowerOff' }}
    - condition: template
      value_template: >
       {{ trigger.to_state.attributes.current_activity == 'Project Retro Console' and
          trigger.to_state.attributes.current_activity == 'PowerOff' }}
    - condition: template
      value_template: >
       {{ trigger.to_state.attributes.current_activity == 'Projector' and
          trigger.to_state.attributes.current_activity == 'PowerOff' }}
  action:
  - service: scene.turn_on
    entity_id: scene.taverna_normale
  - delay: 00:10:00
  - service: switch.turn_off
    entity_id: switch.accessory_mss310_main_channel
  - service: switch.turn_off
    entity_id: switch.media_center_mss310_main_channel
  mode: single
- id: aa4a74f063bf406a82177bf3addb9419
  alias: taverna on
  trigger:
  - entity_id: remote.harmony_taverna_2
    platform: state
  condition:
  - condition: or
    conditions:
    - condition: template
      value_template: >
       {{ trigger.to_state.attributes.current_activity == 'PowerOff' and
          trigger.to_state.attributes.current_activity == 'Project Kodi' }}
    - condition: template
      value_template: >
       {{ trigger.to_state.attributes.current_activity == 'PowerOff' and
          trigger.to_state.attributes.current_activity == 'Project Kodi 2' }}
    - condition: template
      value_template: >
       {{ trigger.to_state.attributes.current_activity == 'PowerOff' and
          trigger.to_state.attributes.current_activity == 'Project Wii' }}
    - condition: template
      value_template: >
       {{ trigger.to_state.attributes.current_activity == 'PowerOff' and
          trigger.to_state.attributes.current_activity == 'Project Xbox 360' }}
    - condition: template
      value_template: >
       {{ trigger.to_state.attributes.current_activity == 'PowerOff' and
          trigger.to_state.attributes.current_activity == 'Project Retro Console' }}
    - condition: template
      value_template: >
       {{ trigger.to_state.attributes.current_activity == 'PowerOff' and
          trigger.to_state.attributes.current_activity == 'Projector' }}
  action:
  - service: scene.turn_on
    entity_id: scene.taverna_spenta
  mode: single
  id: ef238f31c77346f18e3dc4678beb65c0

But now I have this problem,
I have other activity not listed here, like watching tv etc, if I switch from one activity not listed to another hassio doesn’t activate the scene … without having to specify every single action to switch from one activity to another, there is a method to say:

If I change activity, does it return to the initial scene?

The first two automations can be combined into a single automation and you can take advantage of the State Trigger’s ability to trigger on changes to an entity’s attribute instead of its state.

- alias: kodi soggiorno
  trigger:
  - platform: state
    entity_id: remote.harmony_soggiorno
    attribute: current_activity
    from: 'Watch Kodi'
    to: 'PowerOff'
  - platform: state
    entity_id: remote.harmony_soggiorno
    attribute: current_activity
    from: 'PowerOff'
    to: 'Watch Kodi'
  condition:
  - condition: state
    entity_id: sun.sun
    state: below_horizon
  action:
    service: scene.turn_on
    data:
      entity_id: "scene.soggiorno_{{ 'normale' if trigger.to_state.attributes.current_activity == 'PowerOff' else 'spenta' }}"

You can also create a State Trigger that triggers when the attribute current_activity changes from a specific value to any other value. For example, this will trigger when it changes from PoweOff to anything else:

- alias: kodi taverna
  trigger:
  - platform: state
    entity_id: remote.harmony_taverna_2
    attribute: current_activity
    from: 'PowerOff'
  condition:
    ### etc ###

The action would then use choose to determine what to do based on the value of trigger.to_state.attributes.current_activity. See: Choose a group of actions.

You should know that there’s a problem with the other automations you posted because the condition’s templates expect to_state to be two different values at the same time.

       {{ trigger.to_state.attributes.current_activity == 'Project Kodi 2' and
          trigger.to_state.attributes.current_activity == 'PowerOff' }}

I think you meant to use to_state and from_state.

Thanks for the answer, I still take advantage of your kindness to ask you if you can explain to me why the condition of these four automations does not work, if you have the possibility to answer me. Thanks

- alias: kodi taverna paused
  trigger:
  - entity_id: media_player.kodi_htpc
    from: playing
    platform: state
  - entity_id: remote.harmony_taverna_2 
    platform: state
    attribute: current_activity
  condition:
  - condition: or
    conditions:
    - condition: template
      value_template: >
       "{{ states.remote.harmony_taverna_2.attributes.current_activity == 'Project Kodi' }}"
    - condition: template
      value_template: >
       "{{ states.remote.harmony_taverna_2.attributes.current_activity == 'Project Kodi 2' }}"
    - condition: template
      value_template: >
       "{{ states.remote.harmony_taverna_2.attributes.current_activity == 'Project Wii' }}"
    - condition: template
      value_template: >
       "{{ states.remote.harmony_taverna_2.attributes.current_activity == 'Project Xbox 360' }}"
    - condition: template
      value_template: >
       "{{ states.remote.harmony_taverna_2.attributes.current_activity == 'Project Retro Console' }}"
    - condition: template
      value_template: >
       "{{ states.remote.harmony_taverna_2.attributes.current_activity == 'Projector' }}"
  action:
    service: scene.turn_on
    entity_id: scene.taverna_pausa
  id: c26b2f379e20459da380f66bf59227a1

- alias: kodi taverna play
  trigger:
  - entity_id: media_player.kodi_htpc
    to: playing
    platform: state
  - entity_id: remote.harmony_taverna_2 
    platform: state
    attribute: current_activity
  condition:
  - condition: or
    conditions:
    - condition: template
      value_template: >
       "{{ states.remote.harmony_taverna_2.attributes.current_activity == 'Project Kodi' }}"
    - condition: template
      value_template: >
       "{{ states.remote.harmony_taverna_2.attributes.current_activity == 'Project Kodi 2' }}"
    - condition: template
      value_template: >
       "{{ states.remote.harmony_taverna_2.attributes.current_activity == 'Project Wii' }}"
    - condition: template
      value_template: >
       "{{ states.remote.harmony_taverna_2.attributes.current_activity == 'Project Xbox 360' }}"
    - condition: template
      value_template: >
       "{{ states.remote.harmony_taverna_2.attributes.current_activity == 'Project Retro Console' }}"
    - condition: template
      value_template: >
       "{{ states.remote.harmony_taverna_2.attributes.current_activity == 'Projector' }}"
  action:
    service: scene.turn_on
    entity_id: scene.taverna_spenta
  id: c18d3c1ab504483885c8ff535fdd055a
- alias: kodi taverna off
  trigger:
  - entity_id: remote.harmony_taverna_2 
    platform: state
    attribute: current_activity
    to: 'PowerOff'
  condition:
   - condition: template
     value_template: >
      "{{ trigger.to_state.attributes.current_activity == 'PowerOff' }}"
  action:
  - service: scene.turn_on
    entity_id: scene.taverna_normale
  - delay: 00:10:00
  - service: switch.turn_off
    entity_id: switch.accessory_mss310_main_channel
  - service: switch.turn_off
    entity_id: switch.media_center_mss310_main_channel

- alias: kodi taverna on
  trigger:
  - entity_id: remote.harmony_taverna_2
    platform: state
    attribute: current_activity
    from: 'PowerOff'
  condition:
  - condition: or
    conditions:
    - condition: template
      value_template: >
       "{{ trigger.from_state.attributes.current_activity == 'PowerOff' and
          trigger.to_state.attributes.current_activity == 'Project Kodi' }}"
    - condition: template
      value_template: >
       "{{ trigger.from_state.attributes.current_activity == 'PowerOff' and
          trigger.to_state.attributes.current_activity == 'Project Kodi 2' }}"
    - condition: template
      value_template: >
       "{{ trigger.from_state.attributes.current_activity == 'PowerOff' and
          trigger.to_state.attributes.current_activity == 'Project Wii' }}"
    - condition: template
      value_template: >
       "{{ trigger.from_state.attributes.current_activity == 'PowerOff' and
          trigger.to_state.attributes.current_activity == 'Project Xbox 360' }}"
    - condition: template
      value_template: >
       "{{ trigger.from_state.attributes.current_activity == 'PowerOff' and
          trigger.to_state.attributes.current_activity == 'Project Retro Console' }}"
    - condition: template
      value_template: >
       "{{ trigger.from_state.attributes.current_activity == 'PowerOff' and
          trigger.to_state.attributes.current_activity == 'Projector' }}"
  action:
  - service: scene.turn_on
    entity_id: scene.taverna_spenta
  id: ef238f31c77346f18e3dc4678beb65c0
- alias: kodi taverna off
  trigger:
  - entity_id: remote.harmony_taverna_2 
    platform: state
    attribute: current_activity
    to: 'PowerOff'
  condition:
   - condition: template
     value_template: >
      "{{ trigger.to_state.attributes.current_activity == 'PowerOff' }}"
  action:
  - service: scene.turn_on
    entity_id: scene.taverna_normale
  - delay: 00:10:00
  - service: switch.turn_off
    entity_id: switch.accessory_mss310_main_channel
  - service: switch.turn_off
    entity_id: switch.media_center_mss310_main_channel

I think the problem is this piece but I don’t understand why …

       "{{ trigger.from_state.attributes.current_activity == 'PowerOff' and
          trigger.to_state.attributes.current_activity == 'Project Kodi' }}"

or

      "{{ trigger.to_state.attributes.current_activity == 'PowerOff' }}"

or

       "{{ states.remote.harmony_taverna_2.attributes.current_activity == 'Project Kodi' }}"

Because you’re incorrectly quoting multiline template notation.

Correct single line template notation

value_template: "{{ template_example_function('interior quotes') }}"
value_template: '{{ template_example_function("interior quotes") }}'
value_template: '{{ template_example_function(''interior quotes'') }}'

Correct multiline template notation ( >, >-, |, |- )

value_template: >
  {{ template_example_function('interior quotes') }}
value_template: >
  {{ template_example_function("interior quotes") }}
value_template: >
  {{ template_example_function(''interior quotes'') }}

Basically, remove the quotes around your template when using multiline notation.


As a sidebar, you’re over complicating your syntax. You can do everything inside a single template.

  - condition: template
    value_template: >
      {{ states.remote.harmony_taverna_2.attributes.current_activity == 'Project Kodi' 
         or states.remote.harmony_taverna_2.attributes.current_activity == 'Project Kodi 2'
         or ... etc }}

Also, it’s suggested that you use state_attr or is_state_attr…

  - condition: template
    value_template: >
      {{ is_state_attr('remote.harmony_taverna_2', 'current_activity', 'Project Kodi' )
         or is_state_attr('remote.harmony_taverna_2', 'current_activity', 'Project Kodi 2' )
         or ...etc... }}

lastly, you can greatly simplify an ‘or’ expression to

  - condition: template
    value_template: >
      {{ state_attr('remote.harmony_taverna_2', 'current_activity') in [ 'Project Kodi', 'Project Kodi 2', ...etc... ] }}

Also, keep in mind that ...etc... is not valid syntax, you have to fill that in with the code for the other activities.