mfaust78 / AmtronXtraModbus

Use Home assistant to get data from your Amtron Xtra / Premium
7 stars 1 forks source link

InvalidStateError: Invalid state with length 285. State max length is 255 characters. #1

Closed redlefloh closed 7 months ago

redlefloh commented 7 months ago

For some strange reasons, starting with HA 2024.2 I receive the error message below which leads to all the template sensors not being parsed correctly. When I downgrade back to HA 2024.1.6 everything works fine again? I could not find any changes in the HA release notes that were obviously connected to this issues.

Are you @mfaust78 or is anyone else seeing this issue (or maybe can confirm, that for them this still works also in 2024.2)? Thanks!

2024-02-11 12:10:19.203 ERROR (MainThread) [homeassistant.helpers.entity] Failed to set state for sensor.mennekes_amtron_xtra_registers, fall back to unknown
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/entity.py", line 1172, in _async_write_ha_state
    hass.states.async_set(
  File "/usr/src/homeassistant/homeassistant/core.py", line 1911, in async_set
    state = State(
            ^^^^^^
  File "/usr/src/homeassistant/homeassistant/core.py", line 1435, in __init__
    validate_state(state)
  File "/usr/src/homeassistant/homeassistant/core.py", line 206, in validate_state
    raise InvalidStateError(
homeassistant.exceptions.InvalidStateError: Invalid state with length 285. State max length is 255 characters.

My configuration for reference:

# https://github.com/mfaust78/AmtronXtraModbus
# https://www.home-assistant.io/integrations/modbus/
- name: Mennekes Amtron Xtra
  type: tcp
  host: 192.168.12.200
  port: 502
  timeout: 5
  delay: 1
  sensors:
    - name: "Mennekes Amtron Xtra Registers"
      unique_id: f41b022d-c1bd-41d2-a01b-16700c5fde6d
      slave: 255
      address: 0x0300
      scan_interval: 10
      input_type: input
      count: 38
      data_type: custom
      structure: ">2h15H22B10H"

    - name: "Mennekes Amtron Xtra - Stromlimit Setzen"
      unique_id: 6d3696ea-4a91-458c-8b71-6de2906e1a99
      slave: 255
      address: 0x0400
      scan_interval: 10
      data_type: uint16
      input_type: holding
      unit_of_measurement: A
      device_class: current      

    - name: "Mennekes Amtron Xtra - Ladezustand Setzen"
      unique_id: e06c40d0-ad10-4197-b41a-7c0f2856f36f
      slave: 255
      address: 0x0401
      scan_interval: 10
      data_type: uint16
      input_type: holding
redlefloh commented 7 months ago

I did a bit of digging and count: 38 and the python struct: ">2h15H22B10H" leads to 285 Bytes. I changed it to count: 31 and structure: ">2h15H22B3H" which still gives me all the template sensor values that I need and that now works fine. Looks like 2024.2 now pays closer attention to the length of the payload (or 2024.1. truncated it)? Either way, the above fixes the issue for me.

mfaust78 commented 6 months ago

thanks for the input. I did some digging and seams 2024.2 or 2024.1 change the precision. Although the values in the struct are int they are converted by HA to float and displayed as for example 4.00. This leads to a way longer string and breaks it. I've added precision: 0 to the sensor, which removes the ".00" and the string fits again. Further the mapper state this way still work.