Data_template with OR condition

Hello community,
I have the following automation to play alarm in xiaomi Gateway if at least one mobile is at home (GPSLogger):

  - service: xiaomi_aqara.play_ringtone
    data_template:
      ringtone_vol: >
        {% if device_tracker.mobile1.state == 'home' || device_tracker.mobile2.state == 'home' %}
          2
        {% else %}
          10
        {% endif %}
      gw_mac: xxxxxxxxxxxxxx
      ringtone_id: 1

But this will end in:

Invalid config for [automation]: invalid template (TemplateSyntaxError: expected token ā€˜nameā€™, got ā€˜|ā€™) for dictionary value @ data[ā€˜actionā€™][1][ā€˜data_templateā€™][ā€˜ringtone_volā€™]. Got None.

It seems it doesnā€™t like the OR condition (||), plus it doensā€™t recognize the ringtone_vol value (but this depends for sure from first error). Could someone help here to add OR to IF THEN? Thank you

Thank you
Lucas

1 Like

Try this:

  - service: xiaomi_aqara.play_ringtone
    data_template:
      ringtone_vol: >-
        {%- if states.device_tracker.mobile1.state == 'home' or states.device_tracker.mobile2.state == 'home' -%}
          2
        {%- else -%}
          10
        {%- endif -%}
      gw_mac: xxxxxxxxxxxxxx
      ringtone_id: 1
3 Likes

Thank you for reply, Iā€™ll try it asap even if Iā€™m suspetting the state doesnā€™t work or at least is not valorized with word ā€˜homeā€™, so first I would like to try to send me a notification with device state, but also there Iā€™m getting crazy :roll_eyes:

  - service: notify.notifiche
    data_template:
      title: "Notification"
      message: "GPS State: {{ device_tracker.mobile1.state }}"

Obliviously other Pushbullet notifications work perfect!!!

EDIT: Ah got it! I forgot ā€œstates.ā€ as per your code

your state should be states.device_tracker.mobile1.state

No way :frowning:

/home/homeassistant/.homeassistant/configuration.yaml: while parsing a block mapping
in ā€œ/home/homeassistant/.homeassistant/automations.yamlā€, line 333, column 7
expected ā€˜block endā€™, but found ā€˜scalarā€™
in ā€œ/home/homeassistant/.homeassistant/automations.yamlā€, line 336, column 11

I move the gateway volume set to a script to simplify the action, so I tried the following, but still got error:

  - service: script.turn_on
    data_template:
      entity_id: >
        {%- if states.device_tracker.mobile1.state == 'home' or states.device_tracker.mobile2.state == 'home' -%} 
          script.gateway_volume_ten
        {% endif %}

probably the indentations.
Ty this instead:

  - service: xiaomi_aqara.play_ringtone
    data_template:
      ringtone_vol: >-
        {%- if states.device_tracker.mobile1.state == 'home' or states.device_tracker.mobile2.state == 'home' -%}2{%- else -%}10{%- endif -%}
      gw_mac: xxxxxxxxxxxxxx
      ringtone_id: 1

if that still doesnā€™t work, try and replace 'home' with "home"

Same errorā€¦ Maybe is better I found another way for my automation.

hum odd. If you leave everything the same and replace only

      ringtone_vol: >-
        {%- if states.device_tracker.mobile1.state == 'home' or states.device_tracker.mobile2.state == 'home' -%}2{%- else -%}10{%- endif -%}

with

      ringtone_vol: >-
        2

does that work?

Try:

{% if 'home' in [ states.device_tracker.mobile1.state, states.device_tracker.mobile2.state ] %}
  0.02
{% else %}
  0.10
{% endif %}

I think @lolouk44ā€™s implementation will work too using 0.02 and 0.10. The documentation for ringtone_vol says that it requires a % value which home assistant typically uses 0 to 1 (at least for media_player devices).

EDIT: Itā€™s hard to sayā€¦ the documentation points to using 8 as a value. I donā€™t know why @lolouk44ā€™s automation isnā€™t working. Chances are my implementation wonā€™t work either.

EDIT 2: I just checked the source code. The min is 0 the max is 100. So, iā€™m not sure why either @lolouk44ā€™s implementation isnā€™t working.

    vol.Optional(ATTR_RINGTONE_VOL):
    vol.All(vol.Coerce(int), vol.Clamp(min=0, max=100))
1 Like

Thank you for the info, Iā€™ll start my automation from scratch and Iā€™ll report the result back here!

Guys, I found the root cause of the error, and thatā€™s a crazy issue. As I wrote I have 2 devices, real names assigned by HA are:
mine: device_tracker.ce051605ca46151234
And my wifeā€™s one: device_tracker.52101ef647546529
The long number string is the devices serial number.

Well, the problem is my wifeā€™s device (Iā€™ve always known that wives are the problem :rofl: )

I donā€™t know why, but HA doesnā€™t like the device name. If, for example I modify it to:
device_tracker.cd52101ef647546529
HA accept the configuration and doesnā€™t give me any error. Obliviously the automation doesnā€™t work due to wrong device name.
Now, how can I solve this issue? The device name is automatically assigned by HA into know_devices.yaml :thinking:

EDIT 1:
Well, seem I solved in this way (using ā€˜ā€™ ):

{% if 'home' in [ states.device_tracker.ce051605ca46151234.state, 'statesdevice_tracker.52101ef647546529' ] %}
  script.gateway_volume_ten
{% endif %}

No more error from HA, letā€™s see if the automation works.

EDIT 2: Nope, Iā€™m surrenderingā€¦ The quotes just report the name od device_tracker as state, so automation doesnā€™t workā€¦ Thatā€™s really oddā€¦

Not sure. I can say that typically python does not like numbers as starting characters of object names. That could be your issue. Try swapping to this:

{% if 'home' in [ state('device_tracker.ce051605ca46151234'), state('statesdevice_tracker.52101ef647546529') ] %}
  script.gateway_volume_ten
{% endif %}

This explain why I got the error, and youā€™re absolutely right about python.
To chek whatā€™s the tracker state, I sent a notification to my phone, like:

message: "State: {{ state('statesdevice_tracker.52101ef647546529') }} "

This will result in (no notification sent):

ERROR (MainThread) [homeassistant.helpers.service] Error rendering data template: UndefinedError: ā€˜stateā€™ is undefined

The right code is:
message: "State: {{ states('device_tracker.52101ef647546529') }} "

And finally I got no errors and the notifiication says:
State: Home

I believe I can use the same format with IF THEN ELSE
Thank you very match to all, expecially you, @petro, who drive me to the solution!! :+1:

1 Like