In 'template' condition: UndefinedError: 'dict object' has no attribute

Hi guys!

Can someone please tell me what I am doing wrong with this automation and how can I repair it?

alias: MQTT2Var - Computer Room Hum Sensor
description: ''
trigger:
  - platform: mqtt
    topic: tele/ZigBee_Bridge/SENSOR
condition:
  - condition: template
    value_template: >-
      {{ 'Humidity' in
      trigger.payload_json['ZbReceived']['Computer_TempHum_Sensor'] }}
action:
  - service: variable.set_variable
    data:
      variable: computer_temphum_humidity
      value: >-
        {{trigger.payload_json['ZbReceived']['Computer_TempHum_Sensor']['Humidity']}}
mode: single

I am getting too many warning messages in the log, similar to this:

2021-06-12 13:53:26 WARNING (MainThread) [homeassistant.components.automation] Error evaluating condition in 'MQTT2Var - Computer Room Hum Sensor':
In 'condition':
In 'template' condition: UndefinedError: 'dict object' has no attribute 'Computer_TempHum_Sensor'

It tells you there is no Computer_TempHum_Sensor attribute in ZbReceived in the json payload.

Check the actual json from MQTT in the tele/ZigBee_Bridge/SENSOR topic.

14:24:59.360 MQT: tele/ZigBee_Bridge/SENSOR = {"ZbReceived":{"Computer_TempHum_Sensor":{"Device":"0x6106","Name":"Computer_TempHum_Sensor","Humidity":58.8,"Endpoint":1,"LinkQuality":76}}}

Butā€¦ this is what tasmota is sending, looking into the console

Maybe not always?
Does the warning is shown for every updates? Is the topic shared by multiple sensors?

Not always. I forced the sensor to send datas now, and the log did not register any warningā€¦

Yes, all the sensors uses this topic

You could do

    value_template: >-
      {{ 'Computer_TempHum_Sensor' in
      trigger.payload_json['ZbReceived'] and 'Humidity' in
      trigger.payload_json['ZbReceived']['Computer_TempHum_Sensor'] }}
1 Like

I will try it now, thanks!

I forgot if tasmota is capable to do this and howā€¦ Can I have different topics for every sensor? Something from SetOption ? I donā€™t remember exactly and I search everywhereā€¦
I think is SetOption89 :slight_smile:

Now, the question isā€¦ .should I have individual topics for every sensor? I meanā€¦ every sensor is giving me 3 informations, which means that it will send 3 information to HA (temperature, humidity and battery)ā€¦ If I make a similar automation, it will trigger 3 times every the sensor refreshesā€¦ I think it could be the same result as now, right?

Up to you. Now, if you have, e.g,. 3 sensors, the same automation will be triggered 9 times.

Pay attention that wil the default ā€œsingleā€ mode of automations, if the same one is triggered while it is already running, the trigger will be ignored.
If you go that route, I recommend to use mode: queued

You just have to keep in mind that if you use a single topic, you wonā€™t always have the same information, i.e. the same json structure.

I am trying with different topics, at least, if I will have a sensor refresh, the automation will trigger 3 times and I can use ā€˜paralelā€™ with maximum number 3. Maybe it will be better :slight_smile:

Thank you for your advices.

Seems that splitting a big automation into more and changing tasmota to make every device have itā€™s own topic, doesnā€™t get me any warning like before.

Thank you again, for all advices.