Keyerror when key is present

Hello, I’m trying to read incoming data using:
Hostname | raspberrypi
System | Raspbian GNU/Linux 10 (buster)
Docker version | 19.03.12
Appdeamon - 4.0.3

the code I’m using is: I’ve snipped out the bulk of the pre serial to save your eyes

def run_every_Serial(self, kwargs):
#set up serial load data etc done here before....#
  if ser.isOpen():
        ser.write(data)
        ser.flush()
        try:
            #read
            doc=ser.readline()
            buttonUpdate=1
            sensorUpdate=1
        except Exception as e:
            self.log("Error : try to parse an incomplete message")
            pass

  if buttonUpdate==1:
        self.updateButtons(doc)

  def updateButtons(self, doc):
     self.log("2. Recieved: %s\n",doc)
     self.log("3: %s",doc['pbr_auto_man'])
     self.log("4: %s",doc['pbr_start_stop'])
     self.log("5: %s",doc['light_auto_man'])
     self.log("6: %s",doc['lp_1'])

from the Appdaemon log, I can see that message has been received. the issue is when I call the key’s every second key throws the following error below:
Unsure as to why I’ve tried including .decode()/.decode(‘utf-8’)/.decode(‘ascii’).
if I comment it out, self.log(5… ) will be fine and self.log(6…) will have the error.

  def updateButtons(self, doc):
     self.log("2. Recieved: %s\n",doc)
     self.log("3: %s",doc['pbr_auto_man'])
     ##self.log("4: %s",doc['pbr_start_stop'])
     self.log("5: %s",doc['light_auto_man'])
     self.log("6: %s",doc['lp_1'])

error:

2020-08-19 02:55:25.511764 INFO serialArduinoPi: 3: 1
2020-08-19 02:55:25.512433 WARNING serialArduinoPi: ------------------------------------------------------------
2020-08-19 02:55:25.512980 WARNING serialArduinoPi: Unexpected error in worker for App serialArduinoPi:
2020-08-19 02:55:25.513588 WARNING serialArduinoPi: Worker Ags: {'id': '0484a5ce91624f8da1f194c7f7ae55ea', 'name': 'serialArduinoPi', 'objectid': '0d49c618014d45b8b782cfdceaab2c73', 'type': 'scheduler', 'function': <bound method ArduinoPiComms.run_every_Serial of <whyIsSerialDumb.ArduinoPiComms object at 0xb542ed60>>, 'pin_app': True, 'pin_thread': 0, 'kwargs': {'interval': 1, '__thread_id': 'thread-0'}}
2020-08-19 02:55:25.514290 WARNING serialArduinoPi: ------------------------------------------------------------
2020-08-19 02:55:25.515130 WARNING serialArduinoPi: Traceback (most recent call last):
  File "/usr/lib/python3.8/site-packages/appdaemon/threading.py", line 766, in worker
    funcref(self.AD.sched.sanitize_timer_kwargs(app, args["kwargs"]))
  File "/config/appdaemon/apps/whyIsSerialDumb.py", line 49, in run_every_Serial
    self.updateButtons(doc)
  File "/config/appdaemon/apps/whyIsSerialDumb.py", line 219, in updateButtons
    self.log("4: %s",doc['pbr_start_stop'])
KeyError: 'pbr_start_stop'

reading from log:

2020-08-19 02:53:05.072693 INFO serialArduinoPi: 1. Outgoing: b'{"pbr_auto_man": 1, "pbr_start_stop:": 1, "light_auto_man": 1, "lp_1:": 1, "lp1_1": 0, "lp1_2:": 0, "lp1_3": 0, "lp1_4:": 0, "lp_2": 1, "lp2_1:": 0, "lp2_2": 0, "lp2_3:": 0, "lp2_4:": 0, "temp_auto_man": 1, "temp_start_stop:": 1, "air_auto_man": 1, "air_start_stop": 1, "dose_auto_man": 1, "dose_start_stop": 0, "dose_ph_up": 1, "dose_ph_down": 1, "dose_nut": 1, "dose_sample": 1, "dose_top_up": 1, "harvset_auto_mann": 0, "harvest_start_stop": 0, "pbr_cycle_length": 0.0, "pbr_cycle_remaining": 0.0, "lux_sv": 0.0, "ph_sv": 5.0, "do_sv": -6.3, "temp_sv": 12.8, "turbity_sv": -10.0, "coil1_sv": -10.0, "coil2_sv": -10.0, "chiller_temp_sv": -10.0, "co2_in_sv": -10.0, "co2_out_sv": -10.0, "pressure_sv": 0.0, "press_valve_sv": 100.0, "dump1_valve_sv": 0.0, "dump2_valve_sv": 0.0}'

