zigbee2mqtt icon indicating copy to clipboard operation
zigbee2mqtt copied to clipboard

Too large a device state causes Home Assistant to crash

Open oleynikandrey opened this issue 1 year ago • 11 comments

What happened?

Hello, I've a problem with a thermostat Moes BHT-002-GBLZBW. It has quite long state which lead to HA crash, because HA expects state not longer than 255 symbols.

I've filed an issue to HA, but they said it's problem in zigbee2mqtt: https://github.com/home-assistant/core/issues/103141

As I can see there was a try to fix this issue, but without success: https://github.com/Koenkk/zigbee2mqtt/issues/16460#issuecomment-1537341464 truncate does not work as expected. May be the reason in true written in lower case here: https://github.com/Koenkk/zigbee2mqtt/blob/311ea070b167ddc6d8de1f8922aed0d8b38025d7/lib/extension/homeassistant.ts#L931C50-L931C50?

What did you expect to happen?

No response

How to reproduce it (minimal and precise)

No response

Zigbee2MQTT version

1.33.2

Adapter firmware version

20220219

Adapter

Egony Ebyte E72-2G4M20S1E

Debug log

No response

oleynikandrey avatar Nov 20 '23 16:11 oleynikandrey

I am facing the follwoing error with HA 2023.12.1 and Z2M 1.34.0-1 which might be related to the issue you are describing:

Logger: homeassistant.helpers.entity
Source: helpers/entity.py:872
First occurred: 17:33:59 (203 occurrences)
Last logged: 17:38:59

Failed to set state for sensor.moes_bht_thermostat_dachstudio_program, fall back to unknown
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 872, in _async_write_ha_state
    hass.states.async_set(
  File "/usr/src/homeassistant/homeassistant/core.py", line 1732, in async_set
    state = State(
            ^^^^^^
  File "/usr/src/homeassistant/homeassistant/core.py", line 1309, in __init__
    validate_state(state)
  File "/usr/src/homeassistant/homeassistant/core.py", line 193, in validate_state
    raise InvalidStateError(
homeassistant.exceptions.InvalidStateError: Invalid state with length 941. State max length is 255 characters.

Bastian007 avatar Dec 10 '23 16:12 Bastian007

Hello I have the same error but without crash since the 1.30.0 update I'm now on 2023.12.1 HAOS and 1.34.0-1 z2mqtt and always the same problem Logger: homeassistant.helpers.entity Source: helpers/entity.py:872 First occurred: 10:33:30 (100 occurrences) Last logged: 14:20:09

Failed to set state for sensor.thermostat_program, fall back to unknown Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 872, in _async_write_ha_state hass.states.async_set( File "/usr/src/homeassistant/homeassistant/core.py", line 1732, in async_set state = State( ^^^^^^ File "/usr/src/homeassistant/homeassistant/core.py", line 1309, in init validate_state(state) File "/usr/src/homeassistant/homeassistant/core.py", line 193, in validate_state raise InvalidStateError( homeassistant.exceptions.InvalidStateError: Invalid state with length 930. State max length is 255 characters.

` I can't delete and reinstall the thermostat because i'm not on place... If you have an idea...

samourai64 avatar Dec 11 '23 13:12 samourai64

I have similar issue. As a workaround i filtered "program" from publishing in payload.

maciekdnd avatar Jan 01 '24 12:01 maciekdnd

@jbouwh in https://github.com/home-assistant/core/issues/103141#issuecomment-1791032698 you mention that truncate doesn't work for the value_template, why is this the case? I added this because HA cannot handle too large states.

Koenkk avatar Jan 02 '24 08:01 Koenkk

This is some time ago. AFAIN this is not about attributes, but the state it self. May the truncate calculation is not accurate enough to calculate the actual size after truncating. By in general, states sould be small, any attributes should be static most of the time

jbouwh avatar Jan 02 '24 08:01 jbouwh

@oleynikandrey

truncate does not work as expected. May be the reason in true written in lower case here:

changed this now, can you try and see if this has been fixed?

Changes will be available in the dev branch in a few hours from now.

Koenkk avatar Jan 02 '24 20:01 Koenkk

@Koenkk I just tested latest z2m release (1.35.1) with HA 2024.1.2 and still have these in HA logs:

2024-01-11 19:02:11.693 ERROR (MainThread) [homeassistant.helpers.entity] Failed to set state for sensor.kitchen_thermostat_program, fall back to unknown Traceback (most recent call last): File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1125, in _async_write_ha_state hass.states.async_set( File "/usr/src/homeassistant/homeassistant/core.py", line 1807, in async_set state = State( ^^^^^^ File "/usr/src/homeassistant/homeassistant/core.py", line 1384, in init validate_state(state) File "/usr/src/homeassistant/homeassistant/core.py", line 204, in validate_state raise InvalidStateError( homeassistant.exceptions.InvalidStateError: Invalid state with length 930. State max length is 255 characters.

aleks-fofanov avatar Jan 11 '24 19:01 aleks-fofanov

I think this best thing to do is to disable this entity in HA until the `truncate is fixed.

Koenkk avatar Jan 11 '24 19:01 Koenkk

changed this now, can you try and see if this has been fixed?

No it doesn't fix the problem

oleynikandrey avatar Jan 30 '24 19:01 oleynikandrey

In https://github.com/home-assistant/core/issues/103141#issuecomment-1793439289 I suggested solution, will that not work?

jbouwh avatar Feb 06 '24 08:02 jbouwh

Maybe Z2M can split big state message into separate messages? Each of which will be less than 255 characters long...

PS I use autodiscovering feature and can't create config for every sensor... The HA team says that the size limitation of the state was introduced because performance reasons and does not want to remove it.

Nikolay-Ch avatar Apr 29 '24 08:04 Nikolay-Ch