If-then-else used in Service Data

Hi all,
I’m stuck on what seems to be a logical fail using if-then-else in a Service Data JSON statement.
What I did was this:

  1. Test a Service call that works OK with if-then-else embedded in Service Data
  2. Develop and test a simple Service call to ensure that each element in my case is correct.
  3. Further develop step #2 to include the if-then-else condition.

Step #1 and step#2 works just perfect, but step#3 fails. Here are the three steps:
Service%20Test%20notify%20OK

Service%20Harmony%20Mute%20OK

As you can see, JSON is not complaining about any syntax error in step #3, but when executed (called) the following error message appear in the log:

Harmony: Device {% if is_state_attr('remote.harmony', 'current_activity', 'Get-TV') %}29903080{% else %}29903080{% endif %} is invalid

My question is this: Why does the if-then-else statement work ok in step #1 but not in step #3 ??

[Edit] Just to test the template, I used the template editor in the GUI, and it too says everything is OK:

That’s because the notify.xxx service supports templates (directly) in its message parameter, whereas remote.send_command does not support templates (directly) in its device parameter. However, you can still use templates for it in a service call from an automation or script (because scripting supports templates for any parameter via its data_template option):

- service: remote.send_command
  entity_id: remote.harmony
  data:
    command: Mute
  data_template:
    device: >
      {% if is_state_attr('remote.harmony', 'current_activity', 'Get-TV') %}
        29903080
      {% else %}
        29903080
      {% endif %}

@pnbruckner, hi and many thanks for your reply.
I used your code sample for testing (in order to understand better), and my automation now looks like this:

- id: '1551008851122'
  alias: Test
  trigger:
  - entity_id: remote.harmony
    platform: state
    to: 'on'
  action:
  - service: remote.send_command
    entity_id: remote.harmony
    data:
       command: Mute
    data_template:
       device: >
           {% if is_state_attr('remote.harmony', 'current_activity', 'Apple-TV') %}
             29903080
           {% else %}
            29038741
           {% endif %}

The syntax seems OK as no errors are generated, but it does not work as expected. No matter what I type as current activity (i.e. Apple-TV) the test appears to always result in true, thus device 29903080 is muted - always.

What does:

{{ states.remote.harmony.attributes }}

look like if entered into the Template Editor?

@pnbruckner, here is the output from the template editor:

{'current_activity': 'Get-TV', 'friendly_name': 'Harmony'}

Hmm, well then I can’t explain it. So are you sure you want to trigger the automation with the state of remote.harmony changing to ‘on’? Or do you want to trigger it when the current_activity attribute changes? (I don’t use this type of entity so I don’t know how it behaves.)

In any case, it would seem that whenever remote.harmony’s state changes to ‘on’ its current_activity attribute is equal to ‘Apple-TV’. That’s the only way to explain (that I can see at this point) why device is always 29903080.

@pnbruckner, Sir, I just discovered some important news (to me, at least); I actually have to restart HA everytime I make a change to the if-then-else statement in automations.yaml for it to recognize the change!
I had no idea! So, in fact it does work!

Still though, your comment about the trigger is very relevant. I had no idea what trigger to use, so I just chose ‘on’ in place of anything better. Now that I finally have an automation that works, it’s time to put it to good use. Any suggestions?

Depending on how you have your configuration laid out it might be possible to use one of the reload buttons on the general configuration page. But certainly restarting HA will definitely get it to read the config files again.

It all depends on what you’re ultimately trying to do, and how your remote.harmony entity behaves. Since I don’t use that it’s kind of hard for me to say. I’d suggest watching that entity to see how it changes in response to your usage. Then it should become obvious what trigger to use, etc. Without a lot more detail I’m not sure I can make any useful suggestions. But if you have any more specific questions, we’re always glad to try and help. :slight_smile: