Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log entity_id payload and template on MQTT value template error #98353

Merged

Conversation

jbouwh
Copy link
Contributor

@jbouwh jbouwh commented Aug 13, 2023

Proposed change

Improve logging of MQTT payload and template when an error occurs when rendering a template.

Without this PR, debug logging is needed to see what is happening when a template error occurs.
The original exception is still raised to supply a stack trace.

An example is issue #98344 where a template causes a TypeError. The error trace does not tell what template and payload was causing the error. A message telling the the topic and state is logged though`, triggered by the same exception.

Log trace starting with the additional log line added by this PR:

2023-08-16 08:03:38.806 ERROR (MainThread) [homeassistant.components.mqtt.models] TypeError: object of type 'NoneType' has no len() rendering template for entity 'sensor.bedroom_temperature_mqtt_sensor', template: '{{ value_json.program|default(\"\") | truncate(254, True, '', 0) }}', default value: default and payload: {
  "child_lock": null,
  "current_heating_setpoint": 23,
  "deadzone_temperature": null,
  "heat": "OFF",
  "last_seen": "2023-08-13T19:53:40+03:00",
  "linkquality": 51,
  "local_temperature": 26,
  "local_temperature_calibration": null,
  "max_temperature_limit": null,
  "min_temperature_limit": null,
  "preset": "hold",
  "preset_mode": "hold",
  "program": null,
  "running_state": "idle",
  "sensor": null,
  "system_mode": "off"
}
2023-08-16 08:03:38.860 ERROR (MainThread) [homeassistant.util.logging] Exception in message_received when handling msg on 'home/bedroom/temperature': '{
  "child_lock": null,
  "current_heating_setpoint": 23,
  "deadzone_temperature": null,
  "heat": "OFF",
  "last_seen": "2023-08-13T19:53:40+03:00",
  "linkquality": 51,
  "local_temperature": 26,
  "local_temperature_calibration": null,
  "max_temperature_limit": null,
  "min_temperature_limit": null,
  "preset": "hold",
  "preset_mode": "hold",
  "program": null,
  "running_state": "idle",
  "sensor": null,
  "system_mode": "off"
}'
Traceback (most recent call last):
  File "/workspaces/core/homeassistant/components/mqtt/debug_info.py", line 44, in wrapper
    msg_callback(msg)
  File "/workspaces/core/homeassistant/components/mqtt/sensor.py", line 293, in message_received
    _update_state(msg)
  File "/workspaces/core/homeassistant/components/mqtt/sensor.py", line 244, in _update_state
    payload = self._template(msg.payload, PayloadSentinel.DEFAULT)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/workspaces/core/homeassistant/components/mqtt/models.py", line 280, in async_render_with_possible_json_value
    raise ex
  File "/workspaces/core/homeassistant/components/mqtt/models.py", line 265, in async_render_with_possible_json_value
    self._value_template.async_render_with_possible_json_value(
  File "/workspaces/core/homeassistant/helpers/template.py", line 735, in async_render_with_possible_json_value
    return _render_with_context(self.template, compiled, **variables).strip()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/workspaces/core/homeassistant/helpers/template.py", line 2179, in _render_with_context
    return template.render(**kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/jinja2/environment.py", line 1301, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.11/site-packages/jinja2/environment.py", line 936, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 1, in top-level template code
  File "/usr/local/lib/python3.11/site-packages/jinja2/filters.py", line 870, in do_truncate
    if len(s) <= length + leeway:
       ^^^^^^
TypeError: object of type 'NoneType' has no len()

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New integration (thank you!)
  • New feature (which adds functionality to an existing integration)
  • Deprecation (breaking change to happen in the future)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Additional information

Checklist

  • The code change is tested and works locally.
  • Local tests pass. Your PR cannot be merged unless tests pass
  • There is no commented out code in this PR.
  • I have followed the development checklist
  • I have followed the perfect PR recommendations
  • The code has been formatted using Black (black --fast homeassistant tests)
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • The manifest file has all fields filled out correctly.
    Updated and included derived files by running: python3 -m script.hassfest.
  • New or updated dependencies have been added to requirements_all.txt.
    Updated by running python3 -m script.gen_requirements_all.
  • For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description.
  • Untested files have been added to .coveragerc.

To help with the load of incoming pull requests:

@home-assistant
Copy link

Hey there @emontnemery, mind taking a look at this pull request as it has been labeled with an integration (mqtt) you are listed as a code owner for? Thanks!

Code owner commands

Code owners of mqtt can trigger bot actions by commenting:

  • @home-assistant close Closes the pull request.
  • @home-assistant rename Awesome new title Renames the pull request.
  • @home-assistant reopen Reopen the pull request.
  • @home-assistant unassign mqtt Removes the current integration label and assignees on the pull request, add the integration domain after the command.

@jbouwh jbouwh marked this pull request as draft August 13, 2023 18:11
@jbouwh jbouwh marked this pull request as ready for review August 13, 2023 18:32
@home-assistant home-assistant bot marked this pull request as draft August 13, 2023 18:35
@home-assistant
Copy link

Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍

Learn more about our pull request process.

@jbouwh jbouwh marked this pull request as ready for review August 13, 2023 18:45
@jbouwh jbouwh force-pushed the mqtt-value-template-error-logging branch from 9041c56 to 57ed25f Compare August 13, 2023 19:15
@jbouwh jbouwh dismissed gjohansson-ST’s stale review August 13, 2023 19:16

Removed last commit

Co-authored-by: Erik Montnemery <erik@montnemery.com>
Copy link
Contributor

@emontnemery emontnemery left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @jbouwh 👍

Copy link
Member

@gjohansson-ST gjohansson-ST left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks 👍

@gjohansson-ST gjohansson-ST merged commit 5ef6c03 into home-assistant:dev Aug 18, 2023
34 checks passed
@jbouwh jbouwh deleted the mqtt-value-template-error-logging branch August 18, 2023 11:06
@github-actions github-actions bot locked and limited conversation to collaborators Aug 19, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants