Condition if trigger has attribute

I’m automating car precooling based on next calendar event location.

Idea is when calendar event has location - precool car.

{{ trigger.calendar_event.location != None }}

When trigger event has no location attribute, this template does not work.
Please help.

From my testing, you should be able to use {{ trigger.calendar_event.location is defined }}, but if you want to be extra sure that a None doesn’t get through, or you want to exclude specific locations try:

{% if trigger.calendar_event.location is defined %}
{{ trigger.calendar_event.location not in [none, "Home", "Secret Location #1"] }}
{% else %}
False
{% endif %}

None has to be lowercase in jinja fyi

I’m not sure that is true. I had this template sensor:

- name: "Upstairs Heatpump Mode Required"
  icon: "mdi:sun-snowflake-variant"
  state: >
    {% if states('sensor.lounge_room_temperature')|float(25) <= states('input_number.upstairs_ac_temp_set_heat')|float(21) - 2 %}
      Heat
    {% elif states('sensor.lounge_room_temperature')|float(19) >= states('input_number.upstairs_ac_temp_set_cool')|float(21) + 2 %}
      Cool
    {% else %}
      None
    {% endif %}

And its state changed to unavailable (or maybe it was unknown ) when hetaing or cooling was not required. I had to change it to

    {% else %}
      'None'
    {% endif %}

You’re confusing a templates output with using none in code. A simple test can show you that None is not a valid test in jinja. Although I was mistaken that you can use None as a non-test keyword. However, what you’re describing is not in code, you’re in the returned part of code.

fails with TemplateAssertionError: No test named 'None'.

{{ 1 is None }}

works

{{ 1 == None }}

Shouldn’t matter because you’re not executing none

{% if ... %}
  None
{% endif %}

Sorry @Didgeridrew, using None in the list will work.

{{ none in [None, 1] }}

TLDR, when checking None as a test (selectattr, select, reject, rejectattr, is), you have to use none.

1 Like

Got it. Thanks.