InfluxDB write api python: write list of dictionaries

i want to use influxdb client library in python to send data to my bucket.

It’s an iot data coming from an api request (different machines with each different sensors).

So far, i managed to process the json response of API call and parsed the data i want to store as follows:
for each machine i have a dictionary with variables(machine_name_sensor_name) as keys and values (float). Whole data is stored in a list (list of dicts)

list_all_data=[
{"machine_name1_sensor_name1": value,
"machine_name1_sensor_name2" : value},
{"machine_name2_sesnor_name1": value,
"machine_name2_sensor_name2" : value}
]

How can i push this data to influxDB ? i’ve been trying to figure this out but it’s not clear. I want to have 1 measurement for each machine . i’m using InfluxDB v2.7.6

so i tried again starting with one machine, still no luck. list index out of range error

#set up client 
client = InfluxDBClient(
	url=url,
	org=org,
	token=token,
	bucket=bucket,
	verify_ssl=False
)

write_api= client.write_api(write_options=SYNCHRONOUS)

machine_name="Oven"
sensor_names=["TC1","TC2"]
sensor_values=[14,125]

#conrtsuct point structure as influxdb expects
for i ,j in enumerate(sensor_names):
    for k,l in enumerate(sensor_values):
         payload = {
                "measurement" : machine_name,
                "tags": {"variable" :sensor_name1[i]},  # expects : tags :{"variable":"TC1"}
		        "fields": {sensor_names[i] : sensor_values[k]}, # expects fields :{"TC1":14}
		}
	data.append(payload)

write_api.write(bucket=bucket, org=org, record=data)

0

i have a list of dictionaries that i want to process so that it matchs influxDB dict format:

dict={
"measurement": 'measurement_name",
"tags" : {"tag_key":"tag_value"},
"fields : {"field_name" : field_value}
}

my list of dicts:

list_val_machines=[
{"machine1_Data_variable1": value,
"machine1_Data_variable2" : value},
{"machine2_Data_variable1": value,
"machine1_Data_variable2" : value}
]

so here is what i tried so far:

for k,v in enumerate(list_val_machines): #loop on list items 
    
    for i,j in v.items(): #iterate on every key value of each dict 
        #list_machine_names=[]
        #for k,v in raw_data_dict.items():
        #list_machine_names.append(machine_name)
        machine_name=i.split('_')[0] #parse machine name 
        variable_names=list(v.keys()) #get variable names (tag keys)
        values_name=list(v.values()) #get values (field values)
        for l,m in enumerate(variable_names):
            for n,o in enumerate(values_name):
                d={
                "measurement" : machine_name,
                "tags" : {"variable_name": m},
                "fields" : {"variable_name": o}     
                }
        list_d.append(d)
data=list_d

data variable looks like this: 
for p in data:
  print(p)

output:

{'measurement': 'machine1', 'tags': {'variable_name': 'machine1_Data_variable1'}, 'fields': {'variable_name': 0.0}}
{'measurement': 'machine1', 'tags': {'variable_name': 'machine1_Data_variable1'}, 'fields': {'variable_name': 0.0}}
{'measurement': 'machine1', 'tags': {'variable_name': 'machine1_Data_variable1'}, 'fields': {'variable_name': 0.0}}
{'measurement': 'machine1', 'tags': {'variable_name': 'machine1_Data_variable1'}, 'fields': {'variable_name': 0.0}}
{'measurement': 'machine2', 'tags': {'variable_name': 'machine2_Data_variable1'}, 'fields': {'variable_name': 0.0}}
{'measurement': 'machine2', 'tags': {'variable_name': 'machine2_Data_variable1'}, 'fields': {'variable_name': 0.0}}
{'measurement': 'machine2', 'tags': {'variable_name': 'machine2_Data_variable1'}, 'fields': {'variable_name': 0.0}}

measurement name is correct but i’m having the same tag field for every machine (exemple: machine1_Data_variable1) and same field value for every variable in a given machine. I don’t understand what is going wrong here. I want to have a 1 dict per variable with correct variable names and value.

Any hints on how to achieve this ?

It seems the problem in your current logic is that you’re trying to process the dictionary as a whole for each key-value pair, rather than processing each key-value pair individually.