Checking if the drone is armed or disarmed

I want to start logging the data when the drone is armed and stop logging when the drone is disarmed. To do so, I want to first check if the drone is armed or not if the drone is armed or not. I have used the following code:

import time

from pymavlink import mavutil

import val # logging file

# create a MAVLink connection

master = mavutil.mavlink_connection('udpin:127.0.0.1:14551')

# check if the drone is armed

while True:

armed = False

# get the current system status

msg = master.recv_match(type='HEARTBEAT', blocking=True)

# check if the drone is armed

armed = (msg.base_mode & mavutil.mavlink.MAV_MODE_FLAG_SAFETY_ARMED)

if armed:

print('Drone is armed')

val.s_info(master,'on')

else:

print('Drone is disarmed')

time.sleep(1)

This code is working on SITL, Pixhawk connected to SITL and pizero. But it is showing the following results even though it is armed in the case of pi64 and pi4:
Drone is armed
Drone is disarmed
Drone is disarmed
Drone is armed
Drone is disarmed
Drone is disarmed
Drone is armed
Drone is disarmed
Drone is disarmed
Drone is armed
Drone is disarmed
Drone is disarmed
Drone is disarmed
Drone is disarmed

What is the issue here and how can I fix it? I would really appreciate if someone could help me.

Isn’t there already a “log when disarmed” parameter which you can just turn off?