Is_state_attr inconsistent results

In dev tools / template, when I type

{{ state_attr('sensor.home_alarm_keypad','alpha') }}

I get

But when I type

{{ is_state_attr('sensor.home_alarm_keypad','alpha','DISARMED CHIME   Ready to Arm') }}

I get

According to dev tools:

Also tried

{{ is_state('sensor.home_alarm_keypad','DISARMED CHIME Ready to Arm') }}

I get:

What am I missing ?

Home Assistant 0.116.2

arch armv7l
chassis embedded
dev false
docker true
docker_version 19.03.11
hassio true
host_os HassOS 4.13
installation_type Home Assistant OS
os_name Linux
os_version 4.19.127-v7l
python_version 3.8.5
supervisor 247
timezone America/New_York
version 0.116.2
virtualenv false
{{ state_attr('sensor.home_alarm_keypad','alpha') == 'DISARMED CHIME   Ready to Arm' }}

Hello David,

Thanks for your reply. Trying what you propose yields the following:

I think this statement is equivalent to the two previous statement I posted, or at least, I think they should be. Correct me if I’m wrong.

There seems to be a hidden character somewhere. This state is the output of the LCD of my alarm. I suspect the comparison may be done with a non-printable character. Is there a way to convert:

{{ state_attr('sensor.home_alarm_keypad','alpha') }}

Into hex to see if that’s the case?

Possibly a tab character between “DISARMED CHIME” and “Ready to arm”?

Try this:

{{ 'Ready to arm' in state_attr('sensor.home_alarm_keypad','alpha') }}

It doesn’t attempt to match the entire value of alpha, just a portion of it.

1 Like

I carried out the following simple experiment:

If the whitespace between “ DISARMED CHIME ” and “ Ready to arm ” is a tab character then you can use \t to match it.

{{ is_state_attr('sensor.home_alarm_keypad','alpha','DISARMED CHIME\tReady to Arm') }}

1 Like
{{ is_state_attr('sensor.home_alarm_keypad','alpha','DISARMED CHIME\tReady to Arm') }}

Still yields false, but

{{ 'Ready to arm' in state_attr('sensor.home_alarm_keypad','alpha') }}

yield true, so there is a non-printable character after Ready to arm, but it’s not a tab. I don’t care atm about finding out, as I only need either the part before OR after that non-printable character. Therefore, ‘Ready to arm’ in state_attr() works for what I’m looking to do.

Good catch, thank you !

1 Like

It could be that there is both space and tab.

tried space space tab, tab space space, space tab space, space tab, tab space, none of them works. Could it be a line change ?

If you are curious to know what’s in the whitespace, you can use the urlencode filter. For example, this reveals the whitespace to be a tab character (%09).

ASCII Table

1 Like

I had no idea that was possible.
In that case it should be possible to compare the state | urlencode with the output of the same.

Sure, you could do that, but it makes it a bit less ‘friendly’:

{{ state_attr('sensor.home_alarm_keypad','alpha') | urlencode == 'DISARMED%20CHIME%20%09%20%20Ready%20to%20Arm' }}

There’s no tab. There was one space before the whole string, and two spaces after the whole string, including the three space inbetween. HA doesn’t display spaces before/after:
image

Thanks for the finding!

1 Like

Now that you know the problem is due to leading and trailing spaces, you can get rid of them using the trim filter.

This screenshot demonstrates how the trim filter eliminates the leading/trailing spaces from the supplied string. urlencode is used only to show that the spaces have been removed.

1 Like