Mqtt sensor mit value template value.json verursacht viele errors

Hello, as the title says, I get a lot of errors.
I don’t know what else I can try.

here is my log and the corresponding yaml files.

Logger: homeassistant.helpers.template
Quelle: helpers/template.py:2746
Erstmals aufgetreten: 09:11:06 (55 Vorkommnisse)
Zuletzt protokolliert: 09:16:13

Template variable error: 'value_json' is undefined when rendering '{{ value_json.REG_BATTERY_HOME_R_VOLTAGE | is_defined }}'
Template variable error: 'value_json' is undefined when rendering '{{ value_json.REG_INVERTER_HOME_R_INVERTER_TEMP | is_defined }}'
Template variable error: 'value_json' is undefined when rendering '{{ value_json.REG_INVERTER_HOME_R_POWER_L1_1 | is_defined }}'
Template variable error: 'value_json' is undefined when rendering '{{ value_json.REG_CUSTOM_TOTAL_SOLAR_POWER | is_defined }}'
Template variable error: 'value_json' is undefined when rendering '{{ value_json.REG_GRID_METER_R_TOTAL_ACTIVE_POWER_1 | is_defined }}'
    - name: "Alpha Inverter Voltage"
      unique_id: "alpha_inverter_voltage"
      state_topic: "alpha2mqtt/state/hour/one"
      device_class: "voltage"
      unit_of_measurement: "V"
      force_update: true
      icon: "mdi:alpha-v"
      state_class: "measurement"
      value_template: "{{ value_json.REG_INVERTER_HOME_R_VOLTAGE_L1 | is_defined }}"

Hello Chris0793,

You may be needing some of this stuff…

There are examples lower in that doc as well.

Yes, I’ve read that, but it doesn’t help me any further.

Can you provide other support? :wink:

Do you need any further information?

Your template is designed to report the value of value_json.REG_INVERTER_HOME_R_VOLTAGE_L1 if it exists. In other words, if the received payload is in JSON format and contains a key named REG_INVERTER_HOME_R_VOLTAGE_L1.

If it does not exist (not defined), it will report the error message you have received.

So it’s working exactly the way you have configured it to work. The key you specified in the template wasn’t found in the received payload.

Confirm the received payload contains data in the format you expect.

You are absolutely right.

But how do I get rid of this error?

I have no influence on the mqtt payload and not all values are transmitted in every message.

I can attach the mqtt messages here when I get home later.

The sensor should simply hold the old value until a new value is in the payload. It does this correctly, but my log is full of this error messages.

There’s the problem.

Other users have had similar problems and I have helped them fix it with an automation.

Create the following automation.

alias: mqtt filter
triggers:
  - trigger: mqtt
    topic: alpha2mqtt/state/hour/one
conditions:
  - condition: template 
    value_template: "{{ trigger.payload_json.REG_INVERTER_HOME_R_VOLTAGE_L1is defined }}"
actions:
  - action: mqtt.publish
    data:
      topic: alpha/one
      payload: "{{ trigger.payload_json.REG_INVERTER_HOME_R_VOLTAGE_L1 }}"

Then reconfigure your MQTT Sensor like this:

    - name: "Alpha Inverter Voltage"
      unique_id: "alpha_inverter_voltage"
      state_topic: "alpha/one"
      device_class: "voltage"
      unit_of_measurement: "V"
      icon: "mdi:alpha-v"
      state_class: "measurement"
      value_template: "{{ value }}"

EDIT 1

What the automation does is receive payloads from alpha2mqtt/state/hour/one. If the payload contains the desired key (REG_INVERTER_HOME_R_VOLTAGE_L1) it passes it on to the MQTT sensor via alpha/one.

If the payload does not contain the key, the automation does nothing. This “filtering” technique has proven to be very robust and ensures the MQTT Sensor only receives valid information.

FWIW, there’s another method I have suggested, that doesn’t use an automation, but users who tried it have had mixed results.

EDIT 2

Correction. The automation’s MQTT Trigger creates trigger.payload_json not value_json.

the automation works but now I get this error.

Template variable error: ‘dict object’ has no attribute ‘payload_json’ when rendering ‘{{ trigger.payload_json.REG_CUSTOM_LOAD is defined }}’

Based on that error message, it appears that sometimes the received payload doesn’t even contain data in JSON format.

Rather than ask you to post several examples of the received payloads, let’s simply make the automation’s Template Condition more stringent.

alias: mqtt filter
triggers:
  - trigger: mqtt
    topic: alpha2mqtt/state/hour/one
conditions:
  - condition: template 
    value_template: >
      {{ trigger is defined and
        trigger.payload_json is defined and
        trigger.payload_json.REG_INVERTER_HOME_R_VOLTAGE_L1 is defined }}"
actions:
  - action: mqtt.publish
    data:
      topic: alpha/one
      payload: "{{ trigger.payload_json.REG_INVERTER_HOME_R_VOLTAGE_L1 }}"

If that fails to eliminate the error message then I will need to see an example of the payload that caused the error (it will be displayed in the automation’s trace).

Any progress to report? Did you implement what I had suggested?