- platform: mqtt
name: "01159038DEFF Temperature"
state_topic: "th10_02/tele/SENSOR"
unit_of_measurement: "°C"
value_template: >
{% set serial_number = '01159038DEFF' %}
{% set selection = value_json.values() | list | selectattr('Id', 'eq', serial_number) | map(attribute='Temperature') | list %}
{{ selection[0] if selection | count > 0 else 0.0 }}
payload_available: "Online"
payload_not_available: "Offline"
json_attributes_template
id: >
{% set serial_number = '01159038DEFF' %}
{% set selection = value_json.values() | list | selectattr('Id', 'eq', serial_number) | map(attribute='Id') | list %}
{{ selection[0] if selection | count > 0 else 0.0 }}
name: >
{% set serial_number = '01159038DEFF' %}
{% set selection = value_json.items() | list | selectattr('[-1].Id', 'eq', serial_number) | map(attribute='[0]') | list %}
{{ selection[0] if selection | count > 0 else 0.0 }}
Hm, this doesn’t work…
Had to add a colon after json_attributes_template, after that I get this error when checking the config.
Invalid config for [sensor.mqtt]: extra keys not allowed @ data[‘id’]. Got “{% set serial_number = ‘01159038DEFF’ %} {% set selection = value_json.values() | list | selectattr(‘Id’, ‘eq’, serial_number) | map(attribute=‘Id’) | list %} {{ selection[0] if selection | count > 0 else 0.0 }}\n”
template value is None for dictionary value @ data[‘json_attributes_template’]. Got None. (See ?, line ?).
Edit: I played around with it some more to see if I could extract the name and the ID separately. The below snippet successfully puts the Id as an attribute:
json_attributes_template: >
{% set serial_number = '0114580048AA' %}
{% set selection = value_json.values() | list | selectattr('Id', 'eq', serial_number) | map(attribute='Id') | list %}
{% set serial = selection[0] if selection | count > 0 else 0.0 %}
{{ {'Id' : serial} | tojson}}
But the below version does not work for the name, it seems to return the whole json structure from the sensor:
json_attributes_template: >
{% set serial_number = '0114580048AA' %}
{% set selection = value_json.items() | list | selectattr('[-1].Id', 'eq', serial_number) | map(attribute='[0]') | list %}
{% set name = selection[0] if selection | count > 0 else 0.0 %}
{{ {'Name' : name} | tojson}}
Resulting attributes:
Time: 2020-02-22T15:05:28
Switch1: ON
DS18B20-1: {
“Id”: “0114580048AA”,
“Temperature”: 20.6
}
DS18B20-2: {
“Id”: “01145800E7AA”,
“Temperature”: 20.2
}
DS18B20-3: {
“Id”: “011458072EAA”,
“Temperature”: 20.3
}
DS18B20-4: {
“Id”: “011458078CAA”,
“Temperature”: 20.3
}
DS18B20-5: {
“Id”: “0114580868AA”,
“Temperature”: 20.4
}
DS18B20-6: {
“Id”: “0114580936AA”,
“Temperature”: 20.3
}
DS18B20-7: {
“Id”: “0114581138AA”,
“Temperature”: 20.3
}
DS18B20-8: {
“Id”: “011927495402”,
“Temperature”: 20.3
}
TempUnit: C
try this for the name:
{% set serial_number = '01159038DEFF' %}
{% set selection = value_json.items() | list | selectattr('1.Id', 'eq', serial_number) | map(attribute='0') | first %}
{{ selection if selection else 'NA' }}
Had to tweek the output format slightly, this one works
json_attributes_template: >
{% set serial_number = '0114580048AA' %}
{% set selection = value_json.items() | list | selectattr('1.Id', 'eq', serial_number) | map(attribute='0') | first %}
{% set name = selection if selection else 'NA' %}
{{ {'Name' : name} | tojson}}
And to get both name and serial as attributes I’m now using this:
json_attributes_template: >
{% set serial_number = '0114580048AA' %}
{% set selection = value_json.items() | list | selectattr('1.Id', 'eq', serial_number) | map(attribute='0') | first %}
{% set name = selection if selection else 'NA' %}
{% set selection = value_json.values() | list | selectattr('Id', 'eq', serial_number) | map(attribute='Id') | list %}
{% set serial = selection[0] if selection | count > 0 else 0.0 %}
{{ {'Id' : serial, 'Name' : name} | tojson}}
Thanks again @petro