MQTT Sensor Attribute as a Automation Condition

     value_template: "{{ state_attr('sensor.wifi_status', 'ssid') == 'MY_SSID' }}"

You almost had it.

value_template: "{{ (state_attr('sensor.wifi_staus.ssid', 'ssid') == 'MY_SSID' }}"

Edit: dammit. too slow again.

That didn’t work :confused:

Looking at the dev tools states page does the sensor change state when you turn your phone’s wifi on/off.

Copy this line into Home Assistant’s Template Editor:

{{ state_attr('sensor.wifi_status', 'ssid') }}

Observe what it reports on the right-hand side of the page. It should be your WiFi SSID name.

That’s the string that you need to use in place of ‘MY_SSID’.

{{ state_attr('sensor.wifi_status', 'ssid') == 'MY_SSID' }}

If you’ve been using the correct string all along, or there’s no string shown, then the problem lies elsewhere.

Ben_Mick,

Did you ever get this ironed out? I’m trying to get the same type of thing working using Ariela as well. and can’t seem to get the automation to trigger. I can get the correct SSID name when I use the Template editor, but cannot seem to get the automation to trigger with {{ state_attr(‘sensor.wifi_status’, ‘ssid’) == ‘MY_SSID’ }} as a condition. I validate the config and it’s ok, but nothing triggers, even after a restart.
I’m new at this, so I have been trying to find an example that works, but no luck. It seems that the “comparison” doesn’t work right.

I found an example trigger that looks like this:

trigger:
platform: template
value_template: '{{ (state_attr("sensor.paul_s9_wi_fi_sensor","ssid")) == "AshShowroom" }}'

But this doesn’t work either…

In the following template, replace wifi_status and MY_SSID with your sensor’s name and your SSID name, then paste it into the Template Editor.

{{ state_attr('sensor.wifi_status', 'ssid') == 'MY_SSID' }} 

The result in the Template Editor should be True.

Comes up false… If I just put in

{{ state_attr(‘sensor.wifi_status’, ‘ssid’) }}

it shows the correct SSID. If I substitute that result in the comparison for ‘MY_SSID’ it comes up false.

The implication is that the string it displays for SSID may contain spaces (at either end) or special characters (like a literal newline or something else that isn’t readily visible).

I can’t simulate a wifi sensor (I don’t have one) but I can use the sun component to prove the == test works correctly.

As an experiment, try this:

{{ 'MY_SSID' in state_attr('sensor.wifi_status', 'ssid') }}

I have gotten it to work. One problem you may be facing is that when ariela publishes the ssid it includes the double quotes as part of the string. You may need to use escape characters in your configuration.

I can post what worked for me when I get home

Oh, FFS …

… then in will work because it’s a sub-string match whereas == requires a full-string match.

My config ended up looking like this:

- condition: template
  value_template: "{{ state_attr('sensor.wifi_status' , 'ssid') == '\"SSID\"' }}"

the backslash is the escape character so HA reads it as part of the string

I will try the escape characters when I get home. That makes sense. Thank you.

@toofewacres

When you examine sensor.wifi_status in the States page, does it not show your SSID delimited by double-quotes?

When you wrote this:

Did it not show your SSID delimited by double-quotes?

I created a test sensor and assigned it a state and attribute containing literal double-quotes. They are evident in the States page:
Screenshot%20from%202019-05-29%2019-50-50

and in the Template Editor:
Screenshot%20from%202019-05-29%2019-54-36

Anyway, I’m reporting this as an error to Ariela’s developer. It’s a mistake to include string-delimiters within an attribute’s value.

@123 , @Ben_Mick

You both were right on target, The output that Ariela publishes does in fact has the double quote as part of the string. Adding the escape characters and the double quotes let my automation run as I wanted.
Thanks for help.

… and it will stop working the moment the developer fixes this malformed attribute (you don’t include literal string-delimiters in the attribute’s value).

If your template uses in instead of == it will work if the ssid attribute does or does not contain literal double-quotes.

Screenshot%20from%202019-05-29%2020-37-01

Hopefully the developer will fix this quickly. At least we know what the issue is now.

The issue was evident 5 hours ago when I wrote this:

What I didn’t count on was that it was readily visible but your eyes were editing out the double-quotes surrounding the SSID value and only reporting the string between them.

When the Template Editor reports this:

"myssid"

it is not the same as this:

myssid

Lesson learned…

Hello,

The next version of Ariela will going to have the quotes of the SSID removed. Hope its ok.