Skip to content

Regression on set_state api call #2464

Description

@jfparis

What happened?

Hello

I am updating some small home assistant script that were previously running smoothly in 4.4.x

When I use the set_state api call, the calls are successful but it messes up with the boolean that are passed as attributes:

  • attributes set to False are dropped and will not appear in the entity
  • attributes set to True are changed to a string "true" in home assistant

This was not the case in the 4.4.x where all attributes would be transmitted with the correct type

Look like this might be linked to the work done in #2425

Version

4.5.12

Installation type

Home Assistant add-on

Relevant log output

Nothing

Relevant code in the app or config file that caused the issue

attributes = {
        "friendly_name": "Octopus Agile Rates",
        "unit_of_measurement": "GBP/kWh",
        "plunge": False,
        "peak": True,
    }
    self.set_state(
        "event.test_entity",
        state=1,
        attributes=attributes,
        replace = True,
    )

Anything else?

Doing a quick hatchet job in utils.py (as per below), I can obtain the correct behavior, but this would likely fixed the other bug you were addressing

def clean_kwargs(val: Any, *, http: bool = False) -> Any:
    match val:
        #case True if http:                                                                                                                                                  
        #    return "true"                                                                                                                                                   
        case str() | int() | float() | bool() | None:
            return val
        case datetime():
            return val.isoformat()
        case Mapping():
            return {k: clean_kwargs(v, http=http) for k, v in val.items()}
        case Iterable():
            return [clean_kwargs(v, http=http) for v in val]
        case _:
            return str(val)

def clean_http_kwargs(val: Any) -> Any:
    """Recursively cleans the kwarg dict to prepare it for use in HTTP requests."""
    cleaned = clean_kwargs(val, http=True)
    pruned = remove_literals(cleaned, (None,)) # Dropped False from the list
    return pruned

