Sonoff Bridge MQTT Door Sensor value_json error

I have several motion sensors that I’ve set up using my Sonoff Bridge with the following config:

  - platform: mqtt
    state_topic: "tele/sonoff/RESULT"
    name: "Office Motion"
    value_template: "{{value_json.RfReceived.Data}}"
    payload_on: "E63B42"
    payload_off: "E63B42off"
    device_class: "motion"
    qos: 1

and this automation to reset after 5 seconds:

- id: '1565124258151'
  alias: OfficeMotionOff
  trigger:
  - entity_id: binary_sensor.office_motion
    for:
      seconds: 5
    platform: state
    to: 'on'
  condition: []
  action:
  - data:
      payload: E63B42off
      topic: tele/sonoff/RESULT
    service: mqtt.publish

These appear to be working correctly, however the logs are consistently filled with the following errors:

Template variable error: 'value_json' is undefined when rendering '{{value_json.RfReceived.Data}}'
Error parsing value: 'value_json' is undefined (value: E63B42off, template: {{value_json.RfReceived.Data}})

I’m pretty sure this was copied from another post somewhere in the forum and it didn’t generate an eror when I created them Can someone explain what I might be doing wrong here?

Read this topic, an excellent explanation how to work with the Sonoff Rf bridge :

Another copy-paste problem. I don’t know where this code originated, but it’s never going to work in the form you’ve posted it — the binary_sensor is expecting to receive structured JSON but your automation is sending it a simple string. Solution in my post in this topic:

I changed the code to reflect your suggestions but it made no difference. I even commented out the references to the element that shows up in the log in both automations.yaml and binary_sensors.yaml, but the error still appears in the log.

Error parsing value: 'value_json' is undefined (value: E63B42off, template: {{value_json.RfReceived.Data}})

11:39:45 – (ERROR) helpers/template.py - message first occurred at 11:39:10 and shows up 90 times

Template variable error: 'value_json' is undefined when rendering '{{value_json.RfReceived.Data}}'

11:39:45 – (ERROR) helpers/template.py - message first occurred at 11:39:10 and shows up 90 times  

Is there somewhere else that I should be looking, because it appears the errors are not being generated by statements in either of those files?

You need to restart HA for the changes to take effect. The error message is quite clear: your sensor template is expecting a different structure to what your automation is feeding it.

I restart it after every change I make. I’ve even hard rebooted the NUC to be absolutely sure.

Any reference to E63B42off is commented out in automations.yaml and binary_sensors.yaml, and yet the error still appears. Are these the only two places the template could be in?

I can’t tell you whether you have used this template anywhere else :face_with_raised_eyebrow:.

How have you commented them out? What does your code look like now?

automation.yaml:

#  - platform: mqtt
#    state_topic: "tele/sonoff/RESULT"
#    name: "Doorbell"
#    value_template: "{{value_json.RfReceived.Data}}"
#    payload_on: "{\"RfReceived\": {\"Data\": \"E63B42\"}}"
#    payload_off: "{\"RfReceived\": {\"Data\": \"E63B42off\"}}"
#    device_class: Sound
#    qos: 1

binary_sensors.yaml

#- id: '1606332626741'
#  alias: DoorBellRing
#  description: Doorbell ringing
#  trigger:
#  - entity_id: binary_sensor.doorbell
#    platform: state
#    to: 'on'
#    from: 'off'
#  condition: []
#  action:
#  - data:
#      payload: "{\"RfReceived\": {\"Data\": \"E63B42off\"}}"
#      topic: tele/sonoff/RESULT
#    service: mqtt.publish
#  - service: notify.mobile_app_sm_g955f
#    data:
#      message: Someone is at the door.
#  mode: single

Well, if you’ve restarted and haven’t (to your knowledge) used the templates anywhere else, the error should not be occurring still; and your code looks good to run as expected if uncommented. Are you sure these aren’t old errors?

2021-06-03 12:32:09 ERROR (MainThread) [homeassistant.helpers.template] Error parsing value: 'value_json' is undefined (value: E63B42off, template: {{value_json.RfReceived.Data}})
2021-06-03 12:32:09 INFO (MainThread) [homeassistant.components.mqtt.binary_sensor] No matching payload found for entity: Landing Motion with state topic: tele/sonoff/RESULT. Payload: 'E63B42off', template output: 'E63B42off', with value template 'Template("{{value_json.RfReceived.Data}}")'
2021-06-03 12:32:09 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'value_json' is undefined when rendering '{{value_json.RfReceived.Data}}'
2021-06-03 12:32:09 ERROR (MainThread) [homeassistant.helpers.template] Error parsing value: 'value_json' is undefined (value: E63B42off, template: {{value_json.RfReceived.Data}})
2021-06-03 12:32:09 INFO (MainThread) [homeassistant.components.mqtt.binary_sensor] No matching payload found for entity: Anna's Bedroom Motion with state topic: tele/sonoff/RESULT. Payload: 'E63B42off', template output: 'E63B42off', with value template 'Template("{{value_json.RfReceived.Data}}")'
2021-06-03 12:32:09 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'value_json' is undefined when rendering '{{value_json.RfReceived.Data}}'
2021-06-03 12:32:09 ERROR (MainThread) [homeassistant.helpers.template] Error parsing value: 'value_json' is undefined (value: E63B42off, template: {{value_json.RfReceived.Data}})
2021-06-03 12:32:09 INFO (MainThread) [homeassistant.components.mqtt.binary_sensor] No matching payload found for entity: Xander's Bedroom Motion with state topic: tele/sonoff/RESULT. Payload: 'E63B42off', template output: 'E63B42off', with value template 'Template("{{value_json.RfReceived.Data}}")'
2021-06-03 12:32:09 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'value_json' is undefined when rendering '{{value_json.RfReceived.Data}}'
2021-06-03 12:32:09 ERROR (MainThread) [homeassistant.helpers.template] Error parsing value: 'value_json' is undefined (value: E63B42off, template: {{value_json.RfReceived.Data}})
2021-06-03 12:32:09 INFO (MainThread) [homeassistant.components.mqtt.binary_sensor] No matching payload found for entity: Office Motion with state topic: tele/sonoff/RESULT. Payload: 'E63B42off', template output: 'E63B42off', with value template 'Template("{{value_json.RfReceived.Data}}")'
2021-06-03 12:32:09 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'value_json' is undefined when rendering '{{value_json.RfReceived.Data}}'
2021-06-03 12:32:09 ERROR (MainThread) [homeassistant.helpers.template] Error parsing value: 'value_json' is undefined (value: E63B42off, template: {{value_json.RfReceived.Data}})
2021-06-03 12:32:09 INFO (MainThread) [homeassistant.components.mqtt.binary_sensor] No matching payload found for entity: Kitchen Motion with state topic: tele/sonoff/RESULT. Payload: 'E63B42off', template output: 'E63B42off', with value template 'Template("{{value_json.RfReceived.Data}}")'
2021-06-03 12:32:09 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'value_json' is undefined when rendering '{{value_json.RfReceived.Data}}'
2021-06-03 12:32:09 ERROR (MainThread) [homeassistant.helpers.template] Error parsing value: 'value_json' is undefined (value: E63B42off, template: {{value_json.RfReceived.Data}})
2021-06-03 12:32:09 INFO (MainThread) [homeassistant.components.mqtt.binary_sensor] No matching payload found for entity: Patio Door with state topic: tele/sonoff/RESULT. Payload: 'E63B42off', template output: 'E63B42off', with value template 'Template("{{value_json.RfReceived.Data}}")'
2021-06-03 12:32:09 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'value_json' is undefined when rendering '{{value_json.RfReceived.Data}}'
2021-06-03 12:32:09 ERROR (MainThread) [homeassistant.helpers.template] Error parsing value: 'value_json' is undefined (value: E63B42off, template: {{value_json.RfReceived.Data}})
2021-06-03 12:32:09 INFO (MainThread) [homeassistant.components.mqtt.binary_sensor] No matching payload found for entity: Front Door with state topic: tele/sonoff/RESULT. Payload: 'E63B42off', template output: 'E63B42off', with value template 'Template("{{value_json.RfReceived.Data}}")'
2021-06-03 12:32:09 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'value_json' is undefined when rendering '{{value_json.RfReceived.Data}}'
2021-06-03 12:32:09 ERROR (MainThread) [homeassistant.helpers.template] Error parsing value: 'value_json' is undefined (value: E63B42off, template: {{value_json.RfReceived.Data}})
2021-06-03 12:32:09 INFO (MainThread) [homeassistant.components.mqtt.binary_sensor] No matching payload found for entity: Kitchen Door with state topic: tele/sonoff/RESULT. Payload: 'E63B42off', template output: 'E63B42off', with value template 'Template("{{value_json.RfReceived.Data}}")'
2021-06-03 12:32:09 ERROR (MainThread) [homeassistant.helpers.template] Template variable error: 'value_json' is undefined when rendering '{{value_json.RfReceived.Data}}'
2021-06-03 12:32:09 ERROR (MainThread) [homeassistant.helpers.template] Error parsing value: 'value_json' is undefined (value: E63B42off, template: {{value_json.RfReceived.Data}})
2021-06-03 12:32:09 INFO (MainThread) [homeassistant.components.mqtt.binary_sensor] No matching payload found for entity: Back Door with state topic: tele/sonoff/RESULT. Payload: 'E63B42off', template output: 'E63B42off', with value template 'Template("{{value_json.RfReceived.Data}}")'

This is from the log file just now. 12:32 was about 20 minutes ago.
Now I look at it, why would it be sending the same payload to all those entities…?

What, exactly, did you mean by this? You say “several” then only paste one…

I mean the binary_sensors.yaml contains references to lots of sensors which are now all set up the same way as the one I showed above. I have commented out all references to the device using E63B42 and E63B42off in both automations.yaml and binary_sensors.yaml. I did this as a start point to see if I could identify what could fix it so that I can then apply that same fix to the others.

I’m contemplating commenting out every line in both files to see what affect that has.

It’d be good to see what’s actually being sent over MQTT from the sensors. I assume that E63B42 is your doorbell one, and that the other sensors broadcast different IDs — and that you have put those IDs into the config for each as appropriate.

I don’t know why these messages are persisting if you have deleted references to the template. Have a look under Configuration / Entities and see if they are still appearing there despite being commented out.

Another possibility is that the sensor is sending the structured data some of the time, and the plain string at other times. Perhaps try uncommenting one sensor, adjusted like this:

  - platform: mqtt
    state_topic: "tele/sonoff/RESULT"
    name: "Office Motion"
    value_template: >-
      {% if value_json.RfReceived is defined %}
        {{ value_json.RfReceived.Data }}
      {% else %}
        {{ value_json }}
      {% endif %}
    payload_on: "E63B42"
    payload_off: "E63B42off"
    device_class: "motion"
    qos: 1

That allows the sensor to deal with either scenario of input data.

OK, I’m still baffled by this. I took out everything from automations and from bin_sensors and the errors disappeared.
I added 5 sensors back in, none of which even mentioned E63B42, and there were 25 errors in the log all mentioning it. I took three out, leaving just two, and there were then four errors mentioning it. The number of errors seems to be the square of the number of sensors, so something is obviously not right about the way I’ve set this up.
Somewhere there’s a template that specifically refers to E63B42off, but I have no idea where it could be :confused:

My automations.yaml is now empty. bin sensors has just a single sensor in it and looks like this:

# Motion Sensors

  - platform: mqtt
    state_topic: "tele/sonoff/RESULT"
    name: "Landing Motion"
    value_template: >-
      {% if value_json.RfReceived is defined %}
        {{ value_json.RfReceived.Data }}
      {% else %}
        {{ value_json }}
      {% endif %}
    payload_on: "C91B26"
    payload_off: "C91B26off"
    device_class: "motion"
    qos: 1

…and yet this error still appears in the log on startup:

Error parsing value: 'value_json' is undefined (value: E63B42off, template: {% if value_json.RfReceived is defined %} {{ value_json.RfReceived.Data }} {% else %} {{ value_json }} {% endif %})
11:39:07 – (ERROR) helpers/template.py
Template variable error: 'value_json' is undefined when rendering '{% if value_json.RfReceived is defined %} {{ value_json.RfReceived.Data }} {% else %} {{ value_json }} {% endif %}'
11:39:07 – (ERROR) helpers/template.py

It’s like that trigger value is cached somewhere in the system and gets sent out to every sensor. I’ve restarted the Sonoff Bridge and restarted HA with the Bridge turned off. The error still appears, so I know the problem exists within HA somewhere. But where?

My best guess is that your system is haunted.

What integrations are listed as running?

There are a lot running. I disabled the MQTT integration then restarted, and the error disappeared. I stopped Mosquitto and restarted, and the error also disappeared. Probably unsurprisingly.
When I started Mosquitto again, the error immediately appeared in the log.

If I listen to tele/sonoff/RESULT in the MQTT settings page, I get the following:

Message 1 received on tele/sonoff/RESULT at 13:04:
E63B42off
QoS: 0 - Retain: true 

Is this message being retained somewhere and if so, how do I reset it?

I’ve published a null value to that topic and it temporarily gets set to 0 with retain false, but as soon as I listen again it’s jumped back to the value above with retain true.