I have a Keyerror since I have added an new key to my MSG_TXT array. I can't see any format errors in MGS_TXT and when I remove this part the code starts working -" ,""time": {timejb}" My code is:
I get this error:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
import datetime import subprocess var = "/home" pipe = subprocess.Popen([ "perl" , "./temper_mon.pl" , var], stdout = subprocess.PIPE) temperature = pipe.communicate()[ 0 ] print (temperature.decode( 'utf-8' )) temperature = (temperature.decode( 'utf-8' )) temperature = temperature.strip( '\n' ) temperature = float (temperature) print ( type (temperature)) import random import time from azure.iot.device import IoTHubDeviceClient, Message CONNECTION_STRING = "HostName=host;DeviceId=898;SharedAccessKey=keykeykey" tjb = datetime.datetime.now() timejb = tjb.strftime( "%H:%M:%S" ) timejb = str (timejb) print ( type (timejb)) humidity = 1 MSG_TXT = '{{"temperature": {temperature},"humidity": {humidity}[b],""time": {timejb}[/b]}}' print (MSG_TXT) def iothub_client_init(): client = IoTHubDeviceClient.create_from_connection_string(CONNECTION_STRING) return client def iothub_client_telemetry_sample_run(): try : client = iothub_client_init() print ( "IoT Hub device sending periodic messages, press Ctrl-C to exit" ) while True : msg_txt_formatted = MSG_TXT. format (temperature = temperature, humidity = humidity, time = timejb) |
Error: File "./IOTTemperature.py", line 59, in <module>
iothub_client_telemetry_sample_run()
File "./IOTTemperature.py", line 39, in iothub_client_telemetry_sample_run
msg_txt_formatted = MSG_TXT.format(temperature=temperature, humidity=humidity, time=timejb)
KeyError: 'timejb'