Activity

  1. Ten0 commented on Oct 31, 2025

    @Ten0
    Contributor

    This should be tagged as bug.

    Gave another try at updating from 4.4 to 4.5 as well due to this and I'm hitting this issue as well on 4.5.12.

    I noted that in addition to values being unwantedly turned to strings, any key that is "falsy" ("false" or None) disappears, which causes issues due to missing keys later.
    I'm not happy either about my null s being gone, as I do pass optional values in there.

    This is unexpected, since HA entities normally do support having arbitrary JSONs in the attributes, not only strings, and this limits what attributes can be used for.

    It looks like this filtering, if it should ever take place, should only take place in case values are serialized as query parameters for a get request, but not in other cases.

  2. henklars commented on Nov 4, 2025

    @henklars

    I'm having a problem with HASS plugin set_state and int value 0. In 4.5.11 and earlier it has been possible to set int values as the code below (pos: int).

    self.hass.set_state(
        self.valve_pos_sensor,
        state=pos,  
        attributes={
            "unit_of_measurement": "%",
            "icon": "mdi:radiator"
        }
    )

    But since upgrading to dev-image (sha256:e54ef4c1019df5d1f30364726b4b743b1555cef0d1b6fa398edbf84f1ca80d70) im having problem with int value pos == 0. Int values > 0 seems to work fine. Converting to string works as a temp fix.

    Logs:

    2025-11-04 20:57:29.791193 ERROR HASS: [400] HTTP POST: Bad Request {'attributes': {'unit_of_measurement': '%', 'icon': 'mdi:radiator', 'last_changed': '2025-11-04T19:45:11.349126+00:00', 'last_reported': '2025-11-04T19:45:11.349126+00:00', 'last_updated': '2025-11-04T19:45:11.349126+00:00', 'context': {'id': '01K986DRNN5HKGD0DCB4XBBATA', 'user_id': 'b55311abc7494eb8afea6b9920cc6f87'}}}
    2025-11-04 20:57:29.791837 ERROR radiator_livingroom: =====  Radiator.initialize.<locals>.<lambda> for radiator_livingroom  =====
    2025-11-04 20:57:29.792126 ERROR radiator_livingroom: StateCallbackFail: State callback failed for 'sensor.radiator_valve_pos_livingroom' from 'radiator_livingroom'
    2025-11-04 20:57:29.792279 ERROR radiator_livingroom:   args: ('sensor.radiator_valve_pos_livingroom', 'state', 100, 0)
    2025-11-04 20:57:29.792415 ERROR radiator_livingroom:   kwargs: {
    2025-11-04 20:57:29.792503 ERROR radiator_livingroom:       "__thread_id": "thread-18"
    2025-11-04 20:57:29.792601 ERROR radiator_livingroom:   }
    2025-11-04 20:57:29.792689 ERROR radiator_livingroom:   argument of type 'ClientResponseError' is not iterable
    2025-11-04 20:57:29.792820 ERROR radiator_livingroom:   State callbacks should have the following signature:
    2025-11-04 20:57:29.792909 ERROR radiator_livingroom:     state_callback(self, entity, attribute, old, new, **kwargs)
    2025-11-04 20:57:29.793103 ERROR radiator_livingroom:   See https://appdaemon.readthedocs.io/en/latest/APPGUIDE.html#state-callbacks for more information
    2025-11-04 20:57:29.793650 ERROR radiator_livingroom:   TypeError: argument of type 'ClientResponseError' is not iterable
    2025-11-04 20:57:29.796062 ERROR radiator_livingroom:     File "/usr/local/lib/python3.12/site-packages/appdaemon/threads.py", line 1092, in safe_callback
    2025-11-04 20:57:29.796303 ERROR radiator_livingroom:       funcref()
    2025-11-04 20:57:29.796460 ERROR radiator_livingroom:     File "/apps/henklars-net/utility/radiator.py", line 48, in <lambda>
    2025-11-04 20:57:29.796592 ERROR radiator_livingroom:       self.update_frontend(),
    2025-11-04 20:57:29.796721 ERROR radiator_livingroom:       ^^^^^^^^^^^^^^^^^^^^^^
    2025-11-04 20:57:29.796849 ERROR radiator_livingroom:     File "/apps/henklars-net/utility/radiator.py", line 85, in update_frontend
    2025-11-04 20:57:29.797002 ERROR radiator_livingroom:       self.hass.set_state(
    2025-11-04 20:57:29.797093 ERROR radiator_livingroom:     File "/usr/local/lib/python3.12/site-packages/appdaemon/utils.py", line 289, in wrapper
    2025-11-04 20:57:29.797192 ERROR radiator_livingroom:       return run_coroutine_threadsafe(self, coro, timeout=timeout)
    2025-11-04 20:57:29.797293 ERROR radiator_livingroom:              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    2025-11-04 20:57:29.797443 ERROR radiator_livingroom:     File "/usr/local/lib/python3.12/site-packages/appdaemon/utils.py", line 615, in run_coroutine_threadsafe
    2025-11-04 20:57:29.797550 ERROR radiator_livingroom:       return future.result(timeout.total_seconds())
    2025-11-04 20:57:29.797636 ERROR radiator_livingroom:              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    2025-11-04 20:57:29.797737 ERROR radiator_livingroom:     File "/usr/local/lib/python3.12/concurrent/futures/_base.py", line 456, in result
    2025-11-04 20:57:29.797834 ERROR radiator_livingroom:       return self.__get_result()
    2025-11-04 20:57:29.797926 ERROR radiator_livingroom:              ^^^^^^^^^^^^^^^^^^^
    2025-11-04 20:57:29.798072 ERROR radiator_livingroom:     File "/usr/local/lib/python3.12/concurrent/futures/_base.py", line 401, in __get_result
    2025-11-04 20:57:29.798164 ERROR radiator_livingroom:       raise self._exception
    2025-11-04 20:57:29.798281 ERROR radiator_livingroom:     File "/usr/local/lib/python3.12/site-packages/appdaemon/adapi.py", line 1790, in set_state
    2025-11-04 20:57:29.798400 ERROR radiator_livingroom:       return await self.AD.state.set_state(
    2025-11-04 20:57:29.798522 ERROR radiator_livingroom:              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    2025-11-04 20:57:29.798624 ERROR radiator_livingroom:     File "/usr/local/lib/python3.12/site-packages/appdaemon/state.py", line 792, in set_state
    2025-11-04 20:57:29.798743 ERROR radiator_livingroom:       if "entity_id" in result:
    2025-11-04 20:57:29.798875 ERROR radiator_livingroom:          ^^^^^^^^^^^^^^^^^^^^^
    2025-11-04 20:57:29.799086 ERROR radiator_livingroom: ===========================================================================
  3. Mausy5043 commented on Nov 7, 2025

    @Mausy5043

    Same issue for me with floats being exactly 0.0.

  4. Ten0 commented on Nov 7, 2025

    @Ten0
    Contributor
  5. jsl12 commented on Nov 7, 2025

    @jsl12
    Contributor

    Hey there @Ten0, sorry I've been traveling and moving apartments recently. This has been kind of a tricky issue because there needs to be slightly different behavior in many situations and fixing one case seems to break another. In the previous state, it didn't work for setting the state of entities in the input_boolean domain or something. I don't quite remember (I need to go find that issue).

    The fix will be in tinkering with clean_kwargs and clean_http_kwargs more so that it works the right way in all cases, but the main impediment is understanding what all the different cases actually are. Once that's fully known, I can include everything in the testing, and it won't ever break again 🤞

  6. self-assigned this
    on Nov 7, 2025
  7. Ten0 commented on Nov 7, 2025

    @Ten0
    Contributor

    sorry I've been traveling and moving apartments recently

    Apologies, I didn't mean for this to feel like pressure, I just didn't want this to go unnoticed just because it wasn't flagged as a bug. I thought you were the relevant person to tag here because you had worked on the other PR.

    because there needs to be slightly different behavior in many situations and fixing one case seems to break another

    As I mentioned in my previous message, it seems that the actual discriminant on whether we should have one behavior or another is whether the request is a get, with query params serialized in the URL, or not.

  8. jsl12 commented on Nov 8, 2025

    @jsl12
    Contributor

    No worries, I'm (generally) the person for this kind of thing these days. Great to see some other contributions though too.

    I think you're correct about the actual discriminant, but I'm also going to check out #2475 to see if that'll do it

  9. gibman commented on Nov 25, 2025

    @gibman

    I see this issue after updating to latest appdaemon.
    Seems to happen when I have callbacks for the "sun.sun" entity which produces negative numbers for elevation.

    If I go to dev. tools within hass and forcefully set the attribute for elevation to some negative number like (-12.87)
    then an error is written to the logs.

    Logs:

    2025-11-24 23:14:20.183031 ERROR daylight_influence_bar: ===========================================================================
    2025-11-24 23:25:57.956084 ERROR virtual_light_sensors: =====  on_sun_elevation_changed() in virtual_light_sensors  ===============
    2025-11-24 23:25:57.956308 ERROR virtual_light_sensors: StateCallbackFail: State callback failed for 'sun.sun' from 'virtual_light_sensors'
    2025-11-24 23:25:57.956481 ERROR virtual_light_sensors:   args: ('sun.sun', 'elevation', -52.79, -53.75, {'__thread_id': 'MainThread'})
    2025-11-24 23:25:57.956641 ERROR virtual_light_sensors:   argument of type 'ClientResponseError' is not iterable
    2025-11-24 23:25:57.956795 ERROR virtual_light_sensors:   State callbacks should have the following signature:
    2025-11-24 23:25:57.956953 ERROR virtual_light_sensors:     state_callback(self, entity, attribute, old, new, **kwargs)
    2025-11-24 23:25:57.957106 ERROR virtual_light_sensors:   See https://appdaemon.readthedocs.io/en/latest/APPGUIDE.html#state-callbacks for more information
    2025-11-24 23:25:57.957656 ERROR virtual_light_sensors:   TypeError: argument of type 'ClientResponseError' is not iterable
    2025-11-24 23:25:57.958548 ERROR virtual_light_sensors:     File "/usr/lib/python3.12/site-packages/appdaemon/threads.py", line 999, in safe_callback
    2025-11-24 23:25:57.958719 ERROR virtual_light_sensors:       await funcref()
    2025-11-24 23:25:57.958880 ERROR virtual_light_sensors:     File "/config/apps/virtual_light_sensors_async.py", line 137, in on_sun_elevation_changed
    2025-11-24 23:25:57.959036 ERROR virtual_light_sensors:       await self.process()
    2025-11-24 23:25:57.959191 ERROR virtual_light_sensors:     File "/config/apps/virtual_light_sensors_async.py", line 173, in process
    2025-11-24 23:25:57.959343 ERROR virtual_light_sensors:       await self.set_binary_sensor_state(self._virtual_lux_sensor, self._virtual_lux_sensor_name, brightness)
    2025-11-24 23:25:57.959506 ERROR virtual_light_sensors:     File "/config/apps/virtual_light_sensors_async.py", line 215, in set_binary_sensor_state
    2025-11-24 23:25:57.959665 ERROR virtual_light_sensors:       await self.create_or_update_sensor(virtual_sensor_name = entity, value = state, icon = icon, unit_of_measurement = unit_of_measurement, friendly_name=friendly_name)
    2025-11-24 23:25:57.959816 ERROR virtual_light_sensors:     File "/config/apps/baseclass.py", line 101, in create_or_update_sensor
    2025-11-24 23:25:57.959966 ERROR virtual_light_sensors:       await self.set_state(virtual_sensor_name, state=value, attributes=final_attribs)
    2025-11-24 23:25:57.960133 ERROR virtual_light_sensors:     File "/usr/lib/python3.12/site-packages/appdaemon/adapi.py", line 1789, in set_state
    2025-11-24 23:25:57.960281 ERROR virtual_light_sensors:       return await self.AD.state.set_state(
    2025-11-24 23:25:57.960426 ERROR virtual_light_sensors:              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    2025-11-24 23:25:57.960579 ERROR virtual_light_sensors:     File "/usr/lib/python3.12/site-packages/appdaemon/state.py", line 793, in set_state
    2025-11-24 23:25:57.960726 ERROR virtual_light_sensors:       if "entity_id" in result:
    2025-11-24 23:25:57.960870 ERROR virtual_light_sensors:          ^^^^^^^^^^^^^^^^^^^^^
    
  10. sithmein commented on Nov 27, 2025

    @sithmein

    Not sure if it's related but I get

    ERROR HASS: [400] HTTP POST: Bad Request {'attributes': {}}
    

    after updating from 4.4.2 (yes, old) to 4.5.12. It's likely caused by calls such as self.set_state('actor.water_valve_target', state = 0)

  11. Ten0 commented on Nov 27, 2025

    @Ten0
    Contributor

    Yes that's it. Blocked on 4.4 still for the same reason.

  12. Ten0 commented on Feb 12, 2026

    @Ten0
    Contributor

    Apparently this is not fixed? #2531

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

Metadata

Metadata

Assignees

Labels

bugissueSomething isn't working

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions