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.
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”.
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.
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.
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
It may indeed be an android thing, but I’ve no experience with ios so I can’t know for sure.