Skip to content

fix: Home Assistant: guard value templates against missing properties - #33071

Open
deviantintegral wants to merge 4 commits into
Koenkk:devfrom
deviantintegral:fix/ha-discovery-value-template-guards
Open

deviantintegral wants to merge 4 commits into
Koenkk:devfrom
deviantintegral:fix/ha-discovery-value-template-guards

Conversation

@deviantintegral

@deviantintegral deviantintegral commented Sep 7, 2026

Copy link
Copy Markdown
Contributor

I'm getting occasional errors when devices report with missing properties in Home Assistant. This PR should fix that!

I'm pushing the tests first which should fail, followed by the fix.

This PR also removes mockProperties based on the discussion below. Of note is that messages no longer contain properties with null values.

Before:

{
  action: "single",
  battery: null,
  identify: null,
  linkquality: null,
  voltage: null,
  power_outage_count: null,
  device_temperature: null,
}

After:

{
  action: "single"
}

@deviantintegral
deviantintegral changed the base branch from master to dev September 7, 2026 18:13
@deviantintegral
deviantintegral force-pushed the fix/ha-discovery-value-template-guards branch from c205305 to 47a5435 Compare September 7, 2026 18:14
@Koenkk

Koenkk commented Sep 8, 2026

Copy link
Copy Markdown
Owner
  • What value will HA show if no value is available?
  • I like this approach more than the current mockProperties, I think this can fully replace the mockProperties?

@deviantintegral

Copy link
Copy Markdown
Contributor Author

For most entities, the guard never fires. adjustMessageBeforePublish will inject the key with null, so prop is true and we render None. I think that gets mapped to [PAYLOAD_NONE](https://github.com/home-assistant/core/blob/d5b6bd957d47fa3cfacfee2884e18613d8d592f5/homeassistant/components/mqtt/const.py#L383), which leads to unknown. I don't think this is changed compared to dev.

For the ones without a matching mockProperty, the template renders "" and HA drops the message at a debug level. The entity should just show whatever the last value was.

So, the only visible effect is that the Jinja warnings stop. Hopefully!

I do think it's likely this could fully replace mockProperties, but that's probably a bigger change. Is that better as a separate PR or bundled in here?

@deviantintegral
deviantintegral force-pushed the fix/ha-discovery-value-template-guards branch from ce909c0 to 535be60 Compare September 11, 2026 00:26
@Koenkk

Koenkk commented Sep 14, 2026

Copy link
Copy Markdown
Owner

Is that better as a separate PR or bundled in here?

If you see the chance, than I would prefer to bundle it here.

deviantintegral and others added 2 commits September 18, 2026 16:43
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ChP1SPEr3tYXAVp4S2o9Kr
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01ChP1SPEr3tYXAVp4S2o9Kr
@deviantintegral
deviantintegral force-pushed the fix/ha-discovery-value-template-guards branch from 535be60 to 69e2a10 Compare September 18, 2026 20:45
The discovery value templates now check that a property is present before
reading it, so there is no longer a reason to pad every published payload
with `null`s for properties the device hasn't reported yet.

Drop the `mockProperties` mechanism entirely. Payloads only carry what was
actually reported, and entities without a value keep their previous state
instead of being reset to unknown on every message.

The `update` entity, whose payload is built from nested reads, is guarded
the same way and now reads `state` with `.get()` so a payload without it
does not log a template variable warning either.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QcurGn7eowCt5Uh1Whxnq3
@deviantintegral
deviantintegral force-pushed the fix/ha-discovery-value-template-guards branch from 69e2a10 to ca21778 Compare September 18, 2026 20:47
@deviantintegral

Copy link
Copy Markdown
Contributor Author

Done! Let me know if you think we should preserve null values. I think it's a reasonable cleanup, but a strict reading would consider that a compatibility break.

@Koenkk

Koenkk commented Sep 20, 2026

Copy link
Copy Markdown
Owner

great, I migrated the remaining cases, will merge this after the 1 October release such that we get enough test time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants