Data_template multiple lines in if?

Hello guys,

I’m pretty new to HA but I’m really interested in getting the best out of it.
I stucked on a think I find really ugly about scripting, at least because I didn’t find a proper solution to it yet. Is it really not possible to make this look better or get rid of some if’s there?

That is what I want to achieve. I think it’s much more readable like that and avoids repetition

shield_up:
  sequence:
  - service_template: >
      {% if is_state('input_select.remote_sender', 'ADB') %}
        androidtv.adb_command
      {% elif is_state('input_select.remote_sender', 'Harmony') %}
        remote.send_command
      {% endif %}
    data_template: >
      {% if is_state('input_select.remote_sender', 'ADB') %}
        entity_id: media_player.shield
        command: UP
      {% elif is_state('input_select.remote_sender', 'Harmony') %}
        entity_id: remote.harmony_hub
        device: 59035175
        command: DirectionUp  
      {% endif %}

But I seem to have to do it like this because I read jinja2 or ha or whatever is the reason won’t take more than one line here

shield_up:
  sequence:
  - service_template: >
      {% if is_state('input_select.remote_sender', 'ADB') %}
        androidtv.adb_command
      {% elif is_state('input_select.remote_sender', 'Harmony') %}
        remote.send_command
      {% endif %}
    data_template: >
        entity_id: >
          {% if is_state('input_select.remote_sender', 'ADB') %}
            media_player.shield
          {% elif is_state('input_select.remote_sender', 'Harmony') %}
            remote.harmony_hub
        command: >
          {% if is_state('input_select.remote_sender', 'ADB') %}
            UP
          {% elif is_state('input_select.remote_sender', 'Harmony') %}
            DirectionUp
          {% elif is_state('input_select.remote_sender', 'Harmony') %}

and even there you can see my problem because I don’t know what happens if I send the device data via adb to a device which doesn’t need it.

Can you tell me if there’s a better way to handle s.th. like this?
My Purpose is to have a remote for my shield but if harmony or adb for whatever reason is down (btw I prefer Harmony, lots faster) I want to be able to change inputs and use the other one instead.

Btw I tried a lot of things, read jinj2 and tried | instead of > to preserve spaces and even tried to get it to
work in the lovelace card which doesn’t seem to accepts templates, so I tried custom templates which lead to the same result + I don’t like logic in my ui cards :stuck_out_tongue:

Thanks in advance :slight_smile: Hope I can give this community some things back when I learned everything about HA:D

EDIT: I tried it the second way and it is how I assumed…
If I send the entry device to adb it doesn’t work and without harmony doesn’t work :confused: so still no good solution

try adding the dash after the > like this:

shield_up:
  sequence:
  - service_template: >-

Hope that helps.

Hey, thanks for your reply :slight_smile:

try adding the dash after the > like this:

shield_up:
 sequence:
 - service_template: >-

My problem is with the data_template. I think the service template should work like that, even if it would be nicer to template the whole thing (service and data) which doesn’t seem to be possible.

I couldn’t find anything in the docs about the usage of > and >- .
What I was able to find out is that according to Template Designer Documentation — Jinja Documentation (2.10.x), the ‘-’ sign in the {%- -%} part removes spaces in the resulting block.
But even with this information I can’t find anything related to home assistant and how we should format the template data in terms of jinja2. So when do we need the >- part?

Addition: Oh no… Don’t tell me the >- just indicates that it’s a dictionary entry… didn’t think about this because it looks like a unity

Hey, I’m trying to figure out a solution to this as well. Did you find a way to get about it?

Thanks!

Sorry but I didn’t look further into it since I found out that I can just catch the script call in node red. I moved all automation logic and now also the scripts to node red and it is way easier to maintain. I don’t know if there are any disadvantages but I didn’t find any yet.

Using version 0.116:

shield_up:
  sequence:
  - service: "{{ 'androidtv.adb_command' if is_state('input_select.remote_sender', 'ADB') else 'remote.send_command' }}"
    data:
      entity_id: "{{ 'media_player.shield' if is_state('input_select.remote_sender', 'ADB') else 'remote.harmony_hub' }}"
      command: "{{ 'UP' if is_state('input_select.remote_sender', 'ADB') else 'DirectionUp' }}"
2 Likes