MQTT sensor, how to use with attributes?

Hi!

Anyone can help me, please? I am trying to make a MQTT sensor which will show me the temperature as state and a “string state” as attribute, but I don’t know how to use that json attribute option.

This is what I have till now:

- name: Computer Ambient Temperature
    state_topic: "tele/ZigBee_BridgeV/545C/SENSOR"
    unit_of_measurement: "°C"
    icon: mdi:thermometer
    value_template: >
      {% if is_number(value_json['ZbReceived']['Computer_Air_Quality']['EF00/0212'] | multiply(0.1) | float | round(1)) %}
      {{ value_json['ZbReceived']['Computer_Air_Quality']['EF00/0212'] | multiply(0.1) | float | round(1) }}
      {% else %}
      {{ states('sensor.computer_ambient_temperature') }}
      {% endif %}
    json_attributes_topic: "tele/ZigBee_BridgeV/545C/SENSOR"
    json_attributes_template: >
      { "string_state": "
        {% set stat = states('sensor.computer_ambient_temperature') %}
        {% if stat <= 13 %} {{ 'very low' }}
        {% elif stat >= 15 and stat <= 18 %} {{ 'low'  }}
        {% elif stat >= 20 and stat <= 25 %} {{ 'normal' }}
        {% elif stat >= 27 and stat <= 30 %} {{ 'high' }}
        {% elif stat >= 32 %} {{ 'very high' }}
        {% else %}
        {{ state_attr('sensor.computer_ambient_temperature', 'string_state') }}
        {% endif %}
        | tojson " }

Thank you in advance!

json_attributes_template is meant to extract information directly from the JSON payload received by tele/ZigBee_BridgeV/545C/SENSOR. Refer to the example in the documentation and you’ll see that you’re not using it the way it was meant to be used.

Yes, I know, but there is any way to use as pure template, without extracting the json? Or to convert the template to a json? I don’t know… I just want to achieve something like in my bad code, but to work :slight_smile:

Let me get this straight, you’re asking if an option meant exclusively for extracting JSON data can be used for not extracting JSON data? :slightly_smiling_face:

I want to add some custom attributes to my mqtt sensor… Attributes which are the result from some conditions, like: mqtt_attribute name “string_state”, mqtt_value " if a =b result should be “1”, if a != b result should be “2”… Something like this, which have nothing to do with the json extraction

Something like “attributes_template” should work, but there is no option like this :smiley:

Exactly.

The available option is for creating attributes directly from the received payload.

Can you explain me more? :smiley:

I think that the only way to achieve what I want is from the value_template… adding two values then to split them when I add it to lovelace… :expressionless: But this is a little silly way do to it… There is no other way?

Perhaps this will help to explain how you can use json_attributes_template to create attributes.

Let’s say the JSON payload looks like this:

{ "used_space": 154, "sys_clock_speed": 1.1, "temp": 35 }

You can create attributes like this:

    json_attributes_template: >-
      { "used": {{ value_json.used_space * 1000000 }},
        "speed": {{value_json.sys_clock_speed * 1000 }},
        "temp": {{ (value_json.cpu_temp * 9/5) + 32 }} } 

The example shows how three attributes are created (with different names from the ones in the payload) and each attribute’s value is computed by a template.

1 Like

I understand… :expressionless: So… there is no way to trick the json_attributes_template to use some custom conditions template… :frowning: too bad…

The example contains templates so conditions are possible within the template.

json_attributes_template: >-
      { "used": {{ iif(value_json.used_space > 150, 'high', 'low') }},
        "speed": {{ 'normal' if value_json.sys_clock_speed < 1
                   else 'elevated' if 1 <= value_json.sys_clock_speed < 2
                   else 'high' }},
        "temp": {{ (value_json.cpu_temp * 9/5) + 32 }} }

hmmmm… So you can add templates but must have something to do with the json… Ok, I will try some custom things :slight_smile: Thank you for your time

1 Like

I have tried this:


json_attributes_template: >-
      { 'string_state': {{ 'very low' if value.json['EF00/0212'] | multiply(0.1) | float | round(1) <= 13
      else 'low' if value.json['EF00/0212'] | multiply(0.1) | float | round(1) >= 15 and <= 18
      else 'normal' if value.json['EF00/0212'] | multiply(0.1) | float | round(1) >= 20 and <= 25
      else 'high' if value.json['EF00/0212'] | multiply(0.1) | float | round(1) >= 27 and <= 30
      else 'very high' if value.json['EF00/0212'] | multiply(0.1) | float | round(1) >= 32 }} }

But I get Invalid config for [mqtt]: invalid template :frowning:

Edit: the mistake could be from here? ’ >= 15 and <= 18 `

There is any way to declare “variable” Like… set test = value.json… ?

Try this:

    json_attributes_template: >
      { "string_state": 
        {{ 'very low' if this.state | float(0) <= 13 else
           'low' if 15 <= this.state | float(0) <= 18 else
           'normal' if 20 <= this.state | float(0) <= 25 else
           'high' if 27 <= this.state | float(0) <= 30 else
           'very high' if this.state | float(0) >= 32 else
           this.attributes.string_state | default('unknown') }} }

I don’t have the means to easily test it so I am not sure if MQTT Sensor accepts this.state. I assume it does but you’ll know for sure after testing it.


EDIT

Correction. Overlooked to terminate the template with }} before the dictionary’s closing }

Invalid config for [mqtt]: invalid template :frowning:

Edit: I have added " }} " at the end of te code, now HA is restarting :smiley:

Edit2: Does not work, I need to test more

This is the value from the topic which I receive:


{
  "ZbReceived": {
    "Computer_Air_Quality": {
      "Device": "0x545C",
      "Name": "Computer_Air_Quality",
      "EF00/0212": 243,
      "Endpoint": 1,
      "LinkQuality": 107
    }
  }
}

243, meaning 24.3 °C

Perhaps it doesn’t accept the this variable.

As an experiment, try this version.

json_attributes_template: >
      { "string_state": 
        {% set stat = states('sensor.computer_ambient_temperature') | float(0) %}
        {{ 'very low' if stat <= 13 else
           'low' if 15 <= stat <= 18 else
           'normal' if 20 <= stat <= 25 else
           'high' if 27 <= stat <= 30 else
           'very high' if stat >= 32 else
           state_attr('sensor.computer_ambient_temperature', 'string_state') }} }

Interesting aproaching, I am restarting HA now

Edit: nope, it is not even showing the attribute

You shouldn’t have to restart Home Assistant to load modified MQTT Sensors.

Go to Settings > Developer Tools > YAML and click Check Configuration to confirm there are no YAML syntax errors.

Then execute Settings > Developer Tools > YAML > Reload Manually Configured MQTT Entities

Oh yes, you are right. I almost forgot about this

That implies it doesn’t like having a Jinja2 statement (the set stat) inside of the dictionary’s definition.

Just for fun, see if this works. If it doesn’t then there’s a fundamental problem here.

    json_attributes_template: >
      { "string_state": 
        {{ 'very low' if 28 <= 13 else
           'low' if 15 <= 28 <= 18 else
           'normal' if 20 <= 28 <= 25 else
           'high' if 27 <= 28 <= 30 else
           'very high' if 28 >= 32 else
           'foo' }} }

Still not showing the attribute :frowning:

If the MQTT sensor had an option similar to attribute_tamplate… :heart_eyes: