Remove attribute using appdaemon


How can we remove attribute from an entity using AppDaemon?


i dont think its at all possible to remove an attribute.

  1. use get_state to get all the attributes of the entity
  2. delete the specific attribute from the attributes dict
  3. use set_state to update this new state


so you say that:
for an entity with 10 attributes
and you use set_state with only 1 attribute, the other 9 will be deleted?

i dont think that it works that way.

i have a sensor and add 1 attribute that has time for the attribute name.
and every time an attribute gets added

and i know that with most components that are setup in HA, you can change things, only untill the entity get changed by HA again


I wish there was a workaround for this. I have components created by app daemon, and I need to remove some attributes.


if its created by appdaemon and you have corrected it in the app, then all you need to do is restart home assistant and it will dissapear.


yep, that’s exactly what i mean.
actually, i even create a function called “change_state” for changing only the state instead of deleting all the attributes by using “set_state”.

def change_state(self, entity, state):
    entire_state = self.get_state(entity, attribute='all')
    entire_attribute = entire_state['attributes']
    self.set_state(entity, state=state, attributes=entire_attribute)

i can’t figure out what happened in your time sensor. i’ll try it later.


you are correct.
set_state only updates attributes.
maybe in previous AD version i used that way to replace attributes?
anyway, i have a lot of apps to update.:sob:
thanks a lot.


i know that it can happen with existing sensors (in HA created) but with sensors created in AD it never did remove attributes.
i was the first to use set_state to create sensors, and i always needed to restart AD to get the wrong attributes away.

but not that long ago i started to create other entities with AD (switches, input_booleans, lights) and it seems that they dont hold their attributes. so i think i might need to use your function :wink:


Sorry, old thread, but I found this thread in my search for a solution, so I figured I’d reply so y’all and anyone else searching would have a solution.

set_state() takes and argument “replace” to completely overwrite the attributes dictionary and state instead of just updating them. So:

self.set_state(appdaemon.entity, replace=True, state='on', attributes={})

will completely delete all the attributes, or including some attributes will modify the attributes dictionary keys to match.

EDIT: Well, I only got the above method to work once. Perhaps I imagined it. I’ll leave it here for discussion. Maybe there’s something I’m missing.

Also, the set_state form at the top of the “< >” entities list page will allow you to overwrite attribute dictionaries completely.


where did you get the idea that that option exists?
ist not something that is in the docs, and i never heard of it before.

but attributes={} will clear the attributes probably, because the attributes on HA are a dict.
but only untill the entity gets its value from the original device again.

again only untill the state from the original device is set.


It is an available argument for the set_app_state() function for exactly this use case, so I figured I’d give it a shot for set_state() and see what happened.

I was under the impression we were talking about procedurally generated entities created by appdaemon? The code creating the entity would just need to be updated, and appdaemon will reinitialize that app.


set_app_state is different function then set_state
set_state doesnt have that option.

the question from the TS was general, so i kept the discussion general (when other read this i dont want them to think its possible for entities in general)

but indeed with entities created with AD, the attributes will probably dissappear when you use attributes={}, because you add an empty dict.

i say probably because i didnt try it, and it could very well be needed to restart HA to lose the attributes that were added before. and in that case attrinutes={} would be like not setting any attributes at all.

HA is just not really consistant when it gets to this.
if you set attributes for a switch (that you created with AD) all other attributes are automaticly lost, but if you add attributes to a sensor, the other attributes dont get erased.


Yeah…I’m aware they’re different functions, which is why I drew the comparison. I’m not sure if replace is a valid argument for set_state or not, I haven’t checked the appdaemon source code, but it did work for me once. Like I said previously, I was trying a bunch of stuff, so it’s possible something else triggered the dictionary wipe.

The discussion obviously shifted.

Unfortunately, this is not true. This is the first thing I tried, and it only updates existing attributes or appends additional unique keys to the existing dictionary. Using set_state with a blank dictionary doesn’t update or remove anything. The only thing I’ve found which overwrites the attributes dictionary completely is using the set-state button in the “< >” entities list in the HA GUI. It would be nice if this were exposed as a service, then we could just call that service withe the appropriate json.


still the original question stands and others will read that and skip forward, reading things with the original question in mind, thats why i mention things to keep it complete.

like i said, it depends on the type of entity. i wasnt sure about sensors, but you say it wont work there.
but on switches or input_booleans it will work.

or restarting HA :wink:
and we are talking about changing entity appaerances, after we have made code changes, so at least its helpfull to have that option.

if you want to have an entity with a changing state that has attributes that change you could try input_text (its actually not that different, with the acception that you can change the value from the frontend also, but in dashboard you can show it as a sensor though)