Set_state not working properly with core-2022.4.2

Hi!

I have a problem if I passing the attributes in the set_state function.
For example:
self.set_state("sensor.example", state=1)
it’s working as excepted.
but this is not working: self.set_state("sensor.example", state=1, attributes={"friendly_name": "Example sensor"}) the entity will be created without attributes.
The previous version works correctly: code-2022.3.x version

More information:

state: 1-2 created on core-2022.4.2
state: 3-4 created on core-2022.3.8

self.set_state("sensor.example", state=3)
self.set_state("sensor.example", state=4, attributes={"friendly_name": "Example"})

thank you for any recommendation or a reference

Interesting, first I am hearing of this issue. I am still on core-2022.3.8 myself. I will give this a try once I have a chance and see if I have the same issue.

1 Like

Did you have time to try?

I have not yet, sorry.

I just updated and tested setting a sensor state with attributes and it works fine.

Strange, could you please share your example with me? I tried again with 2022.4.4 and still not working, but I don’t know why. My appDaemon version is: 0.8.2

Here is my example:

self.set_state("sensor.example", state=5)
self.set_state("sensor.example", state=6, unit_of_measurement = 'kWh', attributes={"friendly_name": "Example"})

Here is the result (2022.4.4)


as you see, domain, attributes and created columns are missed.

Sure… I am running AD from the dev branch which is version 4.2.2. The 0.8.2 add-on you are using should be using 4.2.0 but I am not aware of any changes that would cause an issue between those versions.

I have the following code in a test app.

import adbase
class Test(adbase.ADBase):
    def initialize(self):
        self.adapi = self.get_ad_api()
        self.adapi.set_state("sensor.my_test_sensor2", state=1, attributes={"friendly_name": "My Test Sensor 2"})

When ran, I receive the following log messages.

2022-04-16 09:50:54.080431 INFO AppDaemon: Reloading Module: /conf/apps/test/test.py
2022-04-16 09:50:54.084943 INFO AppDaemon: Initializing app test using class Test from module test
2022-04-16 09:50:54.101901 WARNING test: test: Entity sensor.my_test_sensor2 not found in namespace default
2022-04-16 09:50:54.102649 DEBUG test: set state: sensor.my_test_sensor2, {'state': 1, 'attributes': {'friendly_name': 'My Test Sensor 2'}} from test
2022-04-16 09:50:54.102894 INFO AppDaemon: test: Entity sensor.my_test_sensor2 created in namespace: default

When looked at in HA dev tools, I see the following.

image

Thanks for your example, this was also strange when I checked the result on my database:


Here is my test code:
self.adapi.set_state("sensor.my_test_sensor", state=4, attributes={"friendly_name": "My Test Sensor 3"})

But after I opened the devTools, I saw this result:


and boooom, as you can see they changed the DB schema:
image

so all attributes are moved to a separated table. That’s what I missed…
Here is the article for this: 2022.4: Groups! Groups! Groups! - Home Assistant

So everything is fine, just I missed this modification :slight_smile:

and last but not least Happy Easter!

Ahhh ok… I did notice you were looking at the data in a database but didn’t understand why. Glad you figured it out. Happy Easter to you as well.

1 Like