Confused by jinja template

Basically I’m trying to make a template that will allow me to make an automation that will ‘catch’ a failed attempt at arming the alarm and will be able to eventually report back over TTS why the house alarm hasn’t been able to arm.

"{{ is_state_attr('alarm_control_panel.alarmo', 'open_sensors', 'None') }}"
"{{ state_attr('alarm_control_panel.alarmo', 'open_sensors') }}"

These return as

false
None

respectively. Which confuses me. In addition, in developer view open_sensors shows ‘null’ rather than none. I tried null too and that still returns as false.

thanks!

Try removing the quotes around None or

{{ state_attr('alarm_control_panel.alarmo', 'open_sensors') is None }}

Thank you! But

TemplateAssertionError: no test named 'None'

{{ state_attr('alarm_control_panel.alarmo', 'open_sensors')|regex_search('None', ignorecase=TRUE) }}"

This seems to work…but the ‘ignorecase’ thing doesn’t seem to being honoured.

@frenck do you think I should log this as a bug? I think it has to do with the value of the sensor being ‘null’ which is probably messing up the logic ?

There is probably a hidden character in the string.

Try this:

{{ state_attr('alarm_control_panel.alarmo', 'open_sensors') | length}}

Thank you, I get

TypeError: object of type 'NoneType' has no len()

{% if states.alarm_control_panel.alarmo.attributes.open_sensors is defined %}
Yes
{% else %}
No
{% endif %}

Returns as a Yes

{{ state_attr('alarm_control_panel.alarmo', 'open_sensors') == none }}

Returns true

Hmm… What happens if you have a sensor open.
Does it report a number or the name of the sensor that is open.
Perhaps you can do a reverse check?
Is open_sensors >= 1?

Interestingly, it doesn’t actually seem to be reporting the open_sensors. @neliss do you know if this is something I’m doing wrong or just something the platform doesn’t yet support (but some of the code is there ready for when it does?) Cheers!

I don’t know anything about jinja.
All I can say is that the open_sensors attribute has the following format:

{
  "binary_sensor.some_sensor": "<state>",
}

If there are no open_sensors, it has value None.
I could also make this an empty dict if it would help you.

Why don’t you use the notification editor in Alarmo instead?

You can’t use is_state_attr() on attributes when checking for none because the logic will always return false if the attribute is not populated (I.E. none).

The only way to check an attribute for none is

{{ state_attr('alarm_control_panel.alarmo', 'open_sensors') == none }}

Thanks neliss. Good of you to reply! :slight_smile: :slight_smile:

I’m wondering if open_sensors only takes on a value if the alarm is armed? Because my alarm is disarmed right now, lots of sensors are ‘open’ - and open_sensors is still ‘null’

I can’t use the notification editor because I want to ultimately create an automatic TTS announcement - so I’m guessing I’d have to do this using the actions editor and rig up some input_booleans?

Don’t know if this helps but you can notify to TTS since a few versions.

1 Like

cool! It probably very much DOES! Thanks

open_sensors is only populated when you try to arm but it failed (the problematic sensors are listed) or when the alarm is triggered (the sensor that triggered is listed).
So it is not surprising that the data is empty when the system is disarmed.

Works a TREAT setting up the Google TTS as a NOTIFY service then targeting that from Alarmo. Generates a nice MP3 with details of which sensors are preventing the arming!

Good to hear! :+1:
I’m planning to improve the way open_sensors is converted to human-readable text, so it should become even better.

This topic has helped me solve a long-standing issue with some template sensors. I think I found a bug but I don’t know enough about templates to know for sure.

Here is a simplified use case that demonstrates the issue:

{% if 1 == 1 %}
  Result
{% else %}
  None
{% endif %}

As expected, Result is returned. But…

{% if 1 == 2 %}
  Result
{% else %}
  None
{% endif %}

does not work. If I change ‘None’ to anything else, it does work.

{% if 1 == 2 %}
  Result
{% else %}
  None.
{% endif %}

works.

{% if 1 == 2 %}
  Result
{% else %}
  none
{% endif %}

works. Only ‘None’ fails. The sensors worked in the past but sometime in the last few months, they quit.

It’s not a bug, None is a keyword in jinja that means ‘nothing’. Typically setting a sensor to none will cause an error. Use ‘unknown’ or ‘unavailable’ instead.

shouldn’t this be

is not none

and

is none

because ‘==’ is used to check if a state to state_attr has an explicit value (or not), while the check for none is an existence check ? At least that’s how I always make the difference between the 2. Example in most of the automations where this is relevant:

          {{trigger.to_state is not none and
            trigger.from_state is not none and
            trigger.to_state.state != trigger.from_state.state}}

while also checking:

          {{trigger.to_state.state != 'unavailable' and
            trigger.from_state.state != 'unavailable'}}