How to update (not overwrite) an attirubute of a entity

Guys, i have a python script, its a loop where i receive messages, the message can have the value like 00 or 01 , in the loop there a diffent kind of messages, so the ouput of my script is like messages below:

example:
A:01
B:00
C:01
D:00

so my python script is receiving messages and i do a json rest API

# in while loop receiving messages
    url = "http://localhost:8123/api/states/sensor.message"
    data_json = {"attributes": {message_name:message_data}}       
    response = post(url, headers=headers, data=json.dumps(data_json))

the script is working, but instead of creating new attributes, it always overwrites the same attribute, so at the end i always have only 1 attribute , and not multiple like A, B , C , D … it always overwrites the line with the last message, while the message_name is unique

what am i doing wrong

to make it clear, lets say this my first command i the loop:
it will create an atribute called A with an value of 01

url = "http://localhost:8123/api/states/sensor.some_sensor"
data_json = {"state": "sensor","attributes": {"A":"01"}}       
response = post(url, headers=headers, data=json.dumps(data_json))

next command is like :

url = "http://localhost:8123/api/states/sensor.some_sensor"
data_json = {"state": "dobiss","attributes": {"B":"01"}}       
response = post(url, headers=headers, data=json.dumps(data_json))

it will overwrite the previous A value by B…
i want it to create a new attribute B, so i have 2 attributes

You need to send all attributes at once. This would anyway be better than calling the same URL multiple times, just call it once at the end of the loop and pass all attributes.

yes, but i dont know all attributes , i am just populating it
it will start with 1 attribute, if the message name of the next message is different , i want to create a second attribute and so on … it can be like 40 attributes at the end

so before sending, i maybe need to read all attributes first ,then send all it all back… with maybe a new aiitribute, or if the message name already excists, update that one

is there no better way ?

As far as I know the only way is getting all the attributes, add your new attribute to the dict and then post the whole thing to the URL.

ok, thnx for sharing

This is what I am doing, read the state and attributes, change or add an attribute and post it back again.

def set_attribute(sensor_name, attribute, value):
    response = requests.get(API_URL + sensor_name, headers=HEADERS)
    msg = json.loads(response.text)
    msg['attributes'][attribute] = value
    payload = json.dumps({'state':  msg['state'], 'attributes': msg['attributes']})
    requests.post(API_URL + sensor_name, headers=HEADERS, data=payload)

Ok, I also need to make some kind of loop, to read inside all json attributes and just change a single attribuut and then post everything thing back…
If my loop doesn’t find an excisting attribuut, i need to add one

What’s the value in your example? Is it like “attribute_name”:“attribute_value” ?

Yes, correct. attribute is the name of the attribute and value is the value to set for the attribute.

To loop through the attributes:

    for attribute in msg['attributes']:
        # Print the name and value of the attribute
        print(attribute, msg['attributes'][attribute])
        # Do something here...

Thnx, gonna play with it tomorrow…

Appreciated

perfect, didnt need the for loop at all, it replaced already the excisting attribute if already present
if not present, a new one was created