123
(Taras)
February 25, 2023, 5:34pm
21
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']
PickOne
February 25, 2023, 5:37pm
22
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…
123
(Taras)
February 25, 2023, 5:43pm
23
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:
Yes. Check the documentation for the MQTT Sensor integration and you’ll find json_attributes_template .
Basically, if the payload is published as a JSON dictionary, the template (assigned to json_attributes_template) is used to parse the payload and assign its values to separate attributes.
However, each payload must contain all attributes. In other words, you can’t send one payload containing values for one attribute, followed by another payload containing a value for some other attribute. Ea…
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' }} }
PickOne
February 25, 2023, 5:44pm
24
I know that post and I was hoping that you will answer to this post as well
I will do the test now, just a sec
Edit: Not showing the attribute
Edit2: WAIT 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 {{ }}
123
(Taras)
February 25, 2023, 5:47pm
25
Look in Logs for any error/warning messages related to this MQTT Sensor.
PickOne
February 25, 2023, 5:49pm
26
I have a result here as well, I just added those double quotas, seems to work…
PickOne
February 25, 2023, 5:51pm
27
Still… the set stat
still not working…
123
(Taras)
February 25, 2023, 6:07pm
28
Where did you add double quotes? What you posted looks identical to what I had suggested.
PickOne
February 25, 2023, 6:08pm
29
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' }}" }
PickOne
February 25, 2023, 6:12pm
30
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…)
123
(Taras)
February 25, 2023, 6:16pm
31
You’re trying to confuse me, right?
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
PickOne
February 25, 2023, 6:18pm
32
It is working
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
123
(Taras)
February 25, 2023, 6:30pm
33
Whew! Glad to hear it’s working now because I was running out of ideas.
1 Like
PickOne
February 25, 2023, 6:33pm
34
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…
123
(Taras)
February 25, 2023, 6:36pm
35
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?
PickOne
February 25, 2023, 6:38pm
36
Oh ok, I think that this should be done from Tasmota, per device.
PickOne
February 25, 2023, 6:40pm
37
I wanted to ask you… what this " | default(‘unknown’)" is doing? I dont understand, can you please explain to me?
123
(Taras)
February 25, 2023, 6:43pm
38
If this.attributes.string_state
doesn’t exist then the default
filter reports unknown
.
1 Like
PickOne
February 25, 2023, 6:45pm
39
Thank you. I can sleep well tonight
Hi again @123
I have a small issue on the same subject… maybe you know what is happening.
I have this sensor which you helped me with:
- name: Bedroom Minibar Ambient Battery
state_topic: "tele/ZigBee_Bridge/0253/SENSOR"
unit_of_measurement: "%"
device_class: battery
value_template: >
{% if is_number(value_json['ZbReceived']['Bedroom_Minibar_TempHum_Sensor']['BatteryPercentage'] | float | round(0)) %}
{{ value_json['ZbReceived']['Bedroom_Minibar_TempHum_Sensor']['BatteryPercentage'] | float | round(0) }}
{% else %}
{{ states('sensor.bedroom_minibar_ambient_battery') }}
{% endif %}
json_attributes_topic: tele/ZigBee_Bridge/0253/SENSOR
json_attributes_template: >
{ "string_state":
"{{ 'low' if this.state | float(0) <= 30 else
'normal' if this.state | float(0) > 30 else
this.attributes.string_state | default('unknown') }}" }
The problem is that the attribute will only change it’s state if the sensor state changes the second time… So, if I will receive a lower value than 30, the attribute state will change only if the sensor will receive another lower value… Same for higher than 30… I don’t understand why is like this…
Any idea?
Thank you in advance!