MQTT sensor, how to use with attributes?

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:

It’s value_json not value.json

If the payload looks like this:

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

then to reference the value of EF00/0212 you would use this:

value_json.ZbReceived.Computer_Air_Quality['EF00/0212']

The strange fact is that the attribute is showed even without extracting the json. If I add this, it will show exactly what is supposed to " { “string_state”: “test” } But instead of the “test”, seems hard to do some conditions…

Then there’s a fundamental problem here.

The example I had posted earlier in the thread is biased on a working example I posted 2 years ago:

Perhaps something has changed in how MQTT Sensor processes its json_attributes_template option.

Last test:

    json_attributes_template: >
      { "string_state": {{ 'on' if true else 'off' }} }

I know that post and I was hoping that you will answer to this post as well :smiley:

I will do the test now, just a sec

Edit: Not showing the attribute

Edit2: WAIT :smiley: I have a result

Edit3:

With this

    json_attributes_template: >
      { "string_state": "{{ 'on' if true else 'off' }}" }

I see the attribute with state “on”. So… I needed to add some double quotas around the {{ }}

Look in Logs for any error/warning messages related to this MQTT Sensor.

I have a result here as well, I just added those double quotas, seems to work…

Still… the set stat still not working…

Where did you add double quotes? What you posted looks identical to what I had suggested.

This is what you gave me as code

    json_attributes_template: >
      { "string_state": {{ 'on' if true else 'off' }} }

And this is where I added

    json_attributes_template: >
      { "string_state": "{{ 'on' if true else 'off' }}" }

Strange again… take a look


This is the result of:

    json_attributes_template: >
      { "string_state": "{{ value_json.ZbReceived.Computer_Air_Quality['EF00/0212'] }}" }

And this:


is the result of:

    json_attributes_template: >
      { "string_state": "{{ 'on' if value_json.ZbReceived.Computer_Air_Quality['EF00/0212'] < 1000 }}" }

And it is going through all the details (temperature, humidity and so on…)

You’re trying to confuse me, right? :slightly_smiling_face:

The single-line template you just posted isn’t the multi-line template you had posted in the message where you first said you added double-quotes.

OK, understood. The result of the template isn’t automatically interpreted to be a string so, to create a valid JSON string value, we must explicitly wrap the template’s result in double-quotes.

Based on that information, try the original suggestion with the addition of double-quotes.

    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') }}" }
1 Like

It is working :partying_face: :partying_face:

But still, how this “this.state” knows that it is about the state of ['EF00/0212'] ?

Edit: I am stupid, don’t mind me :))) I understood now

1 Like

Whew! Glad to hear it’s working now because I was running out of ideas. :slightly_smiling_face:

1 Like

I can’t thank you enough.

I am now trying to understand how to modify this, in order to have the same last result after the HA restarts. Because if I restart HA, for just a second, the state of the sensor will be unknown, then the value it will be shown… and so on… :slight_smile:

Is the device that publishes this payload:

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

publishing it as a retained message?

Oh ok, I think that this should be done from Tasmota, per device.

I wanted to ask you… what this " | default(‘unknown’)" is doing? I dont understand, can you please explain to me?