How to use single and double quotes

Hi,

So I have a problem, that I thought I’d solved, but something in the mean time has changed, either on the home assistant side, or on my side. I was stripping out a bluetooth sensor attribute to create a stand alone sensor, which worked fine. The output I was looking for was a mac address, which I got. However, the output seems to have changed to now include single quote marks.
This creates a problem, as I had created another sensor that took that mac address and gave it a name.

      phone_bt:
        entity_id: sensor.bt_connection
        value_template: "{%if states.sensor.bt_connection.state == '[]' %}Nothing{% elif states.sensor.bt_connection.state == '[XX:XX:XX:XX:XX:XX]' %}Helmet{% else %}Unknown{% endif %}"

This was working fine, and the sensor was outputting the mac address as expected.
[XX:XX:XX:XX:XX:XX]

Sometime recently though, the sensor began outputting the mac address as follows.
[‘XX:XX:XX:XX:XX:XX’]
Note the new single quotes.

How can I add them into the template above?

Try this:

value_template: >
  {% if states('sensor.bt_connection') == '[]' %}
    Nothing
  {% elif states('sensor.bt_connection') == "['XX:XX:XX:XX:XX:XX']" %}
    Helmet
  {% else %}
    Unknown
  {% endif %}

Also please heed this warning for all your templates:

https://www.home-assistant.io/docs/configuration/templating/#states

The other option would be to fix sensor.bt_connection but you have not shown the configuration for that. IT could be quite possible to add the none and unknown states directly to it too, so you would not need two sensors.

Okay, before I go messing with it, I’ll post my config for the sensors, in case, as you say, there might be a way to fix sensor.bt_connection

  - platform: template
    sensors:
#
### Break out the paired and connected attribute from Paul Bluetooth sensor
#
      bt_connection:
        friendly_name: "Bluetooth Connection"
        value_template: '{{ state_attr("sensor.paul_bluetooth_connection", "connected_paired_devices") }}'
#
# Assigning output to new sensor taken from bt_connection
#
      phone_bt:
        entity_id: sensor.bt_connection
        value_template: "{%if states.sensor.bt_connection.state == '[]' %}Nothing{% elif states.sensor.bt_connection.state == '[00:1D:DF:91:E8:FE]' %}Helmet{% else %}Unknown{% endif %}"

To clarify, sensor.paul_bluetooth_connection comes direct from the companion app, and lists the number of connections, with the the attributes “connected not paired devices”, “connected paired devices”, and “paired devices”.

I really appreciate the help.

  - platform: template
    sensors:
      phone_bt:
        friendly_name: "Bluetooth Connection"
        value_template: >
          {% set mac = state_attr("sensor.paul_bluetooth_connection", "connected_paired_devices")|default(none) %}
          {% if mac == '[]' %}
            none
          {% elif mac == "['XX:XX:XX:XX:XX:XX']" %}
            Helmet
          {% else %}
            unknown
          {% endif %}

This eliminates the intermediate step of stripping out the attribute from the original sensor and avoids needlessly creating that middle sensor? Cool.

I’ll give this a try and see how it goes. Thanks also for the bit on states.sensor.temperature.state
I cobbled together the sensors from older posts.

Doesn’t seem to have worked. The sensor is showing “unknown”.
In case the single quotes were being introduced somehow by my intermediate step, I removed them from around the mac address and returned the double quotes with single, but same result.

Can you copy and paste the attributes into formatted code blocks?

Or take a screen shot of the Dev Tools > States. You can obscure the last couple of bytes if you want but try not to obscure the actual surrounding content.

I got it to work. I went back to your 1st suggestion, before you revised with the two sensors down to one and tried that code and it works.

As my coding experience is minimal, do you have any idea why maintaining both sensors (the purpose of the first to strip out the attribute and the 2nd to interpret the output) worked, but streamlining the process to only create one sensor failed?

I did check to make sure I’d not made any errors adding the code, so I think I’m clear on that part.

The only thing I added was the default and it’s not really needed, this is equivalent to the two sensors you had before:

  - platform: template
    sensors:
      phone_bt:
        friendly_name: "Bluetooth Connection"
        value_template: >
          {% set mac = state_attr("sensor.paul_bluetooth_connection", "connected_paired_devices") %}
          {% if mac == '[]' %}
            none
          {% elif mac == "['XX:XX:XX:XX:XX:XX']" %}
            Helmet
          {% else %}
            unknown
          {% endif %}

Hmm, that’s strange then. I only commented out the single sensor version, so I may check it again tomorrow and remove the default, and see what happens.

Currently it’s working as expected.
Thanks for the help. :slight_smile:

Your topic is named “How to use single and double quotes” and I was wondering the same thing because It’s confusing. I’ve found some anwers: Difference Between Single and Double Quotes in Python - AskPython and Beginner Python Tutorial 19 - Double vs Single Quotes - YouTube so I guess it doesn’t matter, but if you open with a single quote or double quote then you have to close with that same character and the next characters are going to cause issues. You can escape a single qoute or double qoute with .

The attribute can contain more than one mac adress, so that may be the problem. So the template should check if the mac adress is in the string:

  - platform: template
    sensors:
      phone_bt:
        friendly_name: "Bluetooth Connection"
        value_template: >
          {% set mac = state_attr("sensor.paul_bluetooth_connection", "connected_paired_devices") |default(none) %}
          {% if mac == '[]' %}
            none
          {% elif "['XX:XX:XX:XX:XX:XX']" in mac %}
            Helmet
          {% else %}
            unknown
          {% endif %}

For curiosity, my attribute doesn’t have brackets neither quotes (and i am using the companion app also, maybe there is a difference between android and ios?), so my elif would look like this:

connected_paired_devices: FC:1D:43:B4:98:DF, D0:1B:49:DB:1D:16

{% elif "D0:1B:49:DB:1D:16" in mac %}

That was probably going to be a question I would have needed to ask at a later date, the multiple mac addresses within the attribute. So much thanks for that :slight_smile:

It may indeed be an android thing, but I’ve no experience with ios so I can’t know for sure.