Help needed with automation and json template warning

FYI, I read somewhere that rolling back to Mosquitto 5.1 will help resolve the issues, in my case it didn’t change anything…

With Zigbee2MQTT, you need to add these settings to disable it’s legacy code which causes HA 2021.4 and newer to spew warnings.

advanced:
  legacy_api: false
device_options:
  legacy: false

Refer to Still showing " Template variable warning " in HA with Z2M 1.19.1 · Issue #7824 · Koenkk/zigbee2mqtt (github.com)

2 Likes

i have add those options in the AddOn config Zigbee2Mqtt but no changes…

Perhaps a stupid question : its wrote to add thoses lines in “configuration.yaml”, it is under the AddOn Zigbee2Mqtt configuration or under HassOs /config/configuration.yaml ???

Thanks…i m lost…

1 Like

thanks a lot @dbrunt !

i just realise that this config is different from the AddOn config…i am used to modify the config in the AddOn not under “config/Zigbee2mqtt/configuration.yaml”…

Is it normal that my config there is different to the AddOn’s one please?

EDIT : i put the 2 option legacy but still the warning “Template variable warning: ‘dict object’ has no attribute ‘voltage’ when rendering ‘{{ value_json.voltage }}’” is still there.

That is what I see also but I cannot comment about the differences. I too am using the add-on…

I added default, and I still getting the rendering error

Logger: homeassistant.helpers.template
Source: helpers/template.py:1834
First occurred: 13:43:45 (2 occurrences)
Last logged: 13:44:25

Template variable error: 'dict object' has no attribute 'DeepSleep' when rendering '{{value_json.DeepSleep. Time | default | as_timestamp | timestamp_custom('%H:%M:%S %d/%m/%Y') }}'

Mine seems to be odd however.
It occurs when I get a response in tasmota console from DeepSleepTime, when it should be looking for DeepSleep. Time
I tried it with DeepSleep.Epoch also, same issue

Is there a way to isolate value_json so the error is not triggering from the wrong response?

MQTT Sensor config

- platform: mqtt
    name: "Pool Roof Duration"
    state_topic: "stat/ds1820/RESULT"
    value_template: '{{value_json.DeepSleepTime | timestamp_custom("%H:%M", False) }}'
    icon: "mdi:clock"
    unit_of_measurement: "HRs"        
    
    
  - platform: mqtt
    name: "Pool Roof Deeptime"
    state_topic: "stat/ds1820/RESULT"
    value_template: "{{value_json.DeepSleep. Time | default | as_timestamp | timestamp_custom('%H:%M:%S %d/%m/%Y') }}"
    icon: "mdi:clock"

Tasmota console

13:44:25.312 MQT: stat/ds1820/RESULT = {"DeepSleepTime":"60"}

13:45:04.414 MQT: stat/ds1820/RESULT = {"DeepSleep":{"Time":"2022-03-07T13:51:00","Epoch":1646621460}}

This is telling you it doesn’t have DeepSleep

You’re not providing a default for deep sleep, you’re providing a default for Time.

Lastly, you already have the timestamp

value_template: >
  {% set deepsleep = value_json.DeepSleep | default(none) %}
  {{ deepsleep.Epoch | timestamp_custom('%H:%M:%S %d/%m/%Y') if deepsleep else none }}
1 Like

The incoming “DeepSleepTime” result triggers the error, it only occurs from that result not both.

13:44:25.312 MQT: stat/ds1820/RESULT = {"DeepSleepTime":"60"}