2020-08-19 02:53:05.500699 INFO serialArduinoPi: 2. Recieved: {'pbr_auto_man': 1, 'pbr_start_stop:': 1, 'light_auto_man': 1, 'lp_1:': 1, 'lp1_1': 0, 'lp1_2:': 0, 'lp1_3': 0, 'lp1_4:': 0, 'lp_2': 1, 'lp2_1:': 0, 'lp2_2': 0, 'lp2_3:': 0, 'lp2_4:': 0, 'temp_auto_man': 1, 'temp_start_stop:': 1, 'air_auto_man': 1, 'air_start_stop': 1, 'dose_auto_man': 1, 'dose_start_stop': 0, 'dose_ph_up': 0, 'dose_ph_down': 0, 'dose_nut': 0, 'dose_sample': 0, 'dose_top_up': 0, 'harvset_auto_mann': 0, 'harvest_start_stop': 0, 'pbr_cycle_length': 0, 'pbr_cycle_remaining': 0, 'lux_sv': 0, 'ph_sv': 5, 'do_sv': -6.3, 'temp_sv': 12.8, 'turbity_sv': -10, 'coil1_sv': -10, 'coil2_sv': -10, 'chiller_temp_sv': -10, 'co2_in_sv': -10, 'co2_out_sv': -10, 'pressure_sv': 0, 'press_valve_sv': 100, 'dump1_valve_sv': 0, 'dump2_valve_sv': 0}

have double checked by running:

if 'pbr_start_stop' in doc:
        self.log('key present')
    else:
        self.log('key not found')

which returns:
2020-08-19 05:52:29.443850 INFO serialArduinoPi: key not found

so that leads me to believe i’ve done something wrong but for the life of me can’t see what as it is there in the incoming log after doc=json.loads(doc)

i just noticed where the error is occurring! the message has errors with the placement of the quotations

Pre json.loads() the log shows:

{"pbr_auto_man":0,"pbr_start_stop:":1,"light_auto_man":0,"lp_1:":0,"lp1_1":0,"lp1_2:":0,"lp1_3":0,"lp1_4:":0,"lp_2":0,"lp2_1:":0,"lp2_2":0,"lp2_3:":0,"lp2_4:":0,"temp_auto_man":0,"temp_start_stop:":0,"air_auto_man":0,"air_start_stop":0,"dose_auto_man":0,"dose_start_stop":0,"dose_ph_up":0,"dose_ph_down":0,"dose_nut":0,"dose_sample":0,"dose_top_up":0,"harvset_auto_mann":0,"harvest_start_stop":0,"pbr_cycle_length":0,"pbr_cycle_remaining":0,"lux_sv":0,"ph_sv":5,"do_sv":-6.3,"temp_sv":12.8,"turbity_sv":-10,"coil1_sv":-10,"coil2_sv":-10,"chiller_temp_sv":-10,"co2_in_sv":-10,"co2_out_sv":-10,"pressure_sv":0,"press_valve_sv":100,"dump1_valve_sv":0,"dump2_valve_sv":0}

after json.loads():

{'pbr_auto_man': 0, 'pbr_start_stop:': 1, 'light_auto_man': 0, 'lp_1:': 0, 'lp1_1': 0, 'lp1_2:': 0, 'lp1_3': 0, 'lp1_4:': 0, 'lp_2': 0, 'lp2_1:': 0, 'lp2_2': 0, 'lp2_3:': 0, 'lp2_4:': 0, 'temp_auto_man': 0, 'temp_start_stop:': 0, 'air_auto_man': 0, 'air_start_stop': 0, 'dose_auto_man': 0, 'dose_start_stop': 0, 'dose_ph_up': 0, 'dose_ph_down': 0, 'dose_nut': 0, 'dose_sample': 0, 'dose_top_up': 0, 'harvset_auto_mann': 0, 'harvest_start_stop': 0, 'pbr_cycle_length': 0, 'pbr_cycle_remaining': 0, 'lux_sv': 0, 'ph_sv': 5, 'do_sv': -6.3, 'temp_sv': 12.8, 'turbity_sv': -10, 'coil1_sv': -10, 'coil2_sv': -10, 'chiller_temp_sv': -10, 'co2_in_sv': -10, 'co2_out_sv': -10, 'pressure_sv': 0, 'press_valve_sv': 100, 'dump1_valve_sv': 0, 'dump2_valve_sv': 0}

went full noob, error in my code.