Home > front end >  Every dict value gets updated even tho i point at a specific key - PYTHON
Every dict value gets updated even tho i point at a specific key - PYTHON

Time:10-12

is this a bug or am I missing something?

Im iterating trough a list of topics with a for loop. In that loop i update a key in a premade dict with the value of another dict. This part is working fine. So now i gave a payload dict with the new unit value in it and a topic string. But the last step where i want to write a new dict with the topic as key and the whole payload as value does not really work, cause it always updates every value of the dict instead of just the one i point to.

Has anyone a idea what i am doing wrong? Thanks!

Code:

for topic in self.topics:
                
    counter  = 1
    print(counter)
    payload['unit'] = units[topic]
    print(payload)
    print(topic)
    data[topic] = payload
    print(data)

Output:

1
{'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}
/test/mqtt/Hilfsknoten1
{'/test/mqtt/Hilfsknoten1': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}}

2
{'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}
/test/mqtt/Hilfsknoten2
{'/test/mqtt/Hilfsknoten1': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/Hilfsknoten2': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}}

3
{'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}
/test/mqtt/RaumtemperaturFP32
{'/test/mqtt/Hilfsknoten1': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/Hilfsknoten2': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/RaumtemperaturFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}}

4
{'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}
/test/mqtt/StromFP32
{'/test/mqtt/Hilfsknoten1': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/Hilfsknoten2': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/RaumtemperaturFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/StromFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}}

5
{'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}
/test/mqtt/SpannungFP32
{'/test/mqtt/Hilfsknoten1': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/Hilfsknoten2': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/RaumtemperaturFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/StromFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/SpannungFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}}

6
{'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}
/test/mqtt/FrequenzFP32
{'/test/mqtt/Hilfsknoten1': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/Hilfsknoten2': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/RaumtemperaturFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/StromFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/SpannungFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/FrequenzFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}}

7
{'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}
/test/mqtt/WirkleistungFP32
{'/test/mqtt/Hilfsknoten1': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/Hilfsknoten2': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/RaumtemperaturFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/StromFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/SpannungFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/FrequenzFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/WirkleistungFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}}

8
{'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}
/test/mqtt/BlindleistungFP32
{'/test/mqtt/Hilfsknoten1': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/Hilfsknoten2': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/RaumtemperaturFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/StromFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/SpannungFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/FrequenzFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/WirkleistungFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/BlindleistungFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}}

9
{'value': 'float', 'timestamp': 'iso8601', 'unit': 'Vu'}
/test/mqtt/snmp-ausgesch.Service
{'/test/mqtt/Hilfsknoten1': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'Vu'}, '/test/mqtt/Hilfsknoten2': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'Vu'}, '/test/mqtt/RaumtemperaturFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'Vu'}, '/test/mqtt/StromFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'Vu'}, '/test/mqtt/SpannungFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'Vu'}, '/test/mqtt/FrequenzFP32': {'value': 'float', 'timestamp': 
'iso8601', 'unit': 'Vu'}, '/test/mqtt/WirkleistungFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'Vu'}, '/test/mqtt/BlindleistungFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'Vu'}, '/test/mqtt/snmp-ausgesch.Service': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'Vu'}}

10
{'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}
/test/mqtt/Alarm-Ausgang01
{'/test/mqtt/Hilfsknoten1': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/Hilfsknoten2': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/RaumtemperaturFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/StromFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/SpannungFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/FrequenzFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/WirkleistungFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/BlindleistungFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/snmp-ausgesch.Service': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/Alarm-Ausgang01': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}}

11
{'value': 'float', 'timestamp': 'iso8601', 'unit': 'Vu'}
/test/mqtt/T3-Bereit
{'/test/mqtt/Hilfsknoten1': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'Vu'}, '/test/mqtt/Hilfsknoten2': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'Vu'}, '/test/mqtt/RaumtemperaturFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'Vu'}, '/test/mqtt/StromFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'Vu'}, '/test/mqtt/SpannungFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'Vu'}, '/test/mqtt/FrequenzFP32': {'value': 'float', 'timestamp': 
'iso8601', 'unit': 'Vu'}, '/test/mqtt/WirkleistungFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'Vu'}, '/test/mqtt/BlindleistungFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'Vu'}, '/test/mqtt/snmp-ausgesch.Service': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'Vu'}, '/test/mqtt/Alarm-Ausgang01': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'Vu'}, '/test/mqtt/T3-Bereit': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'Vu'}}

CodePudding user response:

Hard to understand what you need. Does this give you the output you expect?

topics = ['/test/mqtt/Hilfsknoten1', '/test/mqtt/Hilfsknoten2', '/test/mqtt/RaumtemperaturFP32']
payload = {'value': 'float', 'timestamp': 'iso8601', 'unit': True}
units = {'/test/mqtt/Hilfsknoten1': 'V', '/test/mqtt/Hilfsknoten2': 'Vu', '/test/mqtt/RaumtemperaturFP32': 'A'}

counter = 0
data = {}

for topic in topics:
                
    counter  = 1
    print("\n")
    print(counter)
    payload_copy = payload.copy()
    payload_copy['unit'] = units[topic]
    print(payload_copy)
    print(topic)
    data[topic] = payload_copy
    print(data)

Last output: 3

{'value': 'float', 'timestamp': 'iso8601', 'unit': 'A'} /test/mqtt/RaumtemperaturFP32 {'/test/mqtt/Hilfsknoten1': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/Hilfsknoten2': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'Vu'}, '/test/mqtt/RaumtemperaturFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'A'}}

CodePudding user response:

If someone wants to test it on an small example:

topics = ['/test/mqtt/Hilfsknoten1', '/test/mqtt/Hilfsknoten2', '/test/mqtt/RaumtemperaturFP32']
payload = {'value': 'float', 'timestamp': 'iso8601', 'unit': True}
units = {'/test/mqtt/Hilfsknoten1': 'V', '/test/mqtt/Hilfsknoten2': 'Vu', '/test/mqtt/RaumtemperaturFP32': 'A'}

counter = 0
data = {}

for topic in topics:
                
    counter  = 1
    print("\n")
    print(counter)
    payload['unit'] = units[topic]
    print(payload)
    print(topic)
    data[topic] = payload
    print(data)

What i get:

1
{'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}
/test/mqtt/Hilfsknoten1
{'/test/mqtt/Hilfsknoten1': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}}


2
{'value': 'float', 'timestamp': 'iso8601', 'unit': 'Vu'}
/test/mqtt/Hilfsknoten2
{'/test/mqtt/Hilfsknoten1': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'Vu'}, '/test/mqtt/Hilfsknoten2': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'Vu'}}


3
{'value': 'float', 'timestamp': 'iso8601', 'unit': 'A'}
/test/mqtt/RaumtemperaturFP32
{'/test/mqtt/Hilfsknoten1': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'A'}, '/test/mqtt/Hilfsknoten2': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'A'}, '/test/mqtt/RaumtemperaturFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'A'}}

What i want:

1
{'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}
/test/mqtt/Hilfsknoten1
{'/test/mqtt/Hilfsknoten1': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}}


2
{'value': 'float', 'timestamp': 'iso8601', 'unit': 'Vu'}
/test/mqtt/Hilfsknoten2
{'/test/mqtt/Hilfsknoten1': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/Hilfsknoten2': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'Vu'}}


3
{'value': 'float', 'timestamp': 'iso8601', 'unit': 'A'}
/test/mqtt/RaumtemperaturFP32
{'/test/mqtt/Hilfsknoten1': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'V'}, '/test/mqtt/Hilfsknoten2': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'Vu'}, '/test/mqtt/RaumtemperaturFP32': {'value': 'float', 'timestamp': 'iso8601', 'unit': 'A'}}
  • Related