The nested {“DeepSleep”:{ “Time”} result doesn’t trigger an error. With or without the default.

13:45:04.414 MQT: stat/ds1820/RESULT = {"DeepSleep":{"Time":"2022-03-07T13:51:00","Epoch":1646621460}}

Thanks, I’ll try value_template.

value_template: >
  {% set deepsleep = value_json.DeepSleep | default(none) %}
  {{ deepsleep.Epoch | timestamp_custom('%H:%M:%S %d/%m/%Y') if deepsleep else none }}

Sensor is sleeping for the night now, so will be awhile.
Thanks again

If this is an event stream, you’re going about this in the wrong way IMO.

I’d use a template sensor with an MQTT trigger. Every time DeepSleep or DeepSleepTime alternates in that state_topic, the other opposing sensor will go unknown or unavailable.

1 Like

Sorry can you expand a bit on what you mean by event stream?

If It can be done the wrong way, that’s usually how I do it… :rofl:

I send MQTT cmd > DeepSleepTime to the Tasmota Device,because deepsleeptime doesn’t send in the normal sensor array.

that triggers the RESULT/DeepSleepTime: 60 (or whatever) from Tasmota.

RESULT/DeepSleepTime: ## triggers the automation to change the sleep period.

I have another post around with some of the automation

You’re using a single topic that has a different shape of data. That means this isn’t holding a state, it’s telling you information. You need to handle the information as it comes.

template:
- trigger:
  - platform: mqtt
    topic: stat/ds1820/RESULT
    value_template: "{{ value_json.DeepSleep is defined }}"
  sensor:
  - name: Pool Roof Deeptime
    state: "{{ value_json.DeepSleep.Epoch | timestamp_custom('%H:%M:%S %d/%m/%Y') }}"


- trigger:
  - platform: mqtt
    topic: stat/ds1820/RESULT
    value_template: "{{ value_json.DeepSleepTime is defined }}"
  sensor:
  - name: Pool Roof Duration
    state: "{{ value_json.DeepSleepTime | timestamp_custom("%H:%M", False) }}"

Each sensor will only trigger when the information is provided in the topic, and your sensor will always persist with the last sent data.

1 Like

however looking at your automations, it seems that you’re the one making this complicated. Why are you using a single topic to make this work? Use separate topics and you won’t be having any of these issues.

Nevermind, you’re sending a command, missed that in the publish.

1 Like

Well mostly because it was working fine. It still does TBH, just has the error message.
I suppose I can make a rule in tasmota to split the topic though.
I will play around with it.
Cheers.

Thanks Petro,

That fixed the original error. So I don’t actually need the templates to work.

- platform: mqtt
    name: "Pool Roof Deeptime"
    state_topic: "stat/ds1820/RESULT"
    value_template: >
       {% set deepsleep = value_json.DeepSleep | default(none) %}
       {{ deepsleep.Epoch | timestamp_custom('%H:%M:%S %d/%m/%Y') if deepsleep else none }}
    icon: "mdi:clock"

FYI These two don’t work.

template:
- trigger:
  - platform: mqtt
    topic: stat/ds1820/RESULT
    value_template: "{{ value_json.DeepSleep is defined }}"
  sensor:
  - name: Pool Roof Deeptime Test
    state: "{{ value_json.DeepSleep.Epoch | timestamp_custom('%H:%M:%S %d/%m/%Y') }}"


- trigger:
  - platform: mqtt
    topic: stat/ds1820/RESULT
    value_template: "{{ value_json.DeepSleepTime is defined }}"
  sensor:
  - name: Pool Roof Duration Test
    state: "{{ value_json.DeepSleepTime | as_timestamp | timestamp_custom('%H:%M', False) }}"       

Spits out the following errors

Logger: homeassistant.helpers.template
Source: helpers/template.py:1834
First occurred: 12:09:59 (6 occurrences)
Last logged: 12:20:10

Template variable error: 'value_json' is undefined when rendering '{{ value_json.DeepSleep.Epoch | timestamp_custom('%H:%M:%S %d/%m/%Y') }}'
Template variable error: 'value_json' is undefined when rendering '{{ value_json.DeepSleepTime | timestamp_custom('%H:%M', False) }}'
Template variable error: 'value_json' is undefined when rendering '{{ value_json.DeepSleepTime | as_timestamp | timestamp_custom('%H:%M', False) }}'

and

Logger: homeassistant.components.template.sensor
Source: components/template/trigger_entity.py:154
Integration: Template (documentation, issues)
First occurred: 12:09:59 (6 occurrences)
Last logged: 12:20:10

Error rendering state template for sensor.pool_roof_deeptime_test: UndefinedError: 'value_json' is undefined
Error rendering state template for sensor.pool_roof_duration_test: UndefinedError: 'value_json' is undefined

Ah yes, I’ll look in the morning. I see what I did wrong.

1 Like

Just for reference: here is the solution for the trigger based approach by using a template based sensor

The solution I posted doesn’t work because you need to set payload to true. So simply add

payload: true

To the trigger

Apparently I forgot to look in the morning

All good. I split it into separate the mqtt topics if I recall.