MQTT Sensor Attribute as a Automation Condition

I am using Ionut’s Ariela app and having it publish my wifi status to Home Assistant. Per the instructions, my configuration looks like this:

  sensor:
   - platform: mqtt
     name: "wifi Status"
     state_topic: "homeassistant/sensor/android/state"
     value_template: "{{ value_json.status }}"
     json_attributes:
       - ssid
       - link_speed
       - ip

Which I’m sure is right. I have an automation that triggers whenever it gets an update from the mqtt sensor (which ariela sends regularly). I only want it to turn my lights on if I am connected to my SSID. My automation looks like this:

 - id: '12345'
   alias: Lights On Evening
   trigger:
   - platform: mqtt
     topic: homeassistant/sensor/android/state
   condition:
   - condition: template
     value_template: "{{ (state_attr('sensor.wifi_staus.ssid')|string) == 'MY_SSID' }}"
   action:
   - data:
       entity_id: switch.main_lights
     service: switch.turn_on

I think I don’t have my condition setup right. Do I have the right idea? What do I need to change? I checked it online and it says this is valid YAML, but something must be wrong. Any help is appreciated.

Help us, first. Please read the big blue info bar at the top of this page…

I am not sure I understand the question. How can you get the condition or control anything if you are NOT connected to the WiFi SSID?

I have my router setup to port forward home assistant so I can access it remotely. So HA can always know what my wifi is.

You still haven’t formatted your pasted code correctly. You quoted it rather than using the block quote button </>

Sorry, Fixed

1 Like
     